-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbench_encode_depth.py
More file actions
76 lines (59 loc) · 2.16 KB
/
Copy pathbench_encode_depth.py
File metadata and controls
76 lines (59 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
"""Local deep-encode benchmark (not used by CI).
Usage:
python scripts/bench_encode_depth.py
python scripts/bench_encode_depth.py --runs 5 --depths 2000 5000 12000
"""
from __future__ import annotations
import argparse
import statistics
import sys
import time
import typing as t
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))
from qs_codec import encode
from qs_codec.models.encode_options import EncodeOptions
def make_nested(depth: int) -> t.Dict[str, t.Any]:
data: t.Dict[str, t.Any] = {"leaf": "x"}
for _ in range(depth):
data = {"a": data}
return data
def run_once(depth: int) -> float:
data = make_nested(depth)
start = time.perf_counter()
result = encode(data, options=EncodeOptions(encode=False))
elapsed = time.perf_counter() - start
if not result.endswith("=x"):
raise RuntimeError(f"unexpected encoded output for depth={depth}")
return elapsed
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Benchmark deep encode performance.")
parser.add_argument("--runs", type=int, default=3, help="timed runs per depth")
parser.add_argument("--warmups", type=int, default=1, help="warm-up runs per depth")
parser.add_argument(
"--depths",
type=int,
nargs="+",
default=[2000, 5000, 12000],
help="depth values to benchmark",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
if args.runs <= 0:
raise ValueError("--runs must be > 0")
if args.warmups < 0:
raise ValueError("--warmups must be >= 0")
if not args.depths:
raise ValueError("--depths must not be empty")
print(f"python={sys.version.split()[0]} runs={args.runs} warmups={args.warmups}")
for depth in args.depths:
for _ in range(args.warmups):
run_once(depth)
times = [run_once(depth) for _ in range(args.runs)]
median = statistics.median(times)
print(f"depth={depth} median={median:.6f}s " f"runs=[{', '.join(f'{t_:.6f}' for t_ in times)}]")
if __name__ == "__main__":
main()