-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark.py
123 lines (94 loc) · 3.46 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
import os
import subprocess
import time
import requests
import socket
from rich import print
from rich.table import Table
RESULT_DIR = "results"
SERVERS_DIR = "servers"
results = []
os.makedirs(RESULT_DIR, exist_ok=True)
def wait_for_server(framework):
while True:
try:
requests.get("http://localhost:8000", timeout=1)
print(f"[bold green]✔️ {framework} server started[/bold green] :rocket:")
break
except requests.exceptions.ConnectionError:
print(
f"[yellow]⏳ Waiting for [bold]{framework} server[/bold] to start...[/yellow]"
)
time.sleep(1)
def wait_for_cleanup():
while True:
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", 8000))
break
except socket.error:
print("[yellow]⏳ [bold]Waiting for cleanup...[/bold][/yellow]")
time.sleep(1)
pass
def start_server(framework):
wait_for_cleanup()
command = f"make {framework}"
server_process = subprocess.Popen(
command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
wait_for_server(framework)
return server_process
def kill_server(server_process, framework):
print(f"[bold red]🛑 [red]Killing {framework} server[/red][/bold red]")
server_process.terminate()
def run_wkr(framework):
command = "make benchmark"
print(f"[cyan]🚀 [bold]Running wrk for {framework}[/bold] 🚀[/cyan]")
return subprocess.run(command, shell=True, text=True, stdout=subprocess.PIPE)
def write_and_add_results(benchmark_result, framework):
stdout_text = benchmark_result.stdout
output_file_path = os.path.join(RESULT_DIR, f"{framework}.txt")
with open(output_file_path, "w") as f:
f.write(stdout_text)
# Extract relevant data from the 'wrk' result and store it
result_lines = stdout_text.strip().split("\n")
result_data = {}
for line in result_lines:
parts = line.strip().split(": ")
if len(parts) == 2:
key, value = parts
result_data[key] = value
# Append the result data to the results list
results.append(
{
"Framework": framework,
"Requests/sec": result_data.get("Requests/sec", 0),
"Transfer/sec": result_data.get("Transfer/sec", 0),
}
)
def load_test(framework):
server_process = start_server(framework)
benchmark_result = run_wkr(framework)
kill_server(server_process, framework)
write_and_add_results(benchmark_result, framework)
def get_framework_names():
return os.listdir(SERVERS_DIR)
if __name__ == "__main__":
for framework_name in get_framework_names():
load_test(framework_name)
# Create a rich Table to display the results
table = Table(title="Benchmark Results using wrk with 12 threads and 400 connections for 10s")
table.add_column("Framework", style="bold green")
table.add_column("Requests/sec", style="bold blue")
table.add_column("Transfer/sec", style="bold blue")
results.sort(key=lambda x: x["Requests/sec"], reverse=True)
# Add data to the table
for result in results:
table.add_row(
f"[bold]{result['Framework']}[/bold]", # Apply style to Framework column values
result["Requests/sec"],
result["Transfer/sec"],
)
# Print the table using rich
print()
print(table)