'Error load test api login using FastAPI and locust
I have written the simple code about login user, generate access token by fastapi:
import fastapi as _fastapi
import fastapi.security as _security
import sqlalchemy.orm as _orm
import services as _services
import schemas as _schemas
app = _fastapi.FastAPI()
@app.post("/api/users")
async def create_user(user: _schemas.UserCreate, db: _orm.Session = _fastapi.Depends(_services.get_db)):
db_user = await _services.get_user_by_email(email=user.email, db=db)
if db_user:
raise _fastapi.HTTPException(
status_code=400,
detail="User with that email already exists")
user = await _services.create_user(user=user, db=db)
return await _services.create_token(user=user)
@app.post("/api/token")
async def generate_token(
form_data: _security.OAuth2PasswordRequestForm = _fastapi.Depends(),
db: _orm.Session = _fastapi.Depends(_services.get_db)):
user = await _services.authenticate_user(
email=form_data.username, password=form_data.password, db=db)
if not user:
raise _fastapi.HTTPException(
status_code=401, detail="Invalid Credentials")
return _services.create_token(user=user)
database config:
import sqlalchemy as _sql
import sqlalchemy.ext.declarative as _declarative
import sqlalchemy.orm as _orm
DATABASE_URL = 'postgresql://localhost:5432/test1'
engine = _sql.create_engine(
DATABASE_URL, pool_size=15, max_overflow=5)
SessionLocal = _orm.sessionmaker(
autocommit=False, autoflush=False, bind=engine)
Base = _declarative.declarative_base()
I use uvicorn and locust to load test but it has very small rps and limit number request. Locust have error:
POST /api/token: RemoteDisconnected('Remote end closed connection without response')
POST /api/token: ConnectionResetError(54, 'Connection reset by peer')
I try to change connection pool and upgrade request[sercurity] but inoperative.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
