diff --git a/Makefile b/Makefile index e1e25a5..ab4b1e7 100644 --- a/Makefile +++ b/Makefile @@ -27,11 +27,14 @@ test-headed: locust: export DEVPLACE_DATABASE_URL="sqlite:///$(LOCUST_DB)"; \ export DEVPLACE_RATE_LIMIT=$(DEVPLACE_RATE_LIMIT); \ + fuser -k $(LOCUST_PORT)/tcp 2>/dev/null || true; \ + sleep 1; \ mkdir -p $(LOCUST_DB_DIR); \ rm -f $(LOCUST_DB); \ uvicorn devplacepy.main:app --host 127.0.0.1 --port $(LOCUST_PORT) --backlog 8192 > /tmp/devplace_locust_server.log 2>&1 & \ PID=$$!; \ - while ! curl -s http://127.0.0.1:$(LOCUST_PORT)/ > /dev/null 2>&1; do sleep 0.5; done; \ + while kill -0 $$PID 2>/dev/null && ! curl -s http://127.0.0.1:$(LOCUST_PORT)/ > /dev/null 2>&1; do sleep 0.5; done; \ + if ! kill -0 $$PID 2>/dev/null; then echo "Server failed to start (port $(LOCUST_PORT) busy?). See /tmp/devplace_locust_server.log"; exit 1; fi; \ locust --host http://127.0.0.1:$(LOCUST_PORT) --web-port $(LOCUST_WEB_PORT); \ kill $$PID 2>/dev/null || true; \ rm -rf $(LOCUST_DB_DIR) @@ -39,11 +42,14 @@ locust: locust-headless: export DEVPLACE_DATABASE_URL="sqlite:///$(LOCUST_DB)"; \ export DEVPLACE_RATE_LIMIT=$(DEVPLACE_RATE_LIMIT); \ + fuser -k $(LOCUST_PORT)/tcp 2>/dev/null || true; \ + sleep 1; \ mkdir -p $(LOCUST_DB_DIR); \ rm -f $(LOCUST_DB); \ uvicorn devplacepy.main:app --host 127.0.0.1 --port $(LOCUST_PORT) --backlog 8192 > /tmp/devplace_locust_server.log 2>&1 & \ PID=$$!; \ - while ! curl -s http://127.0.0.1:$(LOCUST_PORT)/ > /dev/null 2>&1; do sleep 0.5; done; \ + while kill -0 $$PID 2>/dev/null && ! curl -s http://127.0.0.1:$(LOCUST_PORT)/ > /dev/null 2>&1; do sleep 0.5; done; \ + if ! kill -0 $$PID 2>/dev/null; then echo "Server failed to start (port $(LOCUST_PORT) busy?). See /tmp/devplace_locust_server.log"; exit 1; fi; \ locust --host http://127.0.0.1:$(LOCUST_PORT) --headless -u $(LOCUST_USERS) -r $(LOCUST_SPAWN_RATE) --run-time $(LOCUST_RUN_TIME) --html=$(LOCUST_DB_DIR)/report.html; \ kill $$PID 2>/dev/null || true; \ rm -rf $(LOCUST_DB_DIR) diff --git a/locustfile.py b/locustfile.py index 168161d..1e59938 100644 --- a/locustfile.py +++ b/locustfile.py @@ -72,7 +72,10 @@ def seed_data(environment, **kwargs): import http.cookiejar created = 0 - while created < 5: + attempts = 0 + max_attempts = 25 + while created < 5 and attempts < max_attempts: + attempts += 1 username = random_username() email = f"{username}@locust.devplace" body = urllib.parse.urlencode({ @@ -88,6 +91,13 @@ def seed_data(environment, **kwargs): except Exception as e: logger.warning(f"Create seed user failed: {e}") + if created < 5: + logger.error( + f"Seeding aborted: only {created}/5 users created after " + f"{attempts} attempts. Is the server healthy at {host}?" + ) + return + logger.info(f"Created {len(SEED_USERS)} seed users") def logged_in_opener(email):