-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathbenchmark_report.py
More file actions
executable file
·490 lines (412 loc) · 15.9 KB
/
benchmark_report.py
File metadata and controls
executable file
·490 lines (412 loc) · 15.9 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""
Benchmark report generator for Go Fory benchmarks.
Generates plots and markdown reports from benchmark results.
"""
import json
import os
import platform
import re
import sys
from collections import defaultdict
from datetime import datetime
from pathlib import Path
try:
import matplotlib.pyplot as plt
HAS_MATPLOTLIB = True
except ImportError:
HAS_MATPLOTLIB = False
print("Warning: matplotlib not installed. Skipping plot generation.")
# Color scheme (matching C++ benchmark)
COLORS = {
"fory": "#FF6f01", # Orange
"protobuf": "#55BCC2", # Teal
"msgpack": "#9B59B6", # Purple
}
DATATYPES = [
"struct",
"structlist",
"sample",
"samplelist",
"mediacontent",
"mediacontentlist",
]
OPERATIONS = ["serialize", "deserialize"]
SERIALIZERS = ["fory", "protobuf", "msgpack"]
def parse_benchmark_txt(filepath):
"""Parse Go benchmark text output format."""
results = defaultdict(lambda: defaultdict(dict))
with open(filepath, "r") as f:
for line in f:
# Match lines like: BenchmarkFory_Struct_Serialize-10 1234567 789.0 ns/op
match = re.match(
r"Benchmark(\w+)_(\w+)_(Serialize|Deserialize)-\d+\s+\d+\s+([\d.]+)\s+ns/op",
line,
)
if match:
serializer = match.group(1).lower()
datatype = match.group(2).lower()
operation = match.group(3).lower()
ns_per_op = float(match.group(4))
results[datatype][operation][serializer] = ns_per_op
return results
def parse_benchmark_json(filepath):
"""Parse Go benchmark JSON output format."""
results = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
with open(filepath, "r") as f:
for line in f:
try:
data = json.loads(line)
if data.get("Action") == "output" and "Benchmark" in data.get(
"Output", ""
):
output = data["Output"]
# Match benchmark result lines
match = re.match(
r"Benchmark(\w+)_(\w+)_(Serialize|Deserialize)-\d+\s+\d+\s+([\d.]+)\s+ns/op",
output,
)
if match:
serializer = match.group(1).lower()
datatype = match.group(2).lower()
operation = match.group(3).lower()
ns_per_op = float(match.group(4))
results[datatype][operation][serializer].append(ns_per_op)
except json.JSONDecodeError:
continue
# Average multiple runs
final_results = defaultdict(lambda: defaultdict(dict))
for datatype, ops in results.items():
for op, serializers in ops.items():
for serializer, times in serializers.items():
if times:
final_results[datatype][op][serializer] = sum(times) / len(times)
return final_results
def parse_serialized_sizes(text):
sizes = {}
current = None
for line in text.splitlines():
line = line.strip()
if not line or line.startswith("="):
continue
if line.endswith(":") and not line.startswith(
("Fory:", "Protobuf:", "Msgpack:")
):
current = line.rstrip(":")
sizes[current] = {}
continue
if current is None:
continue
match = re.match(r"^(Fory|Protobuf|Msgpack):\s+(\d+)\s+bytes$", line)
if match:
serializer = match.group(1).lower()
size = int(match.group(2))
sizes[current][serializer] = size
return sizes
def load_serialized_sizes(output_dir):
size_files = [
Path(output_dir) / "serialized_sizes.txt",
Path(output_dir) / "benchmark_results.txt",
]
for path in size_files:
if not path.exists():
continue
text = path.read_text(encoding="utf-8", errors="ignore")
if "Serialized Sizes (bytes):" in text:
return parse_serialized_sizes(text)
return {}
def generate_plots(results, output_dir):
"""Generate comparison plots for each data type."""
if not HAS_MATPLOTLIB:
return
for datatype in DATATYPES:
if datatype not in results:
continue
fig, axes = plt.subplots(1, 2, figsize=(14, 6))
fig.suptitle(
f"{datatype.title()} Serialization Benchmark",
fontsize=14,
fontweight="bold",
)
for idx, op in enumerate(OPERATIONS):
ax = axes[idx]
if op not in results[datatype]:
continue
data = results[datatype][op]
available_serializers = [s for s in SERIALIZERS if s in data]
if not available_serializers:
continue
# Convert ns to ops/sec
ops_per_sec = [
1e9 / data[s] if s in data else 0 for s in available_serializers
]
colors = [COLORS.get(s, "#888888") for s in available_serializers]
bars = ax.bar(available_serializers, ops_per_sec, color=colors)
ax.set_ylabel("Operations/sec")
ax.set_title(f"{op.title()}")
# Add value labels on bars
for bar, val in zip(bars, ops_per_sec):
height = bar.get_height()
ax.annotate(
f"{val / 1e6:.2f}M" if val >= 1e6 else f"{val / 1e3:.0f}K",
xy=(bar.get_x() + bar.get_width() / 2, height),
xytext=(0, 3),
textcoords="offset points",
ha="center",
va="bottom",
fontsize=9,
)
# Add speedup annotations
if "fory" in data:
fory_val = 1e9 / data["fory"]
speedup_lines = []
for s in available_serializers:
if s != "fory" and s in data:
other_val = 1e9 / data[s]
speedup = fory_val / other_val
if speedup > 1:
speedup_lines.append(
f"Fory {speedup:.1f}x faster than {s.title()}"
)
if speedup_lines:
ax.text(
0.98,
0.98,
"\n".join(speedup_lines),
transform=ax.transAxes,
ha="right",
va="top",
fontsize=9,
color="green",
fontweight="bold",
bbox=dict(
boxstyle="round,pad=0.25",
facecolor="white",
edgecolor="none",
alpha=0.85,
),
)
plt.tight_layout()
plt.savefig(
os.path.join(output_dir, f"benchmark_{datatype}.png"),
dpi=150,
bbox_inches="tight",
)
plt.close()
def generate_combined_plot(results, output_dir):
"""Generate a combined plot showing all benchmarks."""
if not HAS_MATPLOTLIB:
return
datatypes = DATATYPES
operations = OPERATIONS
serializers = SERIALIZERS
cols = len(datatypes)
fig_width = max(12, 3.5 * cols)
fig, axes = plt.subplots(len(operations), cols, figsize=(fig_width, 10))
if cols == 1:
axes = [[axes[row]] for row in range(len(operations))]
fig.suptitle(
"Go Serialization Benchmark: Fory vs Protobuf vs Msgpack",
fontsize=14,
fontweight="bold",
)
for row, op in enumerate(operations):
for col, datatype in enumerate(datatypes):
ax = axes[row][col]
if datatype not in results or op not in results[datatype]:
ax.text(
0.5,
0.5,
"No data",
ha="center",
va="center",
transform=ax.transAxes,
)
continue
data = results[datatype][op]
available_serializers = [s for s in serializers if s in data]
if not available_serializers:
continue
ops_per_sec = [
1e9 / data[s] if s in data else 0 for s in available_serializers
]
colors = [COLORS.get(s, "#888888") for s in available_serializers]
bars = ax.bar(available_serializers, ops_per_sec, color=colors)
ax.set_title(f"{datatype.title()} - {op.title()}")
ax.set_ylabel("ops/sec")
# Add value labels
for bar, val in zip(bars, ops_per_sec):
height = bar.get_height()
label = f"{val / 1e6:.2f}M" if val >= 1e6 else f"{val / 1e3:.0f}K"
ax.annotate(
label,
xy=(bar.get_x() + bar.get_width() / 2, height),
xytext=(0, 3),
textcoords="offset points",
ha="center",
va="bottom",
fontsize=8,
)
plt.tight_layout()
plt.savefig(
os.path.join(output_dir, "benchmark_combined.png"), dpi=150, bbox_inches="tight"
)
plt.close()
def generate_markdown_report(results, output_dir):
"""Generate markdown report."""
datatypes = DATATYPES
operations = OPERATIONS
report = []
report.append("# Go Serialization Benchmark Report\n")
report.append(f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
# System info
report.append("## System Information\n")
report.append(f"- **OS**: {platform.system()} {platform.release()}")
report.append(f"- **Architecture**: {platform.machine()}")
report.append(f"- **Python**: {platform.python_version()}")
report.append("")
# Summary table
report.append("## Performance Summary\n")
report.append(
"| Data Type | Operation | Fory (ops/s) | Protobuf (ops/s) | Msgpack (ops/s) | Fory vs PB | Fory vs MP |"
)
report.append(
"|-----------|-----------|--------------|------------------|-----------------|------------|------------|"
)
for datatype in datatypes:
if datatype not in results:
continue
for op in operations:
if op not in results[datatype]:
continue
data = results[datatype][op]
fory_ops = 1e9 / data.get("fory", float("inf")) if "fory" in data else 0
pb_ops = (
1e9 / data.get("protobuf", float("inf")) if "protobuf" in data else 0
)
mp_ops = 1e9 / data.get("msgpack", float("inf")) if "msgpack" in data else 0
fory_str = (
f"{fory_ops / 1e6:.2f}M"
if fory_ops >= 1e6
else f"{fory_ops / 1e3:.0f}K"
)
pb_str = f"{pb_ops / 1e6:.2f}M" if pb_ops >= 1e6 else f"{pb_ops / 1e3:.0f}K"
mp_str = f"{mp_ops / 1e6:.2f}M" if mp_ops >= 1e6 else f"{mp_ops / 1e3:.0f}K"
fory_vs_pb = f"{fory_ops / pb_ops:.2f}x" if pb_ops > 0 else "N/A"
fory_vs_mp = f"{fory_ops / mp_ops:.2f}x" if mp_ops > 0 else "N/A"
report.append(
f"| {datatype.title()} | {op.title()} | {fory_str} | {pb_str} | {mp_str} | {fory_vs_pb} | {fory_vs_mp} |"
)
report.append("")
# Timing details
report.append("## Detailed Timing (ns/op)\n")
report.append("| Data Type | Operation | Fory | Protobuf | Msgpack |")
report.append("|-----------|-----------|------|----------|---------|")
for datatype in datatypes:
if datatype not in results:
continue
for op in operations:
if op not in results[datatype]:
continue
data = results[datatype][op]
fory_ns = f"{data.get('fory', 0):.1f}"
pb_ns = f"{data.get('protobuf', 0):.1f}"
mp_ns = f"{data.get('msgpack', 0):.1f}"
report.append(
f"| {datatype.title()} | {op.title()} | {fory_ns} | {pb_ns} | {mp_ns} |"
)
report.append("")
# Serialized size section
sizes = load_serialized_sizes(output_dir)
report.append("### Serialized Data Sizes (bytes)\n")
if sizes:
report.append("| Data Type | Fory | Protobuf | Msgpack |")
report.append("|-----------|------|----------|---------|")
name_map = {
"NumericStruct": "Struct",
"Sample": "Sample",
"MediaContent": "MediaContent",
"StructList": "StructList",
"SampleList": "SampleList",
"MediaContentList": "MediaContentList",
}
ordered = [
"NumericStruct",
"Sample",
"MediaContent",
"StructList",
"SampleList",
"MediaContentList",
]
for key in ordered:
if key not in sizes:
continue
entry = sizes[key]
report.append(
f"| {name_map.get(key, key)} | {entry.get('fory', 'N/A')} | {entry.get('protobuf', 'N/A')} | {entry.get('msgpack', 'N/A')} |"
)
else:
report.append("No serialized size data found.\n")
# Plots section
if HAS_MATPLOTLIB:
report.append("## Performance Charts\n")
report.append("### Combined Overview")
report.append("\n")
for datatype in datatypes:
if datatype in results:
report.append(f"### {datatype.title()}")
report.append(
f"\n"
)
# Write report
report_path = os.path.join(output_dir, "benchmark_report.md")
with open(report_path, "w") as f:
f.write("\n".join(report))
print(f"Report generated: {report_path}")
def main():
# Accept output directory as argument, default to ./results
if len(sys.argv) > 1:
output_dir = Path(sys.argv[1])
else:
output_dir = Path(__file__).parent / "results"
# Try to parse results
txt_path = output_dir / "benchmark_results.txt"
json_path = output_dir / "benchmark_results.json"
results = None
if txt_path.exists():
print(f"Parsing {txt_path}...")
results = parse_benchmark_txt(txt_path)
elif json_path.exists():
print(f"Parsing {json_path}...")
results = parse_benchmark_json(json_path)
else:
print("Error: No benchmark results found.")
print("Run ./run.sh first to generate benchmark results.")
sys.exit(1)
if not results:
print("Error: Could not parse benchmark results.")
sys.exit(1)
print("Parsed results for data types:", list(results.keys()))
# Generate outputs
generate_plots(results, output_dir)
generate_combined_plot(results, output_dir)
generate_markdown_report(results, output_dir)
print("Done!")
if __name__ == "__main__":
main()