Skip to content

Commit 4c65557

Browse files
authored
feat: Show bar chart with 2% resolution by using partial width box characters. (#97)
example: constructors ⇒ new Registry() ⇒ prom-client@latest | █████████████████████████ | 15,259,633 ops/sec | 11 samples ⇒ prom-client@trunk | ██████████████████████▌── | 14,016,409 ops/sec | 11 samples ⇒ prom-client@current | ██████████████████████▌── | 13,889,989 ops/sec | 11 samples constructors ⇒ new Counter() ⇒ prom-client@latest | █████████████████████──── | 1,081,591 ops/sec | 11 samples ⇒ prom-client@trunk | █████████████████████████ | 1,281,427 ops/sec | 10 samples ⇒ prom-client@current | ████████████████████████▌ | 1,272,858 ops/sec | 10 samples
1 parent 5ec0319 commit 4c65557

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

lib/reporter/chart.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
const { platform, arch, cpus, totalmem } = require("node:os");
22
const { styleText } = require("../utils/styleText");
33

4-
const formatter = Intl.NumberFormat(undefined, {
5-
notation: "standard",
6-
maximumFractionDigits: 2,
7-
});
8-
9-
const timer = Intl.NumberFormat(undefined, {
10-
minimumFractionDigits: 3,
11-
maximumFractionDigits: 3,
12-
});
13-
144
/**
155
* Draws a bar chart representation of a benchmark result
166
* @param {string} label - The label for the bar (benchmark name)
177
* @param {number} value - The value to display (operations per second or time per operation)
188
* @param {number} total - The maximum value in the dataset (for scaling)
199
* @param {number} samples - Number of samples collected
2010
* @param {string} metric - The metric being displayed (opsSec or totalTime)
21-
* @param {number} [length=30] - Length of the bar in characters
11+
* @param {number} [length=25] - Length of the bar in characters
2212
*/
23-
function drawBar(label, value, total, samples, metric, length = 30) {
13+
function drawBar(label, value, total, samples, metric, length = 25) {
2414
let percentage;
2515
let displayedValue;
2616
let displayedMetric;
@@ -51,8 +41,20 @@ function drawBar(label, value, total, samples, metric, length = 30) {
5141
displayedMetric = "total time";
5242
}
5343

54-
const filledLength = Math.round(length * percentage);
55-
const bar = "█".repeat(filledLength) + "-".repeat(length - filledLength);
44+
const ratio = length * percentage;
45+
const filledLength = Math.floor(ratio);
46+
const fraction = ratio % 1;
47+
48+
let partial = "";
49+
50+
if (fraction >= 0.5) {
51+
partial = "▌";
52+
}
53+
54+
const bar =
55+
"█".repeat(filledLength) +
56+
partial +
57+
"─".repeat(length - filledLength - partial.length);
5658

5759
const displayedSamples = styleText(["yellow"], samples.toString());
5860

@@ -61,6 +63,16 @@ function drawBar(label, value, total, samples, metric, length = 30) {
6163
);
6264
}
6365

66+
const formatter = Intl.NumberFormat(undefined, {
67+
notation: "standard",
68+
maximumFractionDigits: 2,
69+
});
70+
71+
const timer = Intl.NumberFormat(undefined, {
72+
minimumFractionDigits: 3,
73+
maximumFractionDigits: 3,
74+
});
75+
6476
const environment = {
6577
nodeVersion: `Node.js version: ${process.version}`,
6678
platform: `${platform()} ${arch()}`,

0 commit comments

Comments
 (0)