-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot_benchmarks.py
More file actions
executable file
·450 lines (369 loc) · 13.7 KB
/
Copy pathplot_benchmarks.py
File metadata and controls
executable file
·450 lines (369 loc) · 13.7 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
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "matplotlib",
# "pyqt6",
# "numpy",
# ]
# ///
"""
Script that generates:
1. A bar chart displaying the relative performance of asyncify and wasmfx benchmarks, grouped by engine
2. $number_of_engines$ bar charts displaying the raw runtime/binary size/etc data for each engine
3. $number_of_benchmarks$ bar charts displaying the raw runtime/binary size/etc data for each benchmark
Where
(1) is saved in results_dir/relative/relative.png,
(2) is saved in results_dir/absolute_engines/absolute_{engine}.png,
(3) is saved in results_dir/absolute_benchmarks/absolute_{benchmark}.png
Usage:
`python3 plot_benchmarks.py results_wasmfx.json results_asyncify.json --benchmarks itersum --engines wasmtime d8 -o results_dir`
"""
import argparse
import json
import os
import pathlib
import matplotlib.pyplot as plt
import numpy as np
import math
# deal with inputs
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"files", nargs="+", type=pathlib.Path, help="JSON files with benchmark results"
)
parser.add_argument(
"--benchmarks",
nargs="*",
help="List of benchmarks to plot (e.g. sieve, itersum, treesum)",
)
parser.add_argument(
"--engines", nargs="*", help="List of engines to plot (d8, wasmtime, wizard)"
)
parser.add_argument(
"-o",
"--output",
help="""
Save image to the given directory (specific charts will be saved in subdirectories
relative/, absolute_engines/, and absolute_benchmarks/)
""",
)
args = parser.parse_args()
benches = args.benchmarks
engines = args.engines
styles = ["wasmfx", "asyncify"]
if benches is None:
raise ValueError("--benchmarks of interest must be specified")
if engines is None:
raise ValueError("--engines of interest must be specified")
# Load a set of files, either by directory or as an immediate list of files.
if os.path.isdir(args.files[0]):
if len(args.files) > 1:
raise ValueError(
"files input should be a single dir with all benchmark results, or a list of json files."
)
results_files = pathlib.Path(args.files[0]).glob("results_*.json")
info_files = pathlib.Path(args.files[0]).glob("*_info.json")
else:
results_files = [f for f in args.files if f.name.startswith("results_")]
info_files = [f for f in args.files if f.name.endswith("_info.json")]
# Collect all the JSON data into a list of results we can query.
data = []
for i, filename in enumerate(results_files):
print(f"Loading data from {filename}...")
with open(filename) as f:
data.extend(json.load(f)["results"])
# Load JSON files containing compiler/engine/parameter into a dict
build_info = {}
for i, filename in enumerate(info_files):
print(f"Loading build data from {filename}...")
with open(filename) as f:
build_info.update(json.load(f))
# Predicates for the hyperfine output json format, to filter results by
# benchmark, engine, and style (wasmfx vs asyncify).
def benchmark_is(result, benchmark):
return result["parameters"]["benchmark"] == benchmark
def engine_is(result, engine):
return result["parameters"]["engine"] == engine
def style_is(result, style):
return result["parameters"]["style"] == style
def fetch_one(data, style, benchmark, engine):
results = [
x
for x in data
if style_is(x, style) and benchmark_is(x, benchmark) and engine_is(x, engine)
]
if len(results) < 1:
raise ValueError(f"No data for {style} {benchmark} {engine}.")
if len(results) > 1:
raise ValueError(f"Too many data points for {style} {benchmark} {engine}!")
return results[0]
def organize(data):
"""Organize the data into a 3-level nested list, by style, benchmark, engine, in the order
specified by the global lists `benches` and `engines`. If there are extra data points in the
input, they are dropped. If any of the specified combinations is missing, we'll get an
IndexError.
"""
return [
[
[fetch_one(data, style, benchmark, engine) for engine in engines]
for benchmark in benches
]
for style in styles
]
def grouped_position(set, offset, cadence):
"""Maps an item in a grouped set of sets to a unique x value for plotting, given the offset of
the set and the cadence (number of items in each set).
"""
return set * (cadence + 1) + offset
def array_positions(ary):
"""Returns positions for items in a grouped list-of-lists, with gaps between groups.
Outermost indices correspond to ary's outer indices, and inner to inner.
"""
return [
[grouped_position(j, i, len(ary)) for j in range(len(ary[0]))]
for i in range(len(ary))
]
nested_data = organize(data)
data_means = np.array(
[[[cell["mean"] for cell in row] for row in plane] for plane in nested_data]
)
data_stddev = np.array(
[[[cell["stddev"] for cell in row] for row in plane] for plane in nested_data]
)
# We now have two numpy arrays whose dimensions are [style][benchmark][engine]
ASYNCIFY_INDEX = 1
WASMFX_INDEX = 0
# The width of the bars in the chart
width = 1
# Hope this is colourblind-friendly enough for sam
bar_colors = [
"tab:blue",
"tab:orange",
"tab:cyan",
"tab:red",
"tab:purple",
"tab:brown",
"tab:pink",
"tab:gray",
]
# --------------
# ------- First chart: ratio of asyncify time to wasmfx time for each benchmark across all engines -----
# --------------
data_wasmfx_means = data_means[WASMFX_INDEX]
data_asyncify_means = data_means[ASYNCIFY_INDEX]
# Get the chart data: divide first column by second column to obtain asyncify / wasmfx ratio
ratio = np.divide(data_asyncify_means, data_wasmfx_means)
# Get the standard deviation for the ratio.
# First we want the standard deviations as percentages of the mean:
data_wasmfx_stddev = data_stddev[WASMFX_INDEX]
data_asyncify_stddev = data_stddev[ASYNCIFY_INDEX]
# Then we apply the formula for error propagation for division
ratio_stddev = ratio * np.sqrt(
np.square(data_asyncify_stddev / data_asyncify_means)
+ np.square(data_wasmfx_stddev / data_wasmfx_means)
)
# Compute x values for bar locations, with gaps between groups of bars for each engine
# eg. x = [0,1,2,4,5,6,8,9,10] for 3 benchmarks across 3 engines,
# with a gap of 1 between each group of 3 bars for each engine
bar_loc = array_positions(ratio)
# Plot the data
fig, (ax1, ax2) = plt.subplots(2,1, figsize=(10, 10), gridspec_kw={'height_ratios': [3, 1]})
for i in range(len(ratio)):
ax1.bar(
bar_loc[i],
ratio[i],
width,
label=benches[i],
color=bar_colors[i],
)
# Pad out list of engines to match number of benchmarks, so x-axis labels are engines
axis_labels = np.repeat(engines, len(benches))
ax1.set_xticks(np.array(bar_loc).transpose().flatten(), axis_labels)
# Keeps every nth label, make the rest invisible
n = len(benches)
[
l.set_visible(False)
for (i, l) in enumerate(ax1.xaxis.get_ticklabels())
if i % n != math.ceil(n / 2) - 1
]
# Error bars
ax1.errorbar(
np.array(bar_loc).flatten(),
ratio.flatten(),
yerr=ratio_stddev.flatten(),
fmt="none",
capsize=3,
linewidth=0.5,
color="black",
label="Standard Deviation",
)
# Readability features
ax1.set(title="Benchmark results (Asyncify time / WasmFX time)",
xlabel="Engine",
ylabel="Speedup (relative to Asyncify)",
)
ax1.grid(visible=True, axis="y")
ax1.legend(benches, loc="upper right")
# Add a horizontal line at y=1 to indicate where wasmfx and asyncify have equal performance
ax1.axhline(y=1.0, color="r", linestyle="--", linewidth=3, label="WasmFX = Asyncify")
# Add text to ax2
ax2.axis([0, 10, 0, 10])
ax2.tick_params(axis='x', colors='white')
ax2.tick_params(axis='y', colors='white')
# Generate string containing compiler info
compile_info = "\n".join([
build_info["wasm_opt_ver"]
+ "with "
+ build_info["wasm_opt_flags"],
build_info["wasicc_ver"]
+ "with "
+ build_info["wasicc_flags"],
build_info["wasmfxtime_ver"]
])
# Generate string containing engine info, based on the engines that were used in this run
engine_info = "\n".join([build_info[engine] for engine in engines])
# Do the same for benchmark arguments
args_info = "\n".join([bench + ": " + build_info[bench] for bench in benches])
# Now put these on the lower subplot.
ax2.text(0.5, 8, 'Compilation info', weight='bold', fontsize=10)
ax2.text(0.5, 5.5, compile_info, fontsize=9 )
ax2.text(5.5, 8, 'Benchmark arguments', weight='bold', fontsize=10)
ax2.text(5.5, 4.5, args_info, fontsize=9 )
ax2.text(0.5, 4, 'Engine info', weight='bold', fontsize=10)
ax2.text(0.5, 1.5, engine_info, fontsize=9 )
# Export figure
if args.output:
plt.savefig(f"{args.output}/relative/relative", bbox_inches="tight")
else:
plt.show()
# --------------
# ----- Next charts: absolute times for asyncify and wasmfx for each benchmark, grouped by engine -----
# --------------
# We want a different chart for each engine where bars are grouped by benchmarks,
# and each bar corresponds to a different backend (wasmfx, asyncify).
for i, engine in enumerate(engines):
# Get array of data from this engine
engine_data = data_means[:, :, i]
engine_data_stddev = data_stddev[:, :, i]
bar_loc = array_positions(engine_data)
# Plot data
fig, (ax1, ax2) = plt.subplots(2,1, figsize=(10, 10), gridspec_kw={'height_ratios': [3, 1]})
for j in range(len(styles)):
ax1.bar(
bar_loc[j],
engine_data[j],
width,
label=["WasmFX", "Asyncify"][j],
color=bar_colors[j % 2],
)
# Pad out list of engines to match number of benchmarks, so x-axis labels are engines
axis_labels = np.repeat(benches, 2)
ax1.set_xticks(np.array(bar_loc).transpose().flatten(), axis_labels)
# Keeps every other label, make the rest invisible
[
l.set_visible(False)
for (i, l) in enumerate(ax1.xaxis.get_ticklabels())
if (i + 1) % 2 == 0
]
# Error bars
ax1.errorbar(
np.array(bar_loc).flatten(),
engine_data.flatten(),
yerr=engine_data_stddev.flatten(),
fmt="none",
capsize=3,
linewidth=0.5,
color="black",
label="Standard Deviation",
)
# Readability features
ax1.set(title=f"Benchmark results (absolute times) for {engine}",
xlabel="Benchmark",
ylabel="Time (seconds)",
)
ax1.grid(visible=True, axis="y")
ax1.legend(["WasmFX", "Asyncify"], loc="upper right")
# Add text to ax2
ax2.axis([0, 10, 0, 10])
ax2.tick_params(axis='x', colors='white')
ax2.tick_params(axis='y', colors='white')
ax2.text(0.5, 8, 'Compilation info', weight='bold', fontsize=10)
ax2.text(0.5, 5.5, compile_info, fontsize=9 )
ax2.text(5.5, 8, 'Benchmark arguments', weight='bold', fontsize=10)
ax2.text(5.5, 4.5, args_info, fontsize=9 )
# Version of the current engine being charted
ax2.text(0.5, 4, 'Engine version', weight='bold', fontsize=10)
ax2.text(0.5, 3, build_info[engine], fontsize=9 )
# Export figure
if args.output:
plt.savefig(
f"{args.output}/absolute_engines/absolute_{engine}", bbox_inches="tight"
)
else:
plt.show()
# --------------
# ----- Next charts: absolute times for asyncify and wasmfx, grouped by benchmark -----
# --------------
# Now we want a different chart for each benchmark where bars are grouped by engines
for i, benchmark in enumerate(benches):
# Get array of data from this benchmark
# There should always be 2 * len(engines) entries for each benchmark
bench_data = data_means[:, i]
bench_data_stddev = data_stddev[:, i]
bar_loc = array_positions(bench_data)
# Plot data
fig, (ax1, ax2) = plt.subplots(2,1, figsize=(10, 10), gridspec_kw={'height_ratios': [3, 1]})
for i in range(len(bench_data)):
ax1.bar(
bar_loc[i],
bench_data[i],
width,
label=engines[i % len(engines)],
color=bar_colors[i % 2],
)
# Pad out list of benchmarks to match number of engines
axis_labels = np.repeat(engines, 2)
ax1.set_xticks(np.array(bar_loc).transpose().flatten(), axis_labels)
# Keeps every other label, make the rest invisible
n = len(engines)
[
l.set_visible(False)
for (i, l) in enumerate(ax1.xaxis.get_ticklabels())
if (i + 1) % 2 == 0
]
# Error bars
ax1.errorbar(
np.array(bar_loc).flatten(),
bench_data.flatten(),
yerr=bench_data_stddev.flatten(),
fmt="none",
capsize=3,
linewidth=0.5,
color="black",
label="Standard Deviation",
)
# Readability features
ax1.set(title=f"Benchmark results (absolute times) for {benchmark}",
xlabel="Engine",
ylabel="Time (seconds)",)
ax1.grid(visible=True, axis="y")
ax1.legend(["WasmFX", "Asyncify"], loc="upper right")
# Add text to ax2
ax2.axis([0, 10, 0, 10])
ax2.tick_params(axis='x', colors='white')
ax2.tick_params(axis='y', colors='white')
ax2.text(0.5, 8, 'Compilation info', weight='bold', fontsize=10)
ax2.text(0.5, 5.5, compile_info, fontsize=9 )
ax2.text(5.5, 8, f'Benchmark arguments ({benchmark})', weight='bold', fontsize=10)
ax2.text(5.5, 7, build_info[benchmark], fontsize=9 )
# Version of the current engine being charted
ax2.text(0.5, 4, 'Engine version', weight='bold', fontsize=10)
ax2.text(0.5, 1.5, engine_info, fontsize=9 )
# Export figure
if args.output:
plt.savefig(
f"{args.output}/absolute_benchmarks/absolute_{benchmark}",
bbox_inches="tight",
)
else:
plt.show()