Skip to content

Commit 83ddc1d

Browse files
authored
feat: Expose bar width flag. (#152)
This should have been wired up in the original PR. Not sure why this got left disconnected like this.
1 parent 28e872e commit 83ddc1d

2 files changed

Lines changed: 45 additions & 16 deletions

File tree

lib/reporter/chart.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,10 @@ const { analyze } = require("../utils/analyze.js");
99
* @param {number} total - The maximum value in the dataset (for scaling)
1010
* @param {number} samples - Number of samples collected
1111
* @param {string} metric - The metric being displayed (opsSec or totalTime)
12+
* @param {number} width - Length of the bar in characters
1213
* @param {string} [comment=""] - optional additional comment
13-
* @param {number} [length=25] - Length of the bar in characters
1414
*/
15-
function drawBar(
16-
label,
17-
value,
18-
total,
19-
samples,
20-
metric,
21-
comment = "",
22-
length = 25,
23-
) {
15+
function drawBar(label, value, total, samples, metric, width, comment = "") {
2416
let percentage;
2517
let displayedValue;
2618
let displayedMetric;
@@ -52,15 +44,15 @@ function drawBar(
5244
displayedMetric = "total time";
5345
}
5446

55-
const ratio = length * percentage;
47+
const ratio = width * percentage;
5648
const filledLength = Math.floor(ratio);
5749
const fraction = ratio % 1;
5850
const partial = fraction >= 0.5 ? "▌" : "";
5951

6052
const bar =
6153
"█".repeat(filledLength) +
6254
partial +
63-
"─".repeat(length - filledLength - partial.length);
55+
"─".repeat(width - filledLength - partial.length);
6456

6557
const displayedSamples = `${styleText(["yellow"], samples.toString().padStart(2))} samples`;
6658

@@ -99,7 +91,10 @@ function chartReport(results, options = { labelWidth: 45, printHeader: true }) {
9991
* @param {BenchmarkResult[]} results - Array of benchmark results
10092
* @param options {object} layout options
10193
*/
102-
function toChart(results, options = { labelWidth: 45, printHeader: true }) {
94+
function toChart(
95+
results,
96+
options = { labelWidth: 45, printHeader: true, barWidth: 25 },
97+
) {
10398
// Determine the primary metric and calculate max value for scaling
10499
const primaryMetric =
105100
results[0]?.opsSec !== undefined ? "opsSec" : "totalTime";
@@ -151,6 +146,7 @@ function toChart(results, options = { labelWidth: 45, printHeader: true }) {
151146
maxValue,
152147
result.histogram.samples,
153148
primaryMetric,
149+
options.barWidth ?? 25,
154150
comment,
155151
);
156152
}

test/reporter.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ describe("baseline comparisons", async (t) => {
515515
let output = "";
516516

517517
before(async () => {
518-
output = toChart(results, { labelWidth: 30 });
518+
output = toChart(results, {});
519519
});
520520

521521
it("should include a summary section", () => {
@@ -532,9 +532,42 @@ describe("baseline comparisons", async (t) => {
532532
assert.ok(summary.includes("slower"));
533533
});
534534

535-
it("can set a specific column width", () => {
535+
it("uses the default column width for the name", () => {
536536
const summary = output.split("Summary (vs. baseline):")[1];
537-
assert.ok(summary.includes("baseline-test ▏"));
537+
assert.ok(
538+
// 123456789012345678901234567890123456789012345 ▏
539+
summary.includes("baseline-test ▏"),
540+
);
541+
});
542+
543+
it("can adjust the display widths to suite", () => {
544+
const inputs = [
545+
{
546+
iterations: 1635480,
547+
histogram: {
548+
samples: 11,
549+
min: 301.5006542361793,
550+
max: 313.07250487172166,
551+
sampleData: [
552+
301.5006542361793, 301.5593469278305, 302.8870084803949,
553+
304.6617804001423, 304.71883159667146, 305.0352153017722,
554+
306.6694294422758, 306.9953128406366, 309.27860394347147,
555+
310.3016037436935, 313.07250487172166,
556+
],
557+
},
558+
name: "baseline-test",
559+
baseline: true,
560+
opsSec: 3268352.671656186,
561+
opsSecPerRun: [3268352.671656186],
562+
},
563+
];
564+
565+
output = toChart(inputs, { labelWidth: 20, barWidth: 10 });
566+
567+
const summary = output.split("Summary (vs. baseline):")[1];
568+
569+
// 12345678901234567890 ▏1234567890 ▏
570+
assert.ok(summary.includes("baseline-test ▏██████████▕ "));
538571
});
539572
});
540573
});

0 commit comments

Comments
 (0)