-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark.py
More file actions
230 lines (193 loc) · 6.75 KB
/
benchmark.py
File metadata and controls
230 lines (193 loc) · 6.75 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Usage: python3 benchmark.py
This script is used to benchmark the performance of microservice communication
based on the implementations in Rust.
"""
import time
import datetime
import os
import sys
import subprocess
import numpy as np
import matplotlib.pyplot as plt
DEBUG = "DEBUG" in [arg.upper() for arg in sys.argv]
def log(msg: str):
if DEBUG:
print(msg)
else:
pass
def benchmark(service: str, test_file: str) -> tuple[int, float]:
calculator_service = os.path.join(
os.getcwd(), "services", service, "request-calculator"
)
manager_service = os.path.join(os.getcwd(), "services", service, "request-manager")
if service == "mpk-thread":
manager_service = os.path.join(os.getcwd(), "services", service)
log("[*] ==================================================")
log("[*] START")
log(f"[*] INFO: Running benchmark for {service} service, {test_file} test...")
# Run cargo clean to remove any previous build artifacts
log("[*] INFO: Cleaning up previous build artifacts...")
if service != "mpk-thread":
subprocess.call(
["cargo", "clean", "--manifest-path", f"{calculator_service}/Cargo.toml"],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
subprocess.call(
["cargo", "clean", "--manifest-path", f"{manager_service}/Cargo.toml"],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if service == "os-pipe":
subprocess.call(
["rm", "-f", "/tmp/request-pipe-request", "/tmp/request-pipe-response"],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
elif service == "unix-domain-sockets":
subprocess.call(
["rm", "-f", "/tmp/service.sock"],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
elif service == "shared-memory":
subprocess.call(
["rm", "-f", "/tmp/request.shm", "/tmp/response.shm"],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
# Build the calculator and manager services
log("[*] INFO: Building calculator and manager services...")
if service != "mpk-thread":
subprocess.call(
[
"cargo",
"build",
"--release",
"--manifest-path",
f"{calculator_service}/Cargo.toml",
],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
subprocess.call(
[
"cargo",
"build",
"--release",
"--manifest-path",
f"{manager_service}/Cargo.toml",
],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
# Start the calulator "server"
if service != "mpk-thread":
log("[*] INFO: Starting calculator server...")
p_calculator = subprocess.Popen(
[f"{calculator_service}/target/release/request-calculator", test_file],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
time.sleep(2)
# Start the timer
start = time.time()
# Start the manager "client"
if service != "mpk-thread":
log("[*] INFO: Starting manager client...")
p_manager = subprocess.Popen(
[f"{manager_service}/target/release/request-manager", test_file],
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
stdout = p_manager.communicate()[0].decode("utf-8")
else:
log("[*] INFO: Starting manager client...")
p_manager = subprocess.Popen(
[f"{manager_service}/target/release/mpk-thread", test_file],
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
stdout = p_manager.communicate()[0].decode("utf-8")
end = time.time()
# Kill the calculator "server"
if service != "mpk-thread":
log("[*] INFO: Killing calculator server...")
p_calculator.kill()
log("[*] END")
count = 0
if service == "mpk-thread":
count = int(stdout.strip().rsplit(": ", 1)[-1].strip('"'))
else:
count = int(stdout.strip().split(": ")[1])
log("[*] RESULTS: {} seconds".format(end - start))
log("[*] RESULTS: {} words".format(count))
return (count, end - start)
# TODO: Add averaging of times for each test count. Running each test N times.
N = 5
SERVICES = ["os-pipe", "unix-domain-sockets", "shared-memory", "mpk-thread"]
TESTS = sorted([test for test in os.listdir("tests") if test.endswith(".in")])
BENCHMARK_DIR = os.path.join(
os.getcwd(), "benchmark", datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
)
def benchmark_service(service: str) -> tuple[list[int], list[float]]:
counts = []
times = []
log(f"[*] INFO: Running benchmark for {service} service...")
for test in TESTS:
try:
count, time = benchmark(service, os.path.join(os.getcwd(), "tests", test))
counts.append(count)
times.append(time)
except Exception as e:
log(f"[*] ERROR: {e}")
return counts, times
def plot_benchmark(counts: list[int], times: list[float], service: str):
# Plot the counts vs. time
_, ax = plt.subplots()
# Make x-axis logarithmic
ax.set_xscale("log")
ax.plot(counts, times, "o-")
ax.set_xlabel("Number of Words")
ax.set_ylabel("Time (s)")
ax.set_title(f"{service} benchmark")
ax.grid()
os.makedirs(BENCHMARK_DIR, exist_ok=True)
# Save the plot
plt.savefig(f"{BENCHMARK_DIR}/{service}.png")
# Save the data
with open(f"{BENCHMARK_DIR}/{service}.txt", "w") as f:
for count, time in zip(counts, times):
f.write(f"{count} {time}\n")
results = {}
for service in SERVICES:
counts, times = benchmark_service(service)
results[service] = (counts, times)
plot_benchmark(counts, times, service)
# Plot the counts vs. time for all services
_, ax = plt.subplots()
# Make x-axis logarithmic
ax.set_xscale("log")
for service, (counts, times) in results.items():
ax.plot(counts, times, "o-", label=service)
ax.set_xlabel("Number of Words")
ax.set_ylabel("Time (s)")
ax.set_title("Benchmark")
ax.legend()
ax.grid()
# Save the plot
os.makedirs(BENCHMARK_DIR, exist_ok=True)
plt.savefig(f"{BENCHMARK_DIR}/overview.png")