askrobots / foxray Public

Foxray: a light error tracker for the Sentry SDKs. Runs inside Django (the admin is the UI) or on its own; SQLite or Postgres, nothing else. Not affiliated with Sentry.

0 downloads

foxray/bench/measure.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/foxray.git
HTTPS
https://gitstable.com/git/askrobots/foxray.git
SSH
ssh://git@gitstable.com:2222/askrobots/foxray.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 0.1.2: the first PyPI release (v0.1.1 is already tagged at 05cc354, before the PyPI metadata) 58507e3 4 weeks ago History
measure.py · 6.7 KB · 146 lines
1"""Measure the standalone server: idle memory and the time to ingest 1,000 events.
2
3 python bench/measure.py # SQLite in a temp dir
4 python bench/measure.py --db postgres://... # an existing, empty Postgres database
5
6Starts ``foxray serve`` in a subprocess, waits, reads its RSS with ``ps``,
7then posts SDK-shaped, gzip-compressed envelopes (about 4 KB of JSON each:
8an exception with in-app frames and source context, breadcrumbs, contexts,
9tags) over keep-alive connections. Rate limits and sampling are switched
10off so every event is fully stored."""
11
12from __future__ import annotations
13
14import argparse
15import gzip
16import http.client
17import json
18import os
19import socket
20import subprocess
21import sys
22import tempfile
23import threading
24import time
25import uuid
26from pathlib import Path
27
28ROOT = Path(__file__).resolve().parent.parent
29
30
31def rss_kb(pid: int) -> int:
32 return int(subprocess.check_output(["ps", "-o", "rss=", "-p", str(pid)]).strip())
33
34
35def free_port() -> int:
36 with socket.socket() as s:
37 s.bind(("127.0.0.1", 0))
38 return s.getsockname()[1]
39
40
41def make_envelope(i: int) -> bytes:
42 kind = i % 20 # 20 distinct issues
43 event_id = uuid.uuid4().hex
44 frames = [{"filename": f"lib/framework/{n}.py", "module": f"framework.mod{n}", "function": f"call{n}",
45 "lineno": 10 + n, "in_app": False, "context_line": f" return handler{n}(request)",
46 "pre_context": ["", "def call():", " request = get()"], "post_context": ["", ""]}
47 for n in range(8)]
48 frames += [{"filename": "shop/views.py", "module": "shop.views", "function": f"view_{kind}", "lineno": 40 + kind,
49 "in_app": True, "context_line": f" total = compute_{kind}(cart)",
50 "pre_context": ["def view(request):", " cart = load(request)", " # sum it"],
51 "post_context": [" return render(total)", ""]}]
52 event = {
53 "event_id": event_id, "timestamp": time.time(), "platform": "python", "level": "error",
54 "release": "shop@1.2.3", "environment": "production", "server_name": "web-1",
55 "transaction": f"/checkout/{kind}/",
56 "exception": {"values": [{"type": f"Error{kind}", "value": f"failed on order {i}",
57 "mechanism": {"type": "django", "handled": False},
58 "stacktrace": {"frames": frames}}]},
59 "breadcrumbs": {"values": [{"timestamp": time.time(), "category": "query", "message": f"SELECT {n} FROM t",
60 "level": "info"} for n in range(10)]},
61 "contexts": {"runtime": {"name": "CPython", "version": "3.10.1"}, "os": {"name": "Linux"},
62 "trace": {"trace_id": uuid.uuid4().hex, "span_id": uuid.uuid4().hex[:16]}},
63 "tags": {"tenant": f"t{i % 7}", "browser": "Firefox"},
64 "user": {"id": str(i % 50)},
65 "request": {"url": f"https://shop.example/checkout/{kind}/", "method": "POST",
66 "headers": {"User-Agent": "Mozilla/5.0 Firefox/120"}},
67 "sdk": {"name": "sentry.python.django", "version": "2.73.0"},
68 }
69 payload = json.dumps(event).encode()
70 body = (json.dumps({"event_id": event_id, "sent_at": "2026-10-24T00:00:00Z"}).encode() + b"\n"
71 + json.dumps({"type": "event", "length": len(payload)}).encode() + b"\n" + payload + b"\n")
72 return gzip.compress(body)
73
74
75def post_all(port: int, key: str, bodies: list[bytes]) -> None:
76 conn = http.client.HTTPConnection("127.0.0.1", port, timeout=30)
77 headers = {"X-Sentry-Auth": f"Sentry sentry_key={key}, sentry_version=7", "Content-Encoding": "gzip",
78 "Content-Type": "application/x-sentry-envelope"}
79 for body in bodies:
80 conn.request("POST", "/api/1/envelope/", body=body, headers=headers)
81 r = conn.getresponse()
82 r.read()
83 if r.status != 200:
84 raise SystemExit(f"unexpected status {r.status}")
85 conn.close()
86
87
88def main() -> int:
89 ap = argparse.ArgumentParser()
90 ap.add_argument("--db", help="database URL (default: a new SQLite file)")
91 ap.add_argument("--events", type=int, default=1000)
92 ap.add_argument("--clients", type=int, default=1, help="parallel keep-alive connections")
93 ap.add_argument("--idle", type=float, default=3.0, help="seconds to wait before reading idle RSS")
94 args = ap.parse_args()
95
96 tmp = tempfile.mkdtemp(prefix="foxray-bench-")
97 db = args.db or os.path.join(tmp, "foxray.db")
98 port = free_port()
99 env = {**os.environ, "PYTHONPATH": str(ROOT), "FOXRAY_RATE_LIMIT_PER_MINUTE": "0", "FOXRAY_SAMPLE_AFTER": "0"}
100 base = f"http://127.0.0.1:{port}"
101 dsn = subprocess.check_output([sys.executable, "-m", "foxray", "--db", db, "create-project", "bench",
102 "--base-url", base], env=env, text=True).strip()
103 key = dsn.split("//")[1].split("@")[0]
104 proc = subprocess.Popen([sys.executable, "-m", "foxray", "--db", db, "serve", "--port", str(port),
105 "--base-url", base], env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
106 try:
107 assert "listening" in proc.stdout.readline()
108 threading.Thread(target=lambda: [None for _ in proc.stdout], daemon=True).start() # drain logs
109 time.sleep(args.idle)
110 idle = rss_kb(proc.pid)
111 bodies = [make_envelope(i) for i in range(args.events)]
112 raw = sum(len(gzip.decompress(b)) for b in bodies) / len(bodies)
113 chunks = [bodies[i::args.clients] for i in range(args.clients)]
114 t0 = time.perf_counter()
115 threads = [threading.Thread(target=post_all, args=(port, key, c)) for c in chunks]
116 [t.start() for t in threads]
117 [t.join() for t in threads]
118 elapsed = time.perf_counter() - t0
119 time.sleep(1.0)
120 after = rss_kb(proc.pid)
121 sys.path.insert(0, str(ROOT))
122 from foxray.standalone.store import open_store
123 store = open_store(db)
124 issues, total = store.list_issues()
125 result = {
126 "database": "postgres" if db.startswith("postgres") else "sqlite",
127 "python": sys.version.split()[0],
128 "events": args.events, "clients": args.clients,
129 "avg_event_json_bytes": round(raw),
130 "idle_rss_mb": round(idle / 1024, 1),
131 "rss_after_ingest_mb": round(after / 1024, 1),
132 "ingest_seconds": round(elapsed, 2),
133 "events_per_second": round(args.events / elapsed),
134 "ms_per_event": round(1000 * elapsed / args.events, 2),
135 "issues": total, "stored_events": store.count_events(),
136 "db_bytes": store.db_size(),
137 }
138 print(json.dumps(result, indent=2))
139 finally:
140 proc.terminate()
141 proc.wait(timeout=10)
142 return 0
143
144
145if __name__ == "__main__":
146 raise SystemExit(main())