Generate HTML report

This commit is contained in:
Joseph Ferano 2025-07-31 16:55:24 +07:00
parent 2debe2aee1
commit 1b00d59b55
2 changed files with 94 additions and 5 deletions

View File

@ -6,6 +6,9 @@ import json
import time
import re
import pprint
import subprocess
from jinja2 import Template
total_start = time.time()
@ -100,11 +103,6 @@ try:
container.reload() # Refresh container status
print(f"""Performance:
Max CPU: {max_cpu_percent:.2f}%
Max Memory: {max_memory_mb:.2f}MB
Total Transfer: {(total_rx_bytes + total_tx_bytes) / 1024} KBs""")
container.wait()
print("Container logs:")
@ -119,6 +117,41 @@ try:
print(f"\t{line}")
container.remove()
metrics = [
{"name": "Max CPU", "value": f"{max_cpu_percent:.2f}", "unit": "%"},
{"name": "Max Memory", "value": f"{max_memory_mb:.2f}", "unit": "MB"},
{"name": "Total Transfer", "value": f"{(total_rx_bytes + total_tx_bytes) / 1024:.0f}", "unit": "KB"},
{"name": "Cold Start Time", "value": f"{(coldstart_stop_time - container_start_time):.3f}", "unit": "seconds"},
{"name": "Time to Response", "value": f"{(scrape_stop_time - scrape_start_time):.3f}", "unit": "seconds"},
{"name": "Total Runtime", "value": f"{(time.time() - total_start):.3f}", "unit": "seconds"}
]
with open('output/output.txt', 'r', encoding='utf-8') as f:
text_content = f.read()
with open('report.html', 'r') as f:
template = Template(f.read())
rendered_html = template.render(metrics=metrics, text_content=text_content)
with open('output/report.html', 'w') as f:
f.write(rendered_html)
# Try running `open` to launch the html file automatically, fail silently
try:
subprocess.run(['open', 'output/report.html'])
print("Report opened in browser")
except (subprocess.CalledProcessError, FileNotFoundError):
print("what happened")
pass
print(f"""Performance:
Max CPU: {max_cpu_percent:.2f}%
Max Memory: {max_memory_mb:.2f}MB
Total Transfer: {(total_rx_bytes + total_tx_bytes) / 1024} KBs""")
print(f"Cold Start Time: {(coldstart_stop_time - container_start_time):.3f} seconds")
print(f"Time to Response: {(scrape_stop_time - scrape_start_time):.3f} seconds")
print(f"Completed everything in: {(time.time() - total_start):.3f} seconds")

56
report.html Normal file
View File

@ -0,0 +1,56 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Performance Report</title>
<link rel="stylesheet" href="https://unpkg.com/sakura.css/css/sakura.css" type="text/css">
<style>
.metrics-table {
margin-bottom: 2rem;
}
.text-output {
width: 100%;
min-height: 400px;
font-family: monospace;
font-size: 14px;
padding: 1rem;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
overflow-y: auto;
}
</style>
</head>
<body>
<h1>Performance Report</h1>
<div class="metrics-table">
<h2>Performance Metrics</h2>
<table>
<thead>
<tr>
<th>Metric</th>
<th>Value</th>
<th>Unit</th>
</tr>
</thead>
<tbody>
{% for metric in metrics %}
<tr>
<td>{{ metric.name }}</td>
<td>{{ metric.value }}</td>
<td>{{ metric.unit }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="text-section">
<h2>Detailed Output</h2>
<textarea class="text-output" readonly>{{ text_content }}</textarea>
</div>
</body>
</html>