Download faisi/src/sandbox/benchmark.py from ppJustin/micro: direct link, hf CLI and curl.
- Browser
- Download file 5.73 kB
-
https://huggingface.co/datasets/ppJustin/micro/resolve/main/faisi/src/sandbox/benchmark.py
- Command line
-
hf download hf://datasets/ppJustin/micro/faisi/src/sandbox/benchmark.py
-
curl -L -o benchmark.py https://huggingface.co/datasets/ppJustin/micro/resolve/main/faisi/src/sandbox/benchmark.py
5.73 kB
| """Run independently seeded tool calls in a 32-process pool.""" | |
| from collections import Counter | |
| from contextlib import contextmanager | |
| import multiprocessing | |
| from pathlib import Path | |
| import random | |
| from statistics import mean | |
| from time import perf_counter_ns | |
| from .query_tools import Sandbox, json_text | |
| from .snapshot import DATASETS | |
| METHODS = ( | |
| "get_initial_symptom_overview", "get_topology_neighbors", "query_entity_metrics", | |
| "query_call_edge_detail", "query_entity_events", "submit_diagnosis", | |
| ) | |
| _WORKER_SESSIONS = None | |
| def percentile(values, rate): | |
| if not values: | |
| return None | |
| numbers = sorted(values) | |
| pos = (len(numbers) - 1) * rate | |
| i = int(pos) | |
| other = numbers[min(i + 1, len(numbers) - 1)] | |
| return round(numbers[i] + (pos - i) * (other - numbers[i]), 6) | |
| def setup_worker(config, assigned, ready): | |
| global _WORKER_SESSIONS | |
| try: | |
| worker, selections = assigned.get(timeout=30) | |
| sandbox = Sandbox(*config) | |
| sessions, times = [], [] | |
| for dataset, case, seed in selections: | |
| started = perf_counter_ns() | |
| session = sandbox.open_session(dataset, case, seed) | |
| times.append((perf_counter_ns() - started) / 1e6) | |
| d = session._SandboxSession__snapshot | |
| mapper = session._SandboxSession__mapper | |
| ids = tuple(sorted(mapper._mask(name) for name in d.entities)) | |
| calls = tuple((mapper._mask(a), mapper._mask(b)) for a, b in sorted(d.edges) | |
| if a in d.entities and b in d.entities) | |
| event_ids = tuple(sorted(mapper._mask(n) for n in d.events if n in d.entities)) | |
| sessions.append((session, ids, calls, event_ids, dataset, mapper._evidence("overview"))) | |
| _WORKER_SESSIONS = (tuple(sessions), worker) | |
| ready.put((multiprocessing.current_process().pid, times, True)) | |
| except Exception: | |
| ready.put((multiprocessing.current_process().pid, [], False)) | |
| _WORKER_SESSIONS = None | |
| def invoke(number): | |
| cohort, worker = _WORKER_SESSIONS | |
| rng = random.Random(20261130 + number) | |
| method = METHODS[number % len(METHODS)] | |
| available = [x for x in cohort if x[2]] if method == "query_call_edge_detail" else cohort | |
| if not available: | |
| return (0.0, 0, True, worker, method, "") | |
| session, ids, edges, event_ids, dataset, overview_evidence = rng.choice(available) | |
| entity = rng.choice(ids) | |
| params = {} | |
| if method == "get_topology_neighbors": | |
| params = {"entity_id": entity, "hop": rng.choice((1, 2))} | |
| elif method == "query_entity_metrics": | |
| params = {"entity_id": entity} | |
| elif method == "query_call_edge_detail": | |
| params = dict(zip(("caller_id", "callee_id"), rng.choice(edges))) | |
| elif method == "query_entity_events": | |
| params = {"entity_id": rng.choice(event_ids or ids), "top_k": rng.choice((3, 5, 10))} | |
| elif method == "submit_diagnosis": | |
| path = [] | |
| if edges: | |
| entity, dst = rng.choice(edges) | |
| path.append([entity, dst]) | |
| params = { | |
| "root_entity_id": entity, "fault_category": "latency", | |
| "propagation_path": path, | |
| "key_evidence_ids": [overview_evidence], | |
| } | |
| start = perf_counter_ns() | |
| result = session.call(method, params) | |
| rendered = json_text(result) | |
| latency = (perf_counter_ns() - start) / 1e6 | |
| tokens = session._SandboxSession__budget.count(result) | |
| return latency, tokens, "error" in result or not rendered, worker, method, dataset | |
| def run_benchmark(config, workers=32, calls=10000): | |
| root = Path(config[0]) | |
| rng = random.Random(83103) | |
| selections = {} | |
| for dataset in DATASETS: | |
| available = sorted(path.name for path in (root / dataset).iterdir() if path.is_dir()) | |
| selections[dataset] = rng.sample(available, workers) | |
| context = multiprocessing.get_context("spawn") | |
| with managed_queue(context) as assigned: | |
| return _run(context, assigned, config, selections, rng, workers, calls) | |
| def managed_queue(context): | |
| queue = context.Queue() | |
| try: | |
| yield queue | |
| finally: | |
| queue.close() | |
| queue.join_thread() | |
| def _run(context, assigned, config, selections, rng, workers, calls): | |
| for worker in range(workers): | |
| assigned.put((worker, [(dataset, selections[dataset][worker], rng.randrange(2**62)) | |
| for dataset in DATASETS])) | |
| with managed_queue(context) as ready: | |
| with context.Pool(workers, initializer=setup_worker, initargs=(config, assigned, ready)) as pool: | |
| opened = [ready.get(timeout=180) for _ in range(workers)] | |
| if not all(ok for _, _, ok in opened): | |
| raise RuntimeError("benchmark worker initialization failed") | |
| results = pool.map(invoke, range(calls), chunksize=4) | |
| latencies, tokens, broken, participants, methods, datasets = zip(*results) | |
| opens = [elapsed for _, times, _ in opened for elapsed in times] | |
| return { | |
| "processes": workers, "active_processes": len(set(participants)), | |
| "calls": len(results), "error_count": sum(broken), "error_rate": sum(broken) / len(results), | |
| "mean_latency_ms": round(mean(latencies), 6), "p99_latency_ms": percentile(latencies, .99), | |
| "mean_response_tokens": round(mean(tokens), 6), "max_response_tokens": max(tokens), | |
| "tool_counts": dict(Counter(methods)), "dataset_counts": dict(Counter(datasets)), | |
| "sessions_opened": len(opens), "session_open_p50_ms": percentile(opens, .50), | |
| "session_open_p99_ms": percentile(opens, .99), | |
| "method": "Worker-resident seeded sessions; timer includes tool dispatch and compact JSON serialization", | |
| } | |