This repository was archived by the owner on Jul 18, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathbenchmark.py
More file actions
267 lines (227 loc) · 8.22 KB
/
Copy pathbenchmark.py
File metadata and controls
267 lines (227 loc) · 8.22 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""PoW benchmark for the Python reference. Sweeps n_zeros, records raw
per-run timings, and optionally dumps the result in the same JSON schema
as the Rust `pow-bench` so both feed the analysis notebook.
Headline metric: max N whose median solve time fits the --target budget."""
from __future__ import annotations
import argparse
import hashlib
import json
import os
import platform
import secrets
import statistics
import sys
import time
from dataclasses import dataclass
from pathlib import Path
from pow import solve
@dataclass
class NSummary:
n_zeros: int
runs: int
attempts: list[int]
elapsed: list[float]
@property
def mean_elapsed(self) -> float:
return statistics.mean(self.elapsed)
@property
def median_elapsed(self) -> float:
return percentile(self.elapsed, 0.50)
@property
def p95_elapsed(self) -> float:
return percentile(self.elapsed, 0.95)
@property
def p99_elapsed(self) -> float:
return percentile(self.elapsed, 0.99)
@property
def min_elapsed(self) -> float:
return min(self.elapsed) if self.elapsed else 0.0
@property
def max_elapsed(self) -> float:
return max(self.elapsed) if self.elapsed else 0.0
@property
def stddev_elapsed(self) -> float:
return statistics.stdev(self.elapsed) if len(self.elapsed) > 1 else 0.0
@property
def mean_attempts(self) -> float:
return statistics.mean(self.attempts) if self.attempts else 0.0
@property
def effective_hps(self) -> float:
total_attempts = sum(self.attempts)
total_elapsed = sum(self.elapsed)
return total_attempts / total_elapsed if total_elapsed > 0 else 0.0
def percentile(xs: list[float], p: float) -> float:
"""Nearest-rank percentile — matches the Rust benchmark implementation."""
if not xs:
return 0.0
s = sorted(xs)
rank = max(1, int(-(-p * len(s) // 1))) # ceil(p * n), at least 1
return s[min(rank, len(s)) - 1]
def measure_hashrate(duration: float = 2.0) -> float:
"""Single-thread sha256 throughput on an unsolvable task."""
token = secrets.token_bytes(16)
attempts = 0
t0 = time.perf_counter()
deadline = t0 + duration
nonce = 0
while time.perf_counter() < deadline:
for _ in range(10_000):
hashlib.sha256(token + str(nonce).encode("ascii")).digest()
nonce += 1
attempts += 10_000
elapsed = time.perf_counter() - t0
return attempts / elapsed
def run_trials(n_zeros: int, runs: int, time_budget: float) -> NSummary:
attempts: list[int] = []
elapsed: list[float] = []
spent = 0.0
for _ in range(runs):
token = secrets.token_bytes(16)
result = solve(token, n_zeros, start_nonce=secrets.randbits(32))
attempts.append(result.attempts)
elapsed.append(result.elapsed)
spent += result.elapsed
if spent > time_budget:
break
return NSummary(n_zeros=n_zeros, runs=len(elapsed), attempts=attempts, elapsed=elapsed)
def fmt_int(value: float) -> str:
return f"{int(value):,}".replace(",", " ")
def print_env() -> None:
print("=" * 80)
print("ENVIRONMENT")
print("=" * 80)
print(f" Python : {platform.python_version()} ({sys.implementation.name})")
print(f" Platform : {platform.platform()}")
print(f" Processor : {platform.processor() or platform.machine()}")
print(f" CPU count : {os.cpu_count()} (Python baseline uses 1)")
print()
def predict_n(hps: float, target: float) -> int:
n = 0
while (16 ** (n + 1)) / hps < target:
n += 1
if n >= 16:
break
return n
def write_json(
path: Path,
args: argparse.Namespace,
hashrate_hps: float,
summaries: list[NSummary],
best_n: int | None,
) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
doc = {
"schema_version": 2,
"timestamp_unix": int(time.time()),
"env": {
"os": platform.system().lower(),
"arch": platform.machine(),
"rayon_threads": 1,
"pow_version": "0.1.0",
"python_version": platform.python_version(),
"python_impl": sys.implementation.name,
},
"config": {
"backend": "Python",
"target_secs": float(args.target),
"runs_per_n": int(args.runs),
"threads": 1,
"start": int(args.start),
"max": int(args.max),
"calibrate_seconds": float(args.calibrate_seconds),
},
"calibration": {
"single_hps": int(hashrate_hps),
"parallel_hps": int(hashrate_hps),
},
"results": [
{
"n_zeros": s.n_zeros,
"runs": s.runs,
"elapsed_secs": s.elapsed,
"attempts": s.attempts,
"stats": {
"mean_elapsed": s.mean_elapsed,
"median_elapsed": s.median_elapsed,
"p95_elapsed": s.p95_elapsed,
"p99_elapsed": s.p99_elapsed,
"min_elapsed": s.min_elapsed,
"max_elapsed": s.max_elapsed,
"stddev_elapsed": s.stddev_elapsed,
"mean_attempts": s.mean_attempts,
"effective_hps": s.effective_hps,
},
}
for s in summaries
],
"max_n_under_target": best_n,
}
path.write_text(json.dumps(doc, indent=2) + "\n")
def main() -> None:
parser = argparse.ArgumentParser(description="PoW benchmark (Python baseline)")
parser.add_argument("--start", type=int, default=4, help="starting n_zeros")
parser.add_argument("--max", type=int, default=7, help="upper bound on n_zeros")
parser.add_argument("--runs", type=int, default=30, help="trials per n_zeros")
parser.add_argument(
"--target", type=float, default=60.0, help="target budget per solve, seconds"
)
parser.add_argument(
"--calibrate-seconds",
type=float,
default=2.0,
help="hashrate calibration duration in seconds",
)
parser.add_argument("--json", type=Path, default=None, help="dump structured results here")
args = parser.parse_args()
print_env()
print("=" * 80)
print(f"CALIBRATION ({args.calibrate_seconds:.1f}s of empty sha256 loop)")
print("=" * 80)
hps = measure_hashrate(args.calibrate_seconds)
print(f" python single-thread H/s : {fmt_int(hps)}")
predicted_n = predict_n(hps, args.target)
print(f" Predicted N : ~{predicted_n} hex zeros should fit in {args.target:.0f}s")
print()
print("=" * 80)
print(f"BENCHMARK (Python, target = {args.target:.0f}s, runs per N = {args.runs})")
print("=" * 80)
header = (
f"{'N':>3} | {'runs':>4} | {'mean t,s':>9} | {'median t,s':>10} | "
f"{'p95 t,s':>8} | {'max t,s':>8} | {'sigma':>8} | {'H/s':>14}"
)
print(header)
print("-" * len(header))
best_n: int | None = None
per_n_budget = max(args.target * 3.0, 30.0)
summaries: list[NSummary] = []
for n_zeros in range(args.start, args.max + 1):
s = run_trials(n_zeros, args.runs, per_n_budget)
summaries.append(s)
print(
f"{s.n_zeros:>3} | {s.runs:>4} | {s.mean_elapsed:>9.3f} | "
f"{s.median_elapsed:>10.3f} | {s.p95_elapsed:>8.3f} | "
f"{s.max_elapsed:>8.3f} | {s.stddev_elapsed:>8.3f} | "
f"{fmt_int(s.effective_hps):>14}",
flush=True,
)
if s.median_elapsed <= args.target:
best_n = s.n_zeros
if s.mean_elapsed > args.target * 2:
print(f" (stop: mean elapsed {s.mean_elapsed:.1f}s > 2 x target)")
break
print()
print("=" * 80)
print("RESULT")
print("=" * 80)
if best_n is None:
print(f" Even N={args.start} exceeds {args.target:.0f}s median.")
else:
print(
f" Max N such that median solve time <= {args.target:.0f}s: "
f"N = {best_n} hex zeros (~{fmt_int(16**best_n)} expected attempts)"
)
if args.json is not None:
write_json(args.json, args, hps, summaries, best_n)
print(f" JSON written to {args.json}")
if __name__ == "__main__":
main()