forked from madacol/btcrecover
-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathgenerate_benchmark_docs.py
More file actions
289 lines (233 loc) · 10.4 KB
/
Copy pathgenerate_benchmark_docs.py
File metadata and controls
289 lines (233 loc) · 10.4 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python3
# generate_benchmark_docs.py -- Generate benchmark documentation from JSON results
# Copyright (C) 2024 Stephen Rothery
#
# This file is part of btcrecover.
#
# btcrecover is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# btcrecover is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with btcrecover. If not, see <http://www.gnu.org/licenses/>.
"""Generate the Benchmarks.md documentation page from JSON result files.
Reads all JSON files in benchmark-results/ and generates a formatted
markdown page with tables comparing results across different systems.
Usage:
python generate_benchmark_docs.py
"""
import glob
import json
import os
import sys
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
RESULTS_DIR = os.path.join(SCRIPT_DIR, "benchmark-results")
TEMPLATE_FILE = os.path.join(SCRIPT_DIR, "docs", "Benchmarks.md.template")
DOCS_FILE = os.path.join(SCRIPT_DIR, "docs", "Benchmarks.md")
# Markers in the template where generated content is inserted
START_MARKER = "<!-- BENCHMARK_RESULTS_START -->"
END_MARKER = "<!-- BENCHMARK_RESULTS_END -->"
def load_all_results():
"""Load all benchmark result JSON files."""
results = []
pattern = os.path.join(RESULTS_DIR, "benchmark_*.json")
for filepath in sorted(glob.glob(pattern)):
try:
with open(filepath, "r") as f:
data = json.load(f)
data["_filename"] = os.path.basename(filepath)
results.append(data)
except (json.JSONDecodeError, IOError) as e:
print(f"Warning: Could not load {filepath}: {e}", file=sys.stderr)
return results
def format_rate(rate):
"""Format a rate value for display in markdown."""
if rate is None or rate == 0:
return "—"
if rate >= 1_000_000:
return f"{rate / 1_000_000:.2f} Mp/s"
elif rate >= 1_000:
return f"{rate / 1_000:.2f} Kp/s"
else:
return f"{rate:.2f} p/s"
def generate_markdown(all_results):
"""Generate the benchmark results markdown content."""
if not all_results:
return (
'!!! note "No benchmark results yet"\n'
" No benchmark result files have have been found in the `benchmark-results/` directory.\n"
" Run `python benchmark.py` to generate your first benchmark, or see the\n"
" contributing section above.\n"
)
lines = []
# ── Collect all test labels and organize by category ──
categories = {}
difficulties = {} # (cat, base_label, mode) -> wallet_difficulty string
rate_lookup = {} # (system_idx, mode, base_label) -> rate
for i, result in enumerate(all_results):
for bench in result.get("benchmarks", []):
cat = bench.get("category", "other")
label = bench.get("label", "Unknown")
mode = bench.get("mode", "cpu")
# Clean label
base_label = label
for suffix in (" (CPU)", " (GPU)", " (OPENCL)"):
base_label = base_label.replace(suffix, "")
if cat not in categories:
categories[cat] = {}
if (base_label, mode) not in categories[cat]:
categories[cat][(base_label, mode)] = {}
# Capture rate
rate = bench.get("passwords_per_second", 0)
rate_lookup[(i, mode, base_label)] = rate
# Capture difficulty
difficulty = bench.get("wallet_difficulty", "")
if difficulty:
difficulties[(cat, base_label, mode)] = difficulty
# ── Build tables for each category (seed before password) ──
for cat in sorted(categories.keys(), reverse=True):
lines.append(f"### {cat.title()} Benchmarks\n")
# Prepare data for _generate_table: (label, mode) -> {system_idx: rate}
category_data = {}
for (base_label, mode), _ in categories[cat].items():
category_data[(base_label, mode)] = {}
for i in range(len(all_results)):
if (i, mode, base_label) in rate_lookup:
category_data[(base_label, mode)][i] = rate_lookup[(i, mode, base_label)]
# Prepare difficulties for this category
cat_difficulties = {}
for (c, label, mode), diff in difficulties.items():
if c == cat:
cat_difficulties[(label, mode)] = diff
_generate_table(lines, category_data, all_results, difficulties=cat_difficulties)
lines.append("")
# ── System Overview ──
lines.append("### Systems Tested\n")
lines.append("| # | CPU | Cores (Phys/Logical) | GPU | OpenCL Device | OS | Date |")
lines.append("|---|-----|----------------------|-----|---------------|----|----- |")
for i, result in enumerate(all_results, 1):
sys_info = result.get("system_info", {})
cpu = sys_info.get("cpu_model", "Unknown")
cpu = cpu.replace("Intel(R) Core(TM) ", "").replace("AMD ", "")
cpu = cpu.replace(" Processor", "").replace(" CPU", "")
phys = sys_info.get("cpu_cores_physical", "?")
logical = sys_info.get("cpu_cores_logical", "?")
gpu_info = sys_info.get("gpu", [])
gpu = gpu_info[0].get("name", "None") if gpu_info else "None"
opencl_devs = sys_info.get("opencl_devices", [])
if isinstance(opencl_devs, list) and opencl_devs:
dev = opencl_devs[0]
opencl = f"{dev.get('name', '?')} ({dev.get('global_memory_mb', '?')} MB)"
else:
opencl = "None"
# Note: using os_release if available, else os
if not sys_info.get('os_release'):
os_name = f"{sys_info.get('os', '?')}"
else:
os_name = f"{sys_info.get('os', '?')} {sys_info.get('os_release', '')}"
timestamp = result.get("metadata", {}).get("timestamp", "?")
date = timestamp[:10] if len(timestamp) >= 10 else timestamp
lines.append(f"| {i} | {cpu} | {phys}/{logical} | {gpu} | {opencl} | {os_name} | {date} |")
lines.append("")
return "\n".join(lines)
def _get_system_hw_label(result, index, mode):
"""Build a short label showing system number and relevant hardware.
For CPU mode, shows the CPU name.
For GPU/OpenCL modes, shows the GPU name.
"""
sys_info = result.get("system_info", {})
cpu = sys_info.get("cpu_model", "Unknown")
cpu = cpu.replace("Intel(R) Core(TM) ", "").replace("AMD ", "")
cpu = cpu.replace(" Processor", "").replace(" CPU", "")
gpu_info = sys_info.get("gpu", [])
gpu = gpu_info[0].get("name", "None") if gpu_info else "None"
if mode in ("gpu", "opencl"):
if gpu != "None":
return f"#{index + 1} {gpu}"
# Fall back to CPU if no GPU info available
return f"#{index + 1} {cpu}"
phys = sys_info.get("cpu_cores_physical", "")
logical = sys_info.get("cpu_cores_logical", "")
if phys and logical:
return f"#{index + 1} {cpu} ({phys}C/{logical}T)"
return f"#{index + 1} {cpu}"
def _generate_table(lines, category_data, all_results, difficulties=None):
"""Generate a markdown table for a category of benchmarks."""
if not category_data:
return
# Collect unique base labels (without mode) in sorted order
base_labels = sorted(set(label for label, mode in category_data.keys()))
# Collect all (system_idx, mode) row keys that have data
row_keys = set()
for (label, mode), system_rates in category_data.items():
for sys_idx in system_rates:
row_keys.add((sys_idx, mode))
# Sort by system index first, then mode
mode_order = {"cpu": 0, "gpu": 1, "opencl": 2}
sorted_row_keys = sorted(row_keys, key=lambda x: (x[0], mode_order.get(x[1], 9)))
# Header — test labels as columns, with difficulty if available
header = "| System | Mode |"
separator = "|--------|------|"
for label in base_labels:
display_label = label
if difficulties:
# Find any difficulty for this label (pick from any mode)
for mode_key in ("cpu", "gpu", "opencl"):
diff = difficulties.get((label, mode_key), "")
if diff:
display_label = f"{label} - {diff}"
break
header += f" {display_label} |"
separator += "--------|"
lines.append(header)
lines.append(separator)
# Rows — one per (system, mode) combination
for sys_idx, mode in sorted_row_keys:
sys_label = _get_system_hw_label(all_results[sys_idx], sys_idx, mode)
row = f"| {sys_label} | {mode.upper()} |"
for label in base_labels:
rate = category_data.get((label, mode), {}).get(sys_idx, None)
row += f" {format_rate(rate)} |"
lines.append(row)
def update_docs_file(content):
"""Generate Benchmarks.md from the template file with benchmark content inserted."""
if not os.path.exists(TEMPLATE_FILE):
print(f"Error: {TEMPLATE_FILE} not found", file=sys.stderr)
return False
with open(TEMPLATE_FILE, "r", encoding="utf-8") as f:
doc = f.read()
start_idx = doc.find(START_MARKER)
end_idx = doc.find(END_MARKER)
if start_idx == -1 or end_idx == -1:
print(f"Error: Could not find markers in {TEMPLATE_FILE}", file=sys.stderr)
return False
new_doc = (
doc[: start_idx + len(START_MARKER)]
+ "\n\n"
+ content
+ "\n\n"
+ doc[end_idx:]
)
with open(DOCS_FILE, "w", encoding="utf-8") as f:
f.write(new_doc)
print(f"Generated {DOCS_FILE} from {TEMPLATE_FILE}")
return True
def main():
print("Loading benchmark results...")
all_results = load_all_results()
print(f"Found {len(all_results)} result file(s)")
content = generate_markdown(all_results)
update_docs_file(content)
if all_results:
print("\nBenchmark tables generated successfully.")
print("Run 'mkdocs serve' to view the documentation.")
else:
print("\nNo benchmark results found. Run 'python benchmark.py' first.")
if __name__ == "__main__":
main()