diff --git a/example/README.md b/example/README.md index a23dd41..6b4ad14 100644 --- a/example/README.md +++ b/example/README.md @@ -20,6 +20,39 @@ Run the suite (105 route-level integration tests, real stack, no mocks): make -f example/example.mk example-test ``` +## Load test (Locust, full integration) + +`example/locustfile.py` hits literally every route: all GETs, all POSTs, +and the file PUTs, including the `/routing/*` prefix and parameterized +routes (`/hello/{name}`, `/di/resolve/{name}`, `/tenancy/t/{tenant}/whoami`, +`/governance/keys/{id}/revoke`, ...). Start the server, then: + +```sh +pip install locust +locust -f example/locustfile.py --host http://127.0.0.1:8080 +``` + +Open the Locust web UI at `http://localhost:8089` (linked as `locust` in +the index matrix on `/`). Or headless: + +```sh +make -f example/example.mk example-locust +``` + +The make target first runs the reproducible coverage gate +(`example/check_locust_coverage.py`), which enumerates every +`add_feature` index route plus every router registration, applies +fill/prefix normalization, and fails on gaps. Expected output: + +``` +routes=112 urls=176 methods=['get', 'options', 'post', 'put'] +OK: 112/112 routes covered, methods ['get', 'options', 'post', 'put'] +``` + +`/ws/chat` is exercised three ways: POST echo, HTTP upgrade-header +probe, and a true websocket roundtrip via `ChatWebsocketUser` +(`websocket-client`: open `ws://host/ws/chat`, send/recv, close). + ## Framework status The framework is skeletal: `example/fw/` holds a compiling skeleton of the diff --git a/example/app/Index.cpp b/example/app/Index.cpp index 85cdd69..dc8abf5 100644 --- a/example/app/Index.cpp +++ b/example/app/Index.cpp @@ -46,6 +46,7 @@ const char* kFeatures[][2] = { {"workflow", "/workflow/release"}, {"redirect", "/redirect/here"}, {"health", "/health"}, + {"locust", "http://localhost:8089"}, }; } // namespace diff --git a/example/check_locust_coverage.py b/example/check_locust_coverage.py new file mode 100644 index 0000000..f95eb08 --- /dev/null +++ b/example/check_locust_coverage.py @@ -0,0 +1,86 @@ +"""Reproducible route-vs-locust coverage check. Fails CI on gaps. + +Usage: python3 example/check_locust_coverage.py +Exit 0 = full coverage. Exit 1 = missing routes printed. +""" +import re +import subprocess +import sys +from pathlib import Path + +HERE = Path(__file__).resolve().parent +APP = HERE / "app" +LOCUST = HERE / "locustfile.py" + +FILLS = {"name": "locust", "id": "1", "status": "404", "tenant": "acme"} + + +def fill(route: str) -> str: + out = route + for k, v in FILLS.items(): + out = out.replace("{" + k + "}", v) + return out + + +def collect_routes() -> set[str]: + routes: set[str] = set() + # 1. Index features (authoritative user-facing list) + out = subprocess.run( + ["grep", "-rhoE", r'add_feature\("[^"]+", "/[^"]+"\)', str(APP)], + capture_output=True, text=True, check=True, + ).stdout + for m in re.findall(r'"/[^"]+"\)', out): + routes.add(m.strip('")')) + # 2. Direct router registrations + out = subprocess.run( + ["grep", "-rhoE", r'\.(get|post|put|delete|patch|options|head)\("/[^"]+"', str(APP)], + capture_output=True, text=True, check=True, + ).stdout + for m in re.findall(r'"/[^"]+"', out): + routes.add(m.strip('"')) + # 3. Prefix-mounted equivalents (see RoutingDemo.cpp / App.cpp) + routes |= {"/views/greet", "/routers/nested/deep", + "/app1/app1/app1/whoami"} + return routes + + +def collect_locust_urls() -> tuple[set[str], str]: + src = LOCUST.read_text() + urls = set(re.findall(r'"(/[^\"]*)"', src)) + methods = set(re.findall(r"self\.client\.(get|post|put|options|delete|patch|head)\(", src)) + return urls, methods + + +def main() -> int: + routes = collect_routes() + urls, methods = collect_locust_urls() + missing: list[str] = [] + for r in sorted(routes): + c = fill(r) + ok = ( + c in urls + or "/routing" + c in urls + or (r == "/static/{name}" and "/static/app.css" in urls) + or (r == "/embedded/{name}" and "/embedded/test.txt" in urls) + or (r == "/di/resolve/{name}" and "/di/resolve/testsvc" in urls) + or (r == "/views/greet" and ("/greet" in urls or "/routing/views/greet" in urls)) + or (r == "/routers/nested/deep" and ("/deep" in urls or "/routing/routers/nested/deep" in urls)) + ) + if not ok: + missing.append(f"{r} -> {c}") + required_methods = {"get", "post", "put", "options"} + missing_methods = required_methods - methods + print(f"routes={len(routes)} urls={len(urls)} methods={sorted(methods)}") + if missing_methods: + print("MISSING METHODS:", sorted(missing_methods)) + for m in missing: + print("MISS", m) + if missing or missing_methods: + print(f"FAIL: {len(missing)} missing routes, {len(missing_methods)} missing methods") + return 1 + print(f"OK: {len(routes)}/{len(routes)} routes covered, methods {sorted(methods)}") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/example/example.mk b/example/example.mk index ac348fe..58f6688 100644 --- a/example/example.mk +++ b/example/example.mk @@ -4,6 +4,7 @@ # make -f example/example.mk example # build lib + server # make -f example/example.mk example-test # build + run example suite # make -f example/example.mk example-clean # remove build/example +# make -f example/example.mk example-locust # headless locust run # Portable make: variables plus sh loops only, no GNU-only functions. CXX ?= g++ @@ -70,7 +71,7 @@ EXAMPLE_ASSETS = \ $(EXAMPLE_DIR)/assets/stats.js EXAMPLE_INC = -I$(EXAMPLE_DIR) -I$(EXAMPLE_BUILD) -.PHONY: example example-assets example-test example-clean +.PHONY: example example-assets example-test example-clean example-locust # Asset embedding is part of the build: example/assets (Chart.js included) # becomes generated C++ under build/example/gen via the MoloApp embedder. @@ -94,8 +95,10 @@ example: example-assets $(CXX) $(EXAMPLE_CXXFLAGS) -c $(EXAMPLE_GEN)/assets.cpp \ -o $(EXAMPLE_BUILD)/obj/gen_assets.o ar rcs $(EXAMPLE_BUILD)/libexample.a $(EXAMPLE_BUILD)/obj/*.o - $(CXX) $(EXAMPLE_CXXFLAGS) -I$(EXAMPLE_DIR) $(EXAMPLE_DIR)/main.cpp \ - $(EXAMPLE_BUILD)/libexample.a -o $(EXAMPLE_BUILD)/living-server + $(MAKE) -f src/supervisor/supervisor.mk build/supervisor/libsupervisor.a + $(CXX) $(EXAMPLE_CXXFLAGS) -I$(EXAMPLE_DIR) -Isrc $(EXAMPLE_DIR)/main.cpp \ + $(EXAMPLE_BUILD)/libexample.a build/supervisor/libsupervisor.a \ + -o $(EXAMPLE_BUILD)/living-server example-test: example rm -rf $(EXAMPLE_BUILD)/tests @@ -116,6 +119,10 @@ example-test: example echo "example suite: $$pass passed, $$fail failed"; \ test $$fail -eq 0 +example-locust: + python3 $(EXAMPLE_DIR)/check_locust_coverage.py + locust -f $(EXAMPLE_DIR)/locustfile.py --host $${LOCUST_HOST:-http://127.0.0.1:8080} --headless -u $${LOCUST_USERS:-10} -r $${LOCUST_RATE:-2} -t $${LOCUST_TIME:-30s} + example-clean: rm -rf $(EXAMPLE_BUILD) diff --git a/example/locustfile.py b/example/locustfile.py new file mode 100644 index 0000000..e75138d --- /dev/null +++ b/example/locustfile.py @@ -0,0 +1,223 @@ +"""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", +] + +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) diff --git a/example/main.cpp b/example/main.cpp index bfc9746..9660b83 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -1,16 +1,36 @@ // Living example server entry: serves the demo app on loopback. -// Usage: living-server [port] (default 8080) +// Usage: living-server [port] [--no-locust] (default port 8080) +// The Locust load-test UI (web UI on 8089) runs as a process worker under +// the real molo::supervisor::Supervisor: started with the server, +// restarted on crash, stopped with it. +#include #include +#include #include #include #include #include "app/App.hpp" #include "fw/Server.hpp" +#include "supervisor/common/clock.hpp" +#include "supervisor/common/observer.hpp" +#include "supervisor/supervisor/process_work.hpp" +#include "supervisor/supervisor/supervisor.hpp" + +namespace { +std::atomic g_stop{false}; +void on_signal(int) { g_stop = true; } +} // namespace int main(int argc, char** argv) { int port = 8080; + bool with_locust = true; + for (int i = 2; i < argc; ++i) { + if (std::string(argv[i]) == "--no-locust") with_locust = false; + } + std::signal(SIGINT, on_signal); + std::signal(SIGTERM, on_signal); if (argc > 1) { try { port = std::stoi(argv[1]); @@ -28,9 +48,25 @@ int main(int argc, char** argv) { return 1; } std::cout << "living example on http://127.0.0.1:" << server.port() << "\n"; + molo::common::SystemClock clock; + molo::supervisor::Supervisor supervisor( + clock, std::make_shared()); + if (with_locust) { + // Paths are cwd-relative like Services::static_root (run from repo root). + molo::supervisor::GroupSpec group; + group.name = "locust"; + supervisor.add_group( + group, molo::supervisor::make_process_work( + {{"locust", "-f", "example/locustfile.py", "--host", + "http://127.0.0.1:" + std::to_string(server.port()), + "--web-host", "127.0.0.1", "--web-port", "8089"}})); + supervisor.start_all(); + std::cout << "locust (supervised) on http://127.0.0.1:8089\n"; + } std::cout << "press Ctrl-C to stop\n"; - while (server.running()) { + while (server.running() && !g_stop) { std::this_thread::sleep_for(std::chrono::milliseconds(200)); } + supervisor.shutdown(0); return 0; } diff --git a/tests/example/matrix_test.cpp b/tests/example/matrix_test.cpp index db5ea8c..c5faf1d 100644 --- a/tests/example/matrix_test.cpp +++ b/tests/example/matrix_test.cpp @@ -15,6 +15,7 @@ int main() { // The template escapes '&' in hrefs. for (std::size_t a; (a = href.find("&")) != std::string::npos;) href.replace(a, 5, "&"); + if (href.rfind("http", 0) == 0) continue; // external (locust UI), not a route auto res = f.client->get(href); // /auth/me demands credentials by design; every other row must be 2xx/3xx. bool ok = res.status < 400 || (href == "/auth/me" && res.status == 401);