Skip to content

Commit 91082b6

Browse files
feat(reporter): polish chart output (#40)
* feat(reporter): polish output * fix * fix
1 parent 5263cc6 commit 91082b6

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

lib/reporter/chart.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
const { platform, arch, cpus, totalmem } = require("node:os");
2+
const { styleText } = require("../utils/styleText");
23

34
const formatter = Intl.NumberFormat(undefined, {
45
notation: "standard",
56
maximumFractionDigits: 2,
67
});
78

8-
function drawBar(label, value, total, length = 30) {
9+
function drawBar(label, value, total, samples, length = 30) {
910
const percentage = value / total;
1011
const filledLength = Math.round(length * percentage);
1112
const bar = "█".repeat(filledLength) + "-".repeat(length - filledLength);
1213

1314
const opsSecReported = value < 100 ? value.toFixed(2) : value.toFixed(0);
15+
const displayedOpsSec = styleText(
16+
["yellow"],
17+
formatter.format(opsSecReported),
18+
);
19+
const displayedSamples = styleText(["yellow"], samples.toString());
20+
1421
process.stdout.write(
15-
`${label.padEnd(45)} | ${bar} | ${formatter.format(opsSecReported)} ops/sec\n`,
22+
`${label.padEnd(45)} | ${bar} | ${displayedOpsSec} ops/sec | ${displayedSamples} samples\n`,
1623
);
1724
}
1825

@@ -29,7 +36,7 @@ function chartReport(results) {
2936
`CPU Cores: ${environment.hardware}\n\n`,
3037
);
3138
for (const result of results) {
32-
drawBar(result.name, result.opsSec, maxOpsSec);
39+
drawBar(result.name, result.opsSec, maxOpsSec, result.histogram.samples);
3340
}
3441
}
3542

lib/reporter/text.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
const util = require("node:util");
2-
3-
const styleText =
4-
typeof util.styleText === "function"
5-
? util.styleText
6-
: (_style, text) => text;
7-
1+
const { styleText } = require("../utils/styleText");
82
const { timer } = require("../clock");
93

104
const formatter = Intl.NumberFormat(undefined, {

lib/utils/styleText.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const util = require("node:util");
2+
3+
module.exports.styleText =
4+
typeof util.styleText === "function"
5+
? util.styleText
6+
: (_style, text) => text;

test/reporter.js

+9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ describe("chartReport outputs benchmark results as a bar chart", async (t) => {
5151
it("should include ops/sec", () => {
5252
assert.ok(output.includes("ops/sec"));
5353
});
54+
55+
it("should include benchmark names", () => {
56+
assert.ok(output.includes("single with matcher"));
57+
assert.ok(output.includes("multiple replaces"));
58+
});
59+
60+
it("should include sample count", () => {
61+
assert.ok(output.includes("samples"));
62+
});
5463
});
5564

5665
describe("htmlReport should create a file", async (t) => {

0 commit comments

Comments
 (0)