-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbenchmark.py
175 lines (150 loc) · 4.94 KB
/
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
from typing import Callable, List, Tuple, Dict
from prettytable import PrettyTable
from benchmarks import Benchmark
import dataclasses
import uvloop, asyncio, time, leviathan
import matplotlib.pyplot as plt
import sys, os, statistics
import matplotlib
from benchmarks import (
event_fiesta_factory,
producer_consumer,
food_delivery,
task_workflow,
chat,
)
BENCHMARKS: List[Benchmark] = [
event_fiesta_factory.BENCHMARK,
producer_consumer.BENCHMARK,
food_delivery.BENCHMARK,
task_workflow.BENCHMARK,
chat.BENCHMARK,
]
matplotlib.use("QtAgg")
try:
os.nice(-20)
except IOError as e:
print(
f"({e}):",
"Couldn't set nice, running with default level",
file=sys.stderr,
)
N: int = 11
ITERATIONS = 10
M_INITIAL: int = 1024
M_MULTIPLIER: int = 2
LOOPS: List[Tuple[str, Callable[[], asyncio.AbstractEventLoop]]] = [
("asyncio", asyncio.new_event_loop),
("uvloop", uvloop.new_event_loop),
("leviathan", leviathan.Loop),
]
@dataclasses.dataclass
class TimeMetrics:
min: float
max: float
avg: float
stdev: float
dict = dataclasses.asdict
def __init__(self, times: List[float]):
self.min = min(times)
self.max = max(times)
self.avg = statistics.mean(times)
self.stdev = statistics.stdev(times)
def benchmark_with_event_loops(
loops: List[Tuple[str, Callable[[], asyncio.AbstractEventLoop]]],
function: Callable[[asyncio.AbstractEventLoop, int], None],
) -> Dict[str, List[Tuple[int, TimeMetrics]]]:
results: Dict[str, List[Tuple[int, TimeMetrics]]] = {}
for loop_name, loop_creator in loops:
results[loop_name] = []
m: int = M_INITIAL
print("Starting benchmark with loop:", loop_name)
loop = loop_creator()
try:
while m <= M_INITIAL * (2 ** (N - 1)):
times: list[float] = []
for _ in range(ITERATIONS):
start: float = time.perf_counter()
function(loop, m)
end: float = time.perf_counter()
times.append(end - start)
metrics = TimeMetrics(times)
print(" - ".join((
loop_name,
str(m),
", ".join(
f"{key}: {value:.6f} s"
for key, value
in metrics.dict().items()
)
)))
results[loop_name].append((m, metrics))
m *= M_MULTIPLIER
finally:
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
print("-" * 50)
return results
def create_comparison_table(
results: Dict[str, List[Tuple[int, TimeMetrics]]],
) -> None:
table: PrettyTable = PrettyTable()
table.field_names = [
"Loop",
"M",
*(
f"{field.capitalize()} (s)"
for field in TimeMetrics.__annotations__
),
"Diff (s)",
"Relative Speed",
]
base_loop_results = results["asyncio"]
for loop_name, loop_results in results.items():
for i, (m, _time) in enumerate(loop_results):
base_time: float = base_loop_results[i][1].avg
relative_time: float = base_time / _time.avg
diff = _time.avg - base_time
table.add_row([
loop_name,
m,
*[
f"{value:.6f}"
for value
in list(_time.dict().values()) + [diff, relative_time]
],
])
print(table)
def plot_results(
results: Dict[str, List[Tuple[int, TimeMetrics]]],
name: str
) -> None:
plt.figure(figsize=(10, 6))
for loop_name, loop_results in results.items():
x: List[int] = [m for m, _ in loop_results]
y: List[float] = [time.avg for _, time in loop_results]
lows: List[float] = [time.avg - time.min for _, time in loop_results]
highs: List[float] = [time.max - time.avg for _, time in loop_results]
# stdevs: List[float] = [time.stdev for _, time in loop_results]
# plt.errorbar(x, y, stdevs, marker="o")
plt.errorbar(x, y, [lows, highs], marker="o", label=loop_name, capsize=5)
plt.xscale("log", base=2)
plt.yscale("log")
plt.xlabel("M (log scale)")
plt.ylabel("Time (s, log scale)")
plt.title(f"Benchmark Comparison Across Event Loops ({name}. Less is better)")
plt.legend()
plt.grid(True, which="both", linestyle="--", linewidth=0.5)
plt.tight_layout()
plt.show()
if __name__ == "__main__":
for benchmark in BENCHMARKS:
print("Starting test for function:", benchmark.name)
benchmark_results = benchmark_with_event_loops(
LOOPS,
benchmark.function
)
create_comparison_table(benchmark_results)
plot_results(benchmark_results, benchmark.name)
print("-" * 50)
print()