forked from FastVM/Web49
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.py
159 lines (140 loc) · 4.12 KB
/
bench.py
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
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import os
import subprocess
import time
import argparse
import colorsys
import json
cur = os.path.dirname(os.path.realpath(__file__))
os.chdir('test/bench')
parser = argparse.ArgumentParser(
prog='bench',
description='Benchmark Wasm Engines',
)
parser.add_argument('--runs', '-r', type=int, help='number of benchmark runs', default=1)
parser.add_argument('--engine', '-e', type=str, action='append', default=[])
parser.add_argument('--test', '-t', type=str, action='append', default=[])
parser.add_argument('--compiler', '-c', type=str, default='emcc')
parser.add_argument('--opt', '-O', type=str, default='2')
args = parser.parse_args()
runs = args.runs
engines = args.engine
emcc = args.compiler
optlevel = args.opt
if len(engines) == 0:
engines = ['wasm3', os.path.join(cur, 'bin/miniwasm')]
if len(args.test) == 0:
tests = {
'trap': {
'runs': 10,
'args': [],
'memory': 256,
},
'nop': {
'runs': 100,
'args': [],
'memory': 256,
},
'coremark': {
'runs': 1,
'args': ['3000'],
'memory': 256,
},
'fib_f32': {
'runs': 1,
'args': ['40'],
'memory': 256,
},
'fib_i32': {
'runs': 1,
'args': ['40'],
'memory': 256,
},
'fib_f64': {
'runs': 1,
'args': ['40'],
'memory': 256,
},
'fib_i64': {
'runs': 1,
'args': ['40'],
'memory': 256,
},
'binary-trees': {
'runs': 1,
'args': ['16'],
'memory': 2**12,
},
'fannkuch-redux': {
'runs': 1,
'args': ['10'],
'memory': 256,
},
'mandelbrot-simd': {
'runs': 1,
'args': ['2000'],
'memory': 256,
},
'mandelbrot': {
'runs': 1,
'args': ['2000'],
'memory': 256,
},
'nbody': {
'runs': 1,
'args': ['2000000'],
'memory': 256,
},
}
else:
tests = {}
for test in args.test:
[name, *args1] = test.split(':')
split = name.split('+')
if len(split) == 1:
n = 1
else:
name = split[0]
n = int(split[1])
tests[name] = {
'args': args1,
'runs': n,
'memory': 256,
}
obj = {}
testdata = {}
for i in range(1, runs+1):
print('RUN: #' + str(i))
for test in tests.keys():
print(' TEST: ' + test)
build = subprocess.call([*emcc.split(' '), '-Xlinker', f'--initial-memory={tests[test]["memory"] * (2 ** 16)}', f"-O{optlevel}", test + '.c', '-o', test + '.wasm'])
if test not in testdata:
testdata[test] = {}
data = testdata[test]
for engine in engines:
print(' ENGINE: ' + engine)
if engine not in data:
data[engine] = []
times = data[engine]
for i in range(tests[test]['runs']):
start = time.time()
run = subprocess.call([engine, test + '.wasm', *tests[test]['args']], stdout=subprocess.DEVNULL)
end = time.time()
times.append(end - start)
fig, ax = plt.subplots()
keys = data.keys()
values = [sum(data[i]) / len(data[i]) for i in keys]
keys = [key.replace(cur, '.').replace('./bin/', '') for key in keys]
for i,key in enumerate(keys):
rgb = colorsys.hsv_to_rgb(i / (len(keys) * (5/4)), 0.4, 0.9)
ax.bar(key, values[i], color=(*rgb, 1.0))
ax.set_ylabel('time taken in seconds')
ax.set_xlabel('wasm engine')
ax.set_title(' '.join([test, *tests[test]['args']]))
fig.savefig(test + '.png')
obj[test] = {}
for i,key in enumerate(keys):
obj[test][key] = values[i]
with open('results.json', 'w') as outputfp:
json.dump(obj, outputfp)