Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(html): improve htmlReport UX #60

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/reporter/html.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { platform, arch, cpus, totalmem } = require("node:os");
const fs = require("node:fs");
const path = require("node:path");

Expand All @@ -13,8 +14,8 @@ const opsToDuration = (maxOps, ops, scalingFactor = 10) => {

const generateHTML = (template, durations) => {
let css = "";
let circleDiv = "";
let labelDiv = "";
let circleDiv = "";
let position = 20;
const colors = [
"blue",
Expand All @@ -40,9 +41,9 @@ const generateHTML = (template, durations) => {
}
`;
circleDiv += `
<div id="label-${d.name}" class="label">${d.name}

(${d.opsSecFormatted} ops/sec)</div>
<div id="label-${d.name}" class="label">
${d.name}(<span class="number">${d.opsSecFormatted}</span> ops/sec)
</div>
`;
labelDiv += `
<div id="circle-${d.name}" class="circle"></div>
Expand All @@ -51,12 +52,17 @@ const generateHTML = (template, durations) => {
position += 80;
}

const environmentDiv = `<p>Node.js version: <span class="number">${process.version}</span></p>
<p>Platform: <span class="number">${platform()} ${arch()}</span></p>
<p>CPU Cores: <span class="number">${cpus().length}</span> vCPUs | <span class="number">${(totalmem() / 1024 ** 3).toFixed(1)}GB Mem</span></p>`;

return template
.replaceAll("{{DURATIONS}}", JSON.stringify(durations))
.replaceAll("{{CONTAINER_HEIGHT}}", `${durations.length * 100}px;`)
.replaceAll("{{CSS}}", css)
.replaceAll("{{ENVIRONMENT_DIV}}", environmentDiv)
.replaceAll("{{LABEL_DIV}}", labelDiv)
.replaceAll("{{CIRCLE_DIV}}", circleDiv)
.replaceAll("{{CONTAINER_HEIGHT}}", `${durations.length * 100}px;`);
.replaceAll("{{DURATIONS}}", JSON.stringify(durations));
};

const templatePath = path.join(__dirname, "template.html");
Expand All @@ -73,7 +79,7 @@ function htmlReport(results) {

const htmlContent = generateHTML(template, durations);
fs.writeFileSync("result.html", htmlContent, "utf8");
process.stdout.write("HTML file has been generated: result.html");
process.stdout.write("HTML file has been generated: result.html\n");
}

module.exports = {
Expand Down
55 changes: 53 additions & 2 deletions lib/reporter/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: #f4f4f4;
font-family: Arial, sans-serif;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
.env {
width: 400px;
}

.container {
Expand All @@ -38,16 +42,61 @@
position: absolute;
}

.number {
font-family: 'Times New Roman', Times, serif;
}

.made-with {
position: fixed;
bottom: 0;
right: 0;
padding: 10px;
font-size: 12px;
}
.made-with a {
color: #2C682C;
}
.made-with a:hover {
text-decoration: none;
}

{{CSS}}

/* Dark theme */
@media (prefers-color-scheme: dark) {
body {
background: #333;
color: #fff;
}

.container {
border-color: #fff;
background: #444;
}

.made-with a {
color: #84BA64;
}
}
</style>
</head>
<body>
<div class="env">
{{ENVIRONMENT_DIV}}
</div>

<div class="container">
{{LABEL_DIV}}

{{CIRCLE_DIV}}
</div>

<div class="made-with">
<p>Benchmark done with <a href="https://github.com/RafaelGSS/bench-node">bench-node</a></p>
</div>

</div>

<script>
const durations = {{DURATIONS}};

Expand All @@ -73,7 +122,9 @@
element.style.transform = `translateX(${circle.position}px)`;
});

requestAnimationFrame(update);
setTimeout(() => {
requestAnimationFrame(update);
}, 1000 / 120); // "60 FPS"
};

update();
Expand Down
Loading