-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_python.py
More file actions
executable file
·237 lines (195 loc) · 7.02 KB
/
bench_python.py
File metadata and controls
executable file
·237 lines (195 loc) · 7.02 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python3
# Copyright 2025 Jascha Wanger / Tarnover, LLC
# SPDX-License-Identifier: Apache-2.0
"""Per-operation microbenchmarks for the Python reference implementation.
Mirrors `rust/vectorpin/benches/perf.rs`:
* hash_text across text length ∈ {128, 1024, 8192}
* hash_vector across vector dim ∈ {384, 768, 1024, 3072}
* sign (pin) across vector dim, fixed 1024-char source
* verify_full across vector dim, fixed 1024-char source
* verify_sig signature-only verification at d=3072
Reports mean / p50 / p95 / p99 in microseconds plus throughput. We
intentionally avoid pytest-benchmark to keep the script standalone and
runnable without dev dependencies.
Usage:
python scripts/bench_python.py [--iters N] [--warmup N] [--json out.json]
"""
from __future__ import annotations
import argparse
import gc
import json
import platform
import statistics
import sys
import time
from dataclasses import asdict, dataclass
from pathlib import Path
import numpy as np
PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT / "src"))
from vectorpin import Signer, Verifier, hash_text, hash_vector
VECTOR_DIMS = [384, 768, 1024, 3072]
TEXT_LENS = [128, 1024, 8192]
TEXT_PATTERN = "The quick brown fox jumps over the lazy dog. "
def make_vector(d: int) -> np.ndarray:
return (np.arange(d, dtype=np.float32) * 0.001).astype(np.float32)
def make_text(n: int) -> str:
if n <= 0:
return ""
repeats = n // len(TEXT_PATTERN) + 1
return (TEXT_PATTERN * repeats)[:n]
@dataclass
class Result:
op: str
param: str
mean_us: float
p50_us: float
p95_us: float
p99_us: float
iters: int
throughput_per_s: float
def _summarize(samples_ns: list[int], op: str, param: str) -> Result:
samples_us = [s / 1000.0 for s in samples_ns]
samples_us.sort()
n = len(samples_us)
mean = statistics.fmean(samples_us)
p50 = samples_us[n // 2]
p95 = samples_us[min(n - 1, int(n * 0.95))]
p99 = samples_us[min(n - 1, int(n * 0.99))]
return Result(
op=op,
param=param,
mean_us=mean,
p50_us=p50,
p95_us=p95,
p99_us=p99,
iters=n,
throughput_per_s=(1_000_000.0 / mean) if mean > 0 else float("inf"),
)
def _time_callable(fn, iters: int, warmup: int) -> list[int]:
"""Run fn() iters+warmup times. Return per-call wall times in ns."""
for _ in range(warmup):
fn()
samples: list[int] = []
gc_was_enabled = gc.isenabled()
gc.disable()
try:
for _ in range(iters):
t0 = time.perf_counter_ns()
fn()
samples.append(time.perf_counter_ns() - t0)
finally:
if gc_was_enabled:
gc.enable()
return samples
def bench_hash_text(iters: int, warmup: int) -> list[Result]:
out = []
for n in TEXT_LENS:
text = make_text(n)
samples = _time_callable(lambda t=text: hash_text(t), iters, warmup)
out.append(_summarize(samples, "hash_text", f"len={n}"))
return out
def bench_hash_vector(iters: int, warmup: int) -> list[Result]:
out = []
for d in VECTOR_DIMS:
v = make_vector(d)
samples = _time_callable(lambda vv=v: hash_vector(vv, "f32"), iters, warmup)
out.append(_summarize(samples, "hash_vector", f"d={d}"))
return out
def bench_sign(iters: int, warmup: int) -> list[Result]:
signer = Signer.generate(key_id="bench")
text = make_text(1024)
out = []
for d in VECTOR_DIMS:
v = make_vector(d)
samples = _time_callable(
lambda vv=v, t=text: signer.pin(source=t, model="text-embedding-3-large", vector=vv),
iters,
warmup,
)
out.append(_summarize(samples, "sign", f"d={d}"))
return out
def bench_verify(iters: int, warmup: int) -> list[Result]:
signer = Signer.generate(key_id="bench")
verifier = Verifier(public_keys={signer.key_id: signer.public_key_bytes()})
text = make_text(1024)
out = []
for d in VECTOR_DIMS:
v = make_vector(d)
pin = signer.pin(source=text, model="text-embedding-3-large", vector=v)
samples = _time_callable(
lambda vv=v, t=text, p=pin: verifier.verify(p, source=t, vector=vv),
iters,
warmup,
)
out.append(_summarize(samples, "verify_full", f"d={d}"))
return out
def bench_verify_signature_only(iters: int, warmup: int) -> list[Result]:
signer = Signer.generate(key_id="bench")
verifier = Verifier(public_keys={signer.key_id: signer.public_key_bytes()})
text = make_text(1024)
v = make_vector(3072)
pin = signer.pin(source=text, model="text-embedding-3-large", vector=v)
# Verifier.verify with no source/vector args is the signature-only path.
samples = _time_callable(lambda p=pin: verifier.verify(p), iters, warmup)
return [_summarize(samples, "verify_signature_only", "d=3072")]
def _print_table(results: list[Result]) -> None:
cols = ("op", "param", "mean_us", "p50_us", "p95_us", "p99_us", "ops/sec")
widths = [16, 12, 12, 12, 12, 12, 14]
header = " ".join(c.ljust(w) for c, w in zip(cols, widths, strict=True))
print(header)
print("-" * len(header))
for r in results:
row = (
r.op,
r.param,
f"{r.mean_us:.2f}",
f"{r.p50_us:.2f}",
f"{r.p95_us:.2f}",
f"{r.p99_us:.2f}",
f"{r.throughput_per_s:,.0f}",
)
print(" ".join(str(c).ljust(w) for c, w in zip(row, widths, strict=True)))
def _platform_info() -> dict[str, str]:
return {
"python": sys.version.split()[0],
"platform": platform.platform(),
"processor": platform.processor() or "unknown",
"machine": platform.machine(),
}
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--iters", type=int, default=5000, help="measured iterations per cell")
parser.add_argument("--warmup", type=int, default=200, help="warmup iterations per cell")
parser.add_argument("--json", type=Path, default=None, help="write JSON results to this path")
args = parser.parse_args()
print("# VectorPin Python microbenchmarks")
info = _platform_info()
for k, v in info.items():
print(f"# {k}: {v}")
print(f"# iters={args.iters} warmup={args.warmup}")
print()
results: list[Result] = []
bench_fns = (
bench_hash_text,
bench_hash_vector,
bench_sign,
bench_verify,
bench_verify_signature_only,
)
for fn in bench_fns:
results.extend(fn(args.iters, args.warmup))
_print_table(results)
if args.json is not None:
args.json.parent.mkdir(parents=True, exist_ok=True)
payload = {
"platform": info,
"iters": args.iters,
"warmup": args.warmup,
"results": [asdict(r) for r in results],
}
args.json.write_text(json.dumps(payload, indent=2))
print(f"\nWrote {args.json}")
return 0
if __name__ == "__main__":
raise SystemExit(main())