2222"""
2323
2424import json
25- import socket
2625import statistics
2726import sys
2827import threading
3837try :
3938 from rust_engine import ExecutionNode
4039except ImportError :
41- sys .exit ("ERROR: rust_engine not built. Run `poetry run maturin develop --manifest-path rust_engine/Cargo.toml`" )
40+ sys .exit (
41+ "ERROR: rust_engine not built. Run `poetry run maturin develop --manifest-path rust_engine/Cargo.toml`"
42+ )
4243
4344# ── constants ─────────────────────────────────────────────────────────────────
44- MID = 90_000.0
45+ MID = 90_000.0
4546TICK = 0.10
4647
47- N_WARMUP = 500
48+ N_WARMUP = 500
4849N_BENCH_NUMBA = 5_000
49- N_BENCH_HTTP = 500 # each call → up to 2 HTTP round-trips
50+ N_BENCH_HTTP = 500 # each call → up to 2 HTTP round-trips
5051
51- SUBMIT_BODY = json .dumps ({
52- "retCode" : 0 , "retMsg" : "OK" ,
53- "result" : {"orderId" : "BENCH-001" , "orderLinkId" : "link-001" },
54- }).encode ()
52+ SUBMIT_BODY = json .dumps (
53+ {
54+ "retCode" : 0 ,
55+ "retMsg" : "OK" ,
56+ "result" : {"orderId" : "BENCH-001" , "orderLinkId" : "link-001" },
57+ }
58+ ).encode ()
5559
5660
5761# ── mock HTTP server ──────────────────────────────────────────────────────────
5862
63+
5964class _Handler (BaseHTTPRequestHandler ):
6065 def do_POST (self ):
6166 # drain body
@@ -83,16 +88,17 @@ def _start_mock_server() -> tuple[ThreadingHTTPServer, str]:
8388
8489# ── stats / reporting ─────────────────────────────────────────────────────────
8590
91+
8692def _stats (samples_ns : list [int ]) -> dict :
8793 s = sorted (samples_ns )
8894 n = len (s )
8995 return {
90- "n" : n ,
91- "min" : s [0 ],
92- "p50" : s [n // 2 ],
93- "p95" : s [int (n * 0.95 )],
94- "p99" : s [int (n * 0.99 )],
95- "max" : s [- 1 ],
96+ "n" : n ,
97+ "min" : s [0 ],
98+ "p50" : s [n // 2 ],
99+ "p95" : s [int (n * 0.95 )],
100+ "p99" : s [int (n * 0.99 )],
101+ "max" : s [- 1 ],
96102 "mean" : int (statistics .mean (s )),
97103 }
98104
@@ -119,6 +125,7 @@ def fmt(ns: int) -> str:
119125
120126# ── warmup ────────────────────────────────────────────────────────────────────
121127
128+
122129def _warmup ():
123130 print (f"Warming up Numba ({ N_WARMUP } iters)..." , end = " " , flush = True )
124131 for _ in range (N_WARMUP ):
@@ -129,6 +136,7 @@ def _warmup():
129136
130137# ── segment 1 ─────────────────────────────────────────────────────────────────
131138
139+
132140def bench_segment1 ():
133141 samples = []
134142 for i in range (N_BENCH_NUMBA ):
@@ -142,6 +150,7 @@ def bench_segment1():
142150
143151# ── segment 2 ─────────────────────────────────────────────────────────────────
144152
153+
145154def bench_segment2 ():
146155 last_bid : float | None = None
147156 last_ask : float | None = None
@@ -174,6 +183,7 @@ def _should_update(b: float, a: float) -> bool:
174183
175184# ── segment 3 ─────────────────────────────────────────────────────────────────
176185
186+
177187def bench_segment3 (base_url : str ):
178188 node = ExecutionNode (
179189 api_key = "bench-key" ,
@@ -204,6 +214,7 @@ def bench_segment3(base_url: str):
204214
205215# ── full pipeline ─────────────────────────────────────────────────────────────
206216
217+
207218def bench_full_pipeline (base_url : str ):
208219 node = ExecutionNode (
209220 api_key = "bench-key" ,
@@ -219,7 +230,7 @@ def bench_full_pipeline(base_url: str):
219230
220231 samples = []
221232 for i in range (N_BENCH_HTTP ):
222- mid = MID + (i % 100 ) * TICK
233+ mid = MID + (i % 100 ) * TICK
223234 score = 0.82 if i % 2 == 0 else 0.18
224235 t0 = time .perf_counter_ns ()
225236 sig , conf = encode_signal (score )
@@ -241,7 +252,7 @@ def bench_full_pipeline(base_url: str):
241252 bench_segment1 ()
242253 bench_segment2 ()
243254
244- print (f "\n Starting localhost mock HTTP server..." , end = " " , flush = True )
255+ print ("\n Starting localhost mock HTTP server..." , end = " " , flush = True )
245256 server , base_url = _start_mock_server ()
246257 print (f"listening on { base_url } " )
247258
0 commit comments