|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import gc |
| 4 | +import json |
| 5 | +import random |
| 6 | +import statistics |
| 7 | +import time |
| 8 | +from dataclasses import dataclass |
| 9 | +from typing import Any, Dict, List |
| 10 | + |
| 11 | +from jsonschema_path import SchemaPath |
| 12 | + |
| 13 | +from openapi_core.templating.paths.finders import APICallPathFinder |
| 14 | + |
| 15 | + |
| 16 | +@dataclass(frozen=True) |
| 17 | +class Result: |
| 18 | + paths: int |
| 19 | + templates_ratio: float |
| 20 | + lookups: int |
| 21 | + repeats: int |
| 22 | + warmup: int |
| 23 | + seconds: List[float] |
| 24 | + |
| 25 | + def as_dict(self) -> Dict[str, Any]: |
| 26 | + return { |
| 27 | + "paths": self.paths, |
| 28 | + "templates_ratio": self.templates_ratio, |
| 29 | + "lookups": self.lookups, |
| 30 | + "repeats": self.repeats, |
| 31 | + "warmup": self.warmup, |
| 32 | + "seconds": self.seconds, |
| 33 | + "median_s": statistics.median(self.seconds), |
| 34 | + "mean_s": statistics.mean(self.seconds), |
| 35 | + "stdev_s": statistics.pstdev(self.seconds), |
| 36 | + "ops_per_sec_median": self.lookups / statistics.median(self.seconds), |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | +def build_spec(paths: int, templates_ratio: float) -> SchemaPath: |
| 41 | + # Mix of exact and templated paths. |
| 42 | + # Keep it minimal so we measure finder cost, not schema complexity. |
| 43 | + tmpl = int(paths * templates_ratio) |
| 44 | + exact = paths - tmpl |
| 45 | + |
| 46 | + paths_obj: Dict[str, Any] = {} |
| 47 | + |
| 48 | + # Exact paths (fast case) |
| 49 | + for i in range(exact): |
| 50 | + p = f"/resource/{i}/sub" |
| 51 | + paths_obj[p] = {"get": {"responses": {"200": {"description": "ok"}}}} |
| 52 | + |
| 53 | + # Template paths (slow case) |
| 54 | + for i in range(tmpl): |
| 55 | + p = f"/resource/{i}" + "/{item_id}/sub/{sub_id}" |
| 56 | + paths_obj[p] = {"get": {"responses": {"200": {"description": "ok"}}}} |
| 57 | + |
| 58 | + spec_dict = { |
| 59 | + "openapi": "3.0.0", |
| 60 | + "info": {"title": "bench", "version": "0"}, |
| 61 | + "servers": [{"url": "http://example.com"}], |
| 62 | + "paths": paths_obj, |
| 63 | + } |
| 64 | + return SchemaPath.from_dict(spec_dict) |
| 65 | + |
| 66 | + |
| 67 | +def build_urls(paths: int, templates_ratio: float, lookups: int, seed: int) -> List[str]: |
| 68 | + rnd = random.Random(seed) |
| 69 | + tmpl = int(paths * templates_ratio) |
| 70 | + exact = paths - tmpl |
| 71 | + |
| 72 | + urls: List[str] = [] |
| 73 | + for _ in range(lookups): |
| 74 | + # 50/50 choose from each population, weighted by how many exist |
| 75 | + if tmpl > 0 and (exact == 0 or rnd.random() < (tmpl / paths)): |
| 76 | + i = rnd.randrange(tmpl) # matches template bucket |
| 77 | + item_id = rnd.randrange(1_000_000) |
| 78 | + sub_id = rnd.randrange(1_000_000) |
| 79 | + urls.append(f"http://example.com/resource/{i}/{item_id}/sub/{sub_id}") |
| 80 | + else: |
| 81 | + i = rnd.randrange(exact) if exact > 0 else 0 |
| 82 | + urls.append(f"http://example.com/resource/{i}/sub") |
| 83 | + return urls |
| 84 | + |
| 85 | + |
| 86 | +def run_once(finder: APICallPathFinder, urls: List[str]) -> float: |
| 87 | + t0 = time.perf_counter() |
| 88 | + for u in urls: |
| 89 | + finder.find("get", u) |
| 90 | + return time.perf_counter() - t0 |
| 91 | + |
| 92 | + |
| 93 | +def main() -> None: |
| 94 | + ap = argparse.ArgumentParser() |
| 95 | + ap.add_argument("--paths", type=int, default=2000) |
| 96 | + ap.add_argument("--templates-ratio", type=float, default=0.6) |
| 97 | + ap.add_argument("--lookups", type=int, default=100_000) |
| 98 | + ap.add_argument("--repeats", type=int, default=7) |
| 99 | + ap.add_argument("--warmup", type=int, default=2) |
| 100 | + ap.add_argument("--seed", type=int, default=1) |
| 101 | + ap.add_argument("--output", type=str, default="") |
| 102 | + ap.add_argument("--no-gc", action="store_true") |
| 103 | + args = ap.parse_args() |
| 104 | + |
| 105 | + spec = build_spec(args.paths, args.templates_ratio) |
| 106 | + finder = APICallPathFinder(spec) |
| 107 | + |
| 108 | + urls = build_urls(args.paths, args.templates_ratio, args.lookups, args.seed) |
| 109 | + |
| 110 | + if args.no_gc: |
| 111 | + gc.disable() |
| 112 | + |
| 113 | + # Warmup (JIT-less, but warms caches, alloc patterns, etc.) |
| 114 | + for _ in range(args.warmup): |
| 115 | + run_once(finder, urls) |
| 116 | + |
| 117 | + seconds: List[float] = [] |
| 118 | + for _ in range(args.repeats): |
| 119 | + seconds.append(run_once(finder, urls)) |
| 120 | + |
| 121 | + if args.no_gc: |
| 122 | + gc.enable() |
| 123 | + |
| 124 | + result = Result( |
| 125 | + paths=args.paths, |
| 126 | + templates_ratio=args.templates_ratio, |
| 127 | + lookups=args.lookups, |
| 128 | + repeats=args.repeats, |
| 129 | + warmup=args.warmup, |
| 130 | + seconds=seconds, |
| 131 | + ) |
| 132 | + |
| 133 | + payload = result.as_dict() |
| 134 | + print(json.dumps(payload, indent=2, sort_keys=True)) |
| 135 | + |
| 136 | + if args.output: |
| 137 | + with open(args.output, "w", encoding="utf-8") as f: |
| 138 | + json.dump(payload, f, indent=2, sort_keys=True) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + main() |
0 commit comments