-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_html.py
More file actions
418 lines (366 loc) · 12 KB
/
parse_html.py
File metadata and controls
418 lines (366 loc) · 12 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import sys
import os
import re
import csv
import json
import platform
import psutil
import subprocess
from datetime import datetime, timezone
# Parses wrk output files into a CSV
RESULTS_DIR = "results/raw"
OUTPUT_CSV = "results/results_summary.csv"
OUTPUT_HTML = "results/results_dashboard.html"
# Regex to extract key metrics from wrk output
regex = {
"requests_per_sec": re.compile(r"Requests/sec:\s+([\d.]+)"),
"latency_avg": re.compile(r"Latency\s+([\d.]+)(ms|s|us)"),
"transfer_per_sec": re.compile(r"Transfer/sec:\s+([\d.]+)(\wB)"),
"non_2xx": re.compile(r"Non-2xx or 3xx responses: (\d+)")
}
def normalize_latency(val, unit):
if unit == "s":
return float(val) * 1000
if unit == "ms":
return float(val)
if unit == "us":
return float(val) / 1000
return float(val)
def normalize_transfer(val, unit):
val = float(val)
if unit == "kB":
return val
if unit == "MB":
return val * 1024
if unit == "GB":
return val * 1024 * 1024
return val
def get_os_name():
try:
import distro
except ImportError:
distro = None # We'll handle missing distro gracefully
if platform.system() == "Darwin":
mac_ver = platform.mac_ver()[0] # e.g., '13.4.1'
os_info = f"macOS {mac_ver}"
elif platform.system() == "Linux":
if distro:
name = distro.name(pretty=True) # 'Ubuntu 24.04 LTS'
if not name:
# fallback if pretty name missing
name = f"{distro.id()} {distro.version()}"
os_info = name
else:
# If distro not installed, fallback to platform info
os_info = f"Linux {platform.release()}"
else:
os_info = f"{platform.system()} {platform.release()}"
return os_info
def get_cpu_name():
system = platform.system()
cpu_name = "Unknown CPU"
if system == "Darwin":
try:
cpu_name = subprocess.check_output(
["sysctl", "-n", "machdep.cpu.brand_string"],
text=True
).strip()
except Exception:
pass
elif system == "Linux":
try:
with open("/proc/cpuinfo") as f:
for line in f:
if "model name" in line:
cpu_name = line.split(":", 1)[1].strip()
break
except Exception:
pass
return cpu_name
rows = []
for filename in os.listdir(RESULTS_DIR):
if not filename.endswith(".txt"): continue
framework = filename.replace(".txt", "")
with open(os.path.join(RESULTS_DIR, filename)) as f:
content = f.read()
rps_match = regex["requests_per_sec"].search(content)
latency_match = regex["latency_avg"].search(content)
transfer_match = regex["transfer_per_sec"].search(content)
non2xx_match = regex["non_2xx"].search(content)
rps = float(rps_match.group(1)) if rps_match else 0
latency = normalize_latency(latency_match.group(1), latency_match.group(2)) if latency_match else 0
transfer = normalize_transfer(transfer_match.group(1), transfer_match.group(2)) if transfer_match else 0
errors = int(non2xx_match.group(1)) if non2xx_match else 0
rows.append({
"framework": framework,
"requests_per_sec": rps,
"avg_latency_ms": latency,
"transfer_kb_sec": transfer,
"non_2xx": errors
})
# Write to CSV
with open(OUTPUT_CSV, "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=rows[0].keys())
writer.writeheader()
writer.writerows(rows)
print(f"✅ Results written to {OUTPUT_CSV}")
# Embed the data directly into an HTML template
TEMPLATE_HTML = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Benchmark Results</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
font-family: 'Segoe UI', sans-serif;
background: #f8f9fa;
color: #333;
margin: 2rem;
}
h1 {
text-align: center;
margin-bottom: 2rem;
}
canvas {
display: block;
width: 100%;
max-width: 1200px;
max-height: 500px;
height: 300px;
margin: 3rem auto;
}
h2 {
text-align: center;
margin-top: 3rem;
color: #444;
}
table {
margin: 3rem auto;
border-collapse: collapse;
width: 100%;
max-width: 1000px;
font-size: 1rem;
background: white;
box-shadow: 0 0 10px rgba(0,0,0,0.05);
}
th, td {
padding: 12px 16px;
border: 1px solid #ddd;
text-align: center;
}
th {
background-color: #f0f0f0;
font-weight: bold;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
#envTable {
margin: 3rem auto;
border-collapse: collapse;
width: 100%;
max-width: 800px;
font-size: 1rem;
background: white;
box-shadow: 0 0 10px rgba(0,0,0,0.05);
}
#envTable th, #envTable td {
padding: 10px 14px;
border: 1px solid #ccc;
text-align: left;
}
#envTable th {
background-color: #f0f0f0;
width: 200px;
}
footer {
text-align: center;
margin-top: 80px;
font-size: 0.9em;
color: #888;
}
</style>
</head>
<body>
<h1>🚀 Benchmark Results Dashboard</h1>
<h2>📈 Requests per Second</h2>
<canvas id="requestsChart"></canvas>
<h2>⏱️ Average Latency (ms)</h2>
<canvas id="latencyChart"></canvas>
<h2>📤 Transfer per Second (kB)</h2>
<canvas id="transferChart"></canvas>
<h2>📊 Full Results Table (Sorted by Requests/sec)</h2>
<table id="resultsTable">
<thead>
<tr>
<th>Framework</th>
<th>Requests/sec</th>
<th>Avg Latency (ms)</th>
<th>Transfer/sec (kB)</th>
</tr>
</thead>
<tbody></tbody>
</table>
<h2>🛠 Benchmark Environment</h2>
<table id="envTable">
<tbody>
<tr><th>OS</th><td id="os">__OS__</td></tr>
<tr><th>CPU</th><td id="cpu">__CPU__</td></tr>
<tr><th>RAM</th><td id="ram">__RAM__</td></tr>
<tr><th>Date</th><td id="date">__DATE__</td></tr>
<tr><th>Benchmark Command</th><td><code>wrk -t__CORES__ -c1000 -d60s -s post.lua</code></td></tr>
<tr><th>Python Version</th><td id="date">__PYVER__</td></tr>
<tr><th>Node Version</th><td id="date">__NODEVER__</td></tr>
<tr><th>Bun Version</th><td id="date">__BUNVER__</td></tr>
<tr><th>Deno Version</th><td id="date">__DENOVER__</td></tr>
<tr><th>Go Version</th><td id="date">__GOVER__</td></tr>
<tr><th>PHP Version</th><td id="date">__PHPVER__</td></tr>
<tr><th>Java Version</th><td id="date">__JAVAVER__</td></tr>
<tr><th>Dotnet Version</th><td id="date">__DOTNETVER__</td></tr>
<tr><th>Ruby Version</th><td id="date">__RUBYVER__</td></tr>
</tbody>
</table>
<footer>
Results generated using <a href="https://github.com/Drarox/Backend-Benchmark" target="_blank" style="color: #555; text-decoration: none;">
Drarox/Backend-Benchmark
</a>
</footer>
<script>
const resultsData = __DATA__;
function createChart(ctxId, label, data, color) {
const ctx = document.getElementById(ctxId).getContext('2d');
return new Chart(ctx, {
type: 'bar',
data: {
labels: data.map(d => d.framework),
datasets: [{
label,
data: data.map(d => d.value),
backgroundColor: color,
borderColor: '#111',
borderWidth: 1
}]
},
options: {
responsive: true,
plugins: {
legend: { display: false },
tooltip: { mode: 'index', intersect: false }
},
scales: {
y: { beginAtZero: true }
}
}
});
}
// Sort individually for each chart
const rpsSorted = [...resultsData].sort((a, b) => a.requests_per_sec - b.requests_per_sec);
const latencySorted = [...resultsData].sort((a, b) => b.avg_latency_ms - a.avg_latency_ms);
const transferSorted = [...resultsData].sort((a, b) => a.transfer_kb_sec - b.transfer_kb_sec);
createChart('requestsChart', 'Requests/sec', rpsSorted.map(d => ({ framework: d.framework, value: d.requests_per_sec })), '#0d6efd');
createChart('latencyChart', 'Average Latency (ms)', latencySorted.map(d => ({ framework: d.framework, value: d.avg_latency_ms })), '#dc3545');
createChart('transferChart', 'Transfer/sec (kB)', transferSorted.map(d => ({ framework: d.framework, value: d.transfer_kb_sec })), '#198754');
// Tabel view
const sortedData = [...resultsData].sort((a, b) => b.requests_per_sec - a.requests_per_sec);
const tableBody = document.querySelector("#resultsTable tbody");
sortedData.forEach(row => {
const tr = document.createElement("tr");
tr.innerHTML = `
<td>${row.framework}</td>
<td>${row.requests_per_sec.toFixed(2)}</td>
<td>${row.avg_latency_ms.toFixed(2)}</td>
<td>${row.transfer_kb_sec.toFixed(2)}</td>
`;
tableBody.appendChild(tr);
});
</script>
</body>
</html>
"""
# Inject JSON directly
html_with_data = TEMPLATE_HTML.replace("__DATA__", json.dumps(rows, indent=2))
# Inject environment
date_info = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
# Sytem infos
os_info = get_os_name()
cpu_name = get_cpu_name()
cpu_cores = psutil.cpu_count(logical=False) or psutil.cpu_count(logical=True) or "Unknown"
cpu_info = f"{cpu_name} ({cpu_cores} cores)"
ram_info = f"{round(psutil.virtual_memory().total / (1024**3))} GB"
# Python version
pyver = sys.version.split()[0] # e.g. '3.11.4'
# Node.js version
try:
nodever = subprocess.check_output(["node", "--version"], text=True).strip()
except Exception:
nodever = "Node not found"
# Bun version
try:
bunver = subprocess.check_output(["bun", "--version"], text=True).strip()
except Exception:
bunver = "Bun not found"
# Deno version
try:
deno_full = subprocess.check_output(["deno", "--version"], text=True).strip().split("\n")[0]
# deno_full example: "deno 1.35.2 (release, x86_64-unknown-linux-gnu)"
denover = " ".join(deno_full.split()[:2]) # keeps "deno 1.35.2"
except Exception:
denover = "Deno not found"
# Go version
try:
gover = subprocess.check_output(["go", "version"], text=True).strip()
# go version output example: 'go version go1.21.0 linux/amd64'
# Extract just 'go1.21.0'
gover = gover.split()[2]
except Exception:
gover = "Go not found"
# PHP version
try:
phpver = subprocess.check_output(["php", "--version"], text=True).strip()
phpver = phpver.split()[1]
except Exception:
phpver = "PHP not found"
# Java version
try:
java_ver_output = subprocess.check_output(["java", "-version"], stderr=subprocess.STDOUT, text=True).strip()
# Updated regex to match versions like "25" or "11.0.28"
match = re.search(r'\"(\d+(\.\d+)*)(\"|\s)', java_ver_output)
if match:
javaver = match.group(1)
else:
javaver = "Java version not found"
except Exception:
javaver = "Java not found"
# Dotnet version
try:
dotnetver = subprocess.check_output(["dotnet", "--version"], stderr=subprocess.STDOUT, text=True).strip()
except Exception:
dotnetver = "Dotnet not found"
# Ruby version
try:
rubyver = subprocess.check_output(["ruby", "-v"], text=True).strip()
rubyver = rubyver.split()[1]
except Exception:
rubyver = "Ruby not found"
html_with_data = html_with_data \
.replace("__OS__", os_info) \
.replace("__CPU__", cpu_info) \
.replace("__CORES__", str(cpu_cores)) \
.replace("__RAM__", ram_info) \
.replace("__DATE__", date_info) \
.replace("__PYVER__", pyver) \
.replace("__NODEVER__", nodever) \
.replace("__BUNVER__", bunver) \
.replace("__DENOVER__", denover) \
.replace("__GOVER__", gover) \
.replace("__PHPVER__", phpver) \
.replace("__JAVAVER__", javaver) \
.replace("__DOTNETVER__", dotnetver) \
.replace("__RUBYVER__", rubyver)
# Write to standalone HTML
with open(OUTPUT_HTML, "w") as f:
f.write(html_with_data)
print(f"✅ Self-contained HTML dashboard written to {OUTPUT_HTML}")