"""Locust load test covering literally all living-example functionality. Run against a live server: make -f example/example.mk example ./build/example/living-server 8080 & pip install locust locust -f example/locustfile.py --host http://127.0.0.1:8080 Then open the Locust web UI at http://localhost:8089. """ from locust import HttpUser, User, task, between, events try: import websocket # websocket-client: true WS interaction for /ws/chat HAVE_WS = True except ImportError: HAVE_WS = False def fill(path: str) -> str: return ( path.replace("{name}", "locust") .replace("{id}", "1") .replace("{status}", "404") .replace("{tenant}", "acme") ) GETS = [ "/", "/health", "/healthz", "/readyz", "/startupz", "/metrics", "/metrics/counters", "/openapi.json", "/docs", "/stats", "/stats/data", "/greet", "/hello/locust", "/deep", "/nested", "/conditional/doc", "/compress/demo", "/context/trace", "/audit/log", "/audit/mask", "/audit/retention", "/audit/subject", "/audit/subject/export", "/audit/verify", "/auth/me", "/cache/lookup", "/cli/ai", "/cli/commands", "/crypto/hash", "/crypto/password", "/crypto/random", "/csp/page", "/csp/report", "/di/services", "/di/resolve/locust", "/embedded/chart.umd.min.js", "/embedded/hello.txt", "/embedded/stats.js", "/events/fire", "/expose/record", "/expose/stats", "/files/", "/files/welcome.txt", "/flash", "/flash/set", "/formats/data", "/forms/contact", "/governance/diff", "/governance/keys", "/governance/lint", "/governance/problem", "/governance/problem/404", "/governance/tiers", "/governance/versions", "/governance/whoami", "/i18n", "/i18n/po", "/kv/items", "/logging/emit", "/logging/records", "/operability/debug", "/orm/users", "/pubsub/publish", "/redirect/here", "/replication/state", "/restclient/self", "/security/admin", "/security/cors", "/security/csrf", "/security/denials", "/security/guard", "/security/headers", "/security/login", "/security/rate", "/security/roles", "/security/secrets", "/security/upload", "/security/whoami", "/sessions/counter", "/socketio/rooms", "/sse/events", "/static/app.css", "/static/app.js", "/streaming/feed", "/streaming/passthrough", "/supervisor/tasks", "/supervisor/workers", "/supplychain/sbom.json", "/telemetry/trace", "/templates/page", "/tenancy/config", "/tenancy/data", "/tenancy/flags", "/tenancy/metrics", "/tenancy/provision", "/tenancy/quota", "/tenancy/resolve", "/tenancy/t/acme/whoami", "/uploads/file", "/v1/items", "/v2/items", "/validate/user", "/webdav/files/", "/whoami", "/workflow/release", "/routing/hello", "/routing/greet", "/routing/hello/world", "/routing/routers/nested/deep", "/routing/views/greet", "/routing", "/routers", "/views", "/acquire", "/advisory", "/apply", "/bad", "/good", "/get", "/drift", "/enter", "/leave", "/heal", "/failure", "/success", "/request", "/reset", "/release", "/rollback", "/run", "/set", "/start", "/step", "/tick", "/transfer", "/write", "/verify", "/tamper", "/app1", "/app1/app1/app1/whoami", "/clients/retry", "/clients/session", "/data/migrations", "/data/query", "/data/singleflight", "/data/transaction", "/data/ttl", "/di/resolve/testsvc", "/embedded/test.txt", "/middleware/demo", "/operability/breaker", "/operability/bulkhead", "/operability/chaos", "/operability/coord", "/operability/deadline", "/operability/deploy", "/operability/shedder", "/operability/slo", "/readyz/checks/db", "/startupz", "/startupz/tasks/migrate", "/static/app.css", "/supplychain", "/tenancy/t/demo/whoami", "/webdav/files/test.txt", ] # NOT load tested on purpose: everything under /voice, /google and /agent # calls or fronts paid Google APIs (check_locust_coverage.py enforces this). POSTS = [ ("/audit/log", {"event": "locust"}), ("/audit/retention/hold", {"hold": True}), ("/audit/retention/purge", {}), ("/audit/subject/erase", {"subject": "locust"}), ("/audit/tamper", {"probe": True}), ("/auth/login", {"user": "locust", "password": "secret"}), ("/conditional/doc", {}), ("/crypto/hash", {"data": "hello"}), ("/crypto/password", {"password": "secret"}), ("/csp/report", {"report": "test"}), ("/events/fire", {"event": "test"}), ("/expose/record", {"data": "x"}), ("/flash/set", {"msg": "hi"}), ("/forms/contact", {"name": "locust", "email": "l@example.com", "message": "hi"}), ("/governance/diff", {"a": 1, "b": 2}), ("/governance/keys", {"name": "locust"}), ("/governance/keys/1/revoke", {}), ("/governance/lint", {"src": "x"}), ("/greet", {"name": "locust"}), ("/kv/items", {"key": "k", "value": "v"}), ("/logging/emit", {"msg": "hi"}), ("/orm/users", {"name": "locust"}), ("/pubsub/publish", {"topic": "t", "msg": "hi"}), ("/readyz/checks/db", {}), ("/replication/state", {}), ("/security/csrf", {}), ("/security/guard", {"user": "locust"}), ("/security/login", {"user": "locust", "password": "secret"}), ("/security/upload", {"file": "x"}), ("/socketio/rooms", {"room": "general"}), ("/startupz/tasks/migrate", {}), ("/supervisor/tasks", {"task": "t"}), ("/tenancy/config", {"tenant": "acme"}), ("/tenancy/data", {"tenant": "acme", "k": "v"}), ("/tenancy/flags", {"flag": "new-ui"}), ("/tenancy/provision", {"tenant": "acme"}), ("/tenancy/quota", {"tenant": "acme"}), ("/uploads/file", {"file": "x"}), ("/validate/user", {"name": "locust", "email": "l@example.com"}), ("/workflow/release", {"version": "1.0"}), ("/ws/chat", {"msg": "hi"}), ] # Canonical filled variants of every parameterized route. Each hits the # same handler as its template (verified against router registrations in # app/RoutingDemo, PlatformDemo, EnterpriseDemo, FormatsDemo, RealtimeDemo). TEMPLATES = [ "/hello/locust", "/di/resolve/locust", "/di/resolve/", "/embedded/locust", "/governance/keys/1/revoke", "/governance/problem/404", "/readyz/checks/locust", "/startupz/tasks/locust", "/static/locust", "/tenancy/t/acme/whoami", "/webdav/files/locust", ] class LivingExampleUser(HttpUser): wait_time = between(0.1, 0.5) @task def all_gets(self): for path in GETS: self.client.get(fill(path), name="GET batch") @task def all_posts(self): for path, payload in POSTS: self.client.post(fill(path), json=payload, name="POST batch") @task def templates(self): for path in TEMPLATES: self.client.get(path, name="GET template") @task def puts(self): self.client.put("/files/welcome.txt", data=b"hello", name="PUT file") self.client.put("/files/", data=b"hello", name="PUT files") @task def cors_preflight(self): self.client.options( "/security/cors", headers={ "Origin": "http://localhost:8089", "Access-Control-Request-Method": "GET", }, name="OPTIONS cors", ) @task def websocket_probe(self): # Server upgrades only with `Upgrade: websocket`; without it the same # handler returns a JSON usage hint. Exercise both paths. self.client.get("/ws/chat", name="GET ws") self.client.get( "/ws/chat", headers={"Upgrade": "websocket", "Connection": "Upgrade"}, name="GET ws-upgrade", ) class ChatWebsocketUser(User): """True websocket interaction: open ws://host/ws/chat, send/recv, close.""" wait_time = between(1, 3) @task def chat_roundtrip(self): if not HAVE_WS: events.request.fire(request_type="WS", name="/ws/chat", response_time=0, response_length=0, exception=RuntimeError("websocket-client not installed")) return ws_url = self.host.replace("http://", "ws://").replace("https://", "wss://") + "/ws/chat" import time t0 = time.time() try: ws = websocket.create_connection(ws_url, timeout=10) ws.send('{"msg":"hi from locust"}') ws.recv() ws.close() events.request.fire(request_type="WS", name="/ws/chat", response_time=int((time.time() - t0) * 1000), response_length=1, exception=None) except Exception as e: events.request.fire(request_type="WS", name="/ws/chat", response_time=int((time.time() - t0) * 1000), response_length=0, exception=e)