-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_benchmark.py
180 lines (158 loc) · 5.4 KB
/
run_benchmark.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from pathlib import Path
from typing import Optional, TypedDict
import subprocess
import os
import multiprocessing
import matplotlib.pyplot as plt
import shutil
import json
PROGS_DIR = Path("./bpf_progs")
EXECUTABLES = [
"./ubpf-jit",
"./llvm-jit",
"./rbpf-jit",
"./rbpf-jit-vm",
"./ubpf-jit-vm",
"<NATIVE>",
"<WASM>",
]
class RunResult(TypedDict):
jit_usage: int
exec_usage: int
result: int
name: str
class RunArgs(TypedDict):
runtime: str
bpf_prog: str
memory: Optional[str]
name: str
def run_single(
runtime: str, bpf_prog: str, memory: Optional[str], name: str
) -> RunResult:
if runtime == "<NATIVE>":
command_line = [bpf_prog.replace(".bpf.bin", ".native")]
if memory:
command_line.append(memory)
elif runtime == "<WASM>":
command_line = ["./wasm-wasmtime-runtime", bpf_prog.replace(".bpf.bin", ".wasm")]
if memory:
command_line.append(memory)
else:
command_line = [runtime, bpf_prog] + ([memory] if memory else [])
ret = subprocess.run(command_line, text=True, capture_output=True, check=True)
jit, exec, ret = (int(x) for x in ret.stdout.strip().split(" "))
return {"exec_usage": exec, "jit_usage": jit, "result": ret, "name": name}
def run_multiple_wrapper(cfg: RunArgs):
ret = {
"exec_usage": 0,
"jit_usage": 0,
"result": [],
"name": cfg["name"],
"metrics": {
"distribution": {},
"avg": 0,
"median": 0,
"max": None,
"min": None,
},
}
for _ in range(10):
r = run_single(**cfg)
ret["exec_usage"] += r["exec_usage"]
ret["jit_usage"] += r["jit_usage"]
ret["result"].append(r["result"])
# Update distribution
ret["metrics"]["distribution"][r["result"]] = (
ret["metrics"]["distribution"].get(r["result"], 0) + 1
)
# Update max and min
if ret["metrics"]["max"] is None or r["result"] > ret["metrics"]["max"]:
ret["metrics"]["max"] = r["result"]
if ret["metrics"]["min"] is None or r["result"] < ret["metrics"]["min"]:
ret["metrics"]["min"] = r["result"]
ret["exec_usage"] /= 50
ret["jit_usage"] /= 50
# Calculate average and median
ret["metrics"]["avg"] = sum(ret["result"]) / len(ret["result"])
sorted_results = sorted(ret["result"])
mid_idx = len(sorted_results) // 2
if len(sorted_results) % 2 == 0:
ret["metrics"]["median"] = (
sorted_results[mid_idx - 1] + sorted_results[mid_idx]
) / 2
else:
ret["metrics"]["median"] = sorted_results[mid_idx]
return ret
def main():
all_tests = []
for bpf_prog in os.listdir(PROGS_DIR):
if bpf_prog.endswith(".bpf.bin"):
name = bpf_prog.replace(".bpf.bin", "")
memory_file = str(PROGS_DIR / (name + ".mem"))
all_tests.append(
{
"name": name,
"bpf_prog": str(PROGS_DIR / bpf_prog),
"memory": memory_file if os.path.exists(memory_file) else None,
}
)
print("TESTS")
print("--------------")
for item in all_tests:
print(item["name"])
print("--------------")
print(f"Loaded {len(all_tests)} tests")
test_results = []
for runtime in EXECUTABLES:
print(f"Testing {runtime}")
with multiprocessing.Pool() as pool:
results = pool.map(
run_multiple_wrapper, ({**x, "runtime": runtime} for x in all_tests)
)
test_results.append(results)
for item in results:
print(
f"TEST <{item['name']}> JIT {item['jit_usage']/10**6}ms EXEC {item['exec_usage']/10**6}ms RET {item['result']}"
)
print("-----------------")
images_out = Path("example-output")
with open(images_out / "data.json", "w") as f:
json.dump({"executables": EXECUTABLES, "results": test_results}, f)
for test_line in zip(*test_results):
jit_values = [x["jit_usage"] for x in test_line]
exec_values = [x["exec_usage"] for x in test_line]
name = test_line[0]["name"]
plt.figure()
plt.bar(EXECUTABLES, jit_values, 0.35, label="Compilation", color="orange")
plt.ylabel("Time (nanoseconds)")
plt.xticks(rotation=45)
plt.title(f'Time usage on compiling "{name}"')
plt.legend()
plt.savefig(images_out / f"{name}.compilation.png")
plt.close()
plt.figure()
plt.bar(EXECUTABLES, exec_values, 0.35, label="Execution", color="orange")
plt.ylabel("Time (nanoseconds)")
plt.title(f'Time usage on executing "{name}"')
plt.legend()
plt.savefig(images_out / f"{name}.execution.png")
plt.close()
plt.figure()
plt.bar(EXECUTABLES, jit_values, 0.35, label="Compilation", color="blue")
plt.bar(
EXECUTABLES,
exec_values,
0.35,
bottom=jit_values,
label="Execution",
color="orange",
)
plt.xticks(rotation=45)
plt.ylabel("Time (nanoseconds)")
plt.title(f'Time usage on example "{name}"')
plt.legend()
plt.savefig(images_out / f"{name}.png")
plt.close()
print("Visualized images and data json are in output")
if __name__ == "__main__":
main()