From 3929129c2700e5c2630378630f38a16a97a5e970 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 8 Dec 2025 13:30:05 -0800 Subject: [PATCH] feat: Expose bar width flag. This should have been wired up in the original PR. Not sure why this got left disconnected like this. --- lib/reporter/chart.js | 22 +++++++++------------- test/reporter.js | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/lib/reporter/chart.js b/lib/reporter/chart.js index 4af6dc4..80f5023 100644 --- a/lib/reporter/chart.js +++ b/lib/reporter/chart.js @@ -9,18 +9,10 @@ const { analyze } = require("../utils/analyze.js"); * @param {number} total - The maximum value in the dataset (for scaling) * @param {number} samples - Number of samples collected * @param {string} metric - The metric being displayed (opsSec or totalTime) + * @param {number} width - Length of the bar in characters * @param {string} [comment=""] - optional additional comment - * @param {number} [length=25] - Length of the bar in characters */ -function drawBar( - label, - value, - total, - samples, - metric, - comment = "", - length = 25, -) { +function drawBar(label, value, total, samples, metric, width, comment = "") { let percentage; let displayedValue; let displayedMetric; @@ -52,7 +44,7 @@ function drawBar( displayedMetric = "total time"; } - const ratio = length * percentage; + const ratio = width * percentage; const filledLength = Math.floor(ratio); const fraction = ratio % 1; const partial = fraction >= 0.5 ? "▌" : ""; @@ -60,7 +52,7 @@ function drawBar( const bar = "█".repeat(filledLength) + partial + - "─".repeat(length - filledLength - partial.length); + "─".repeat(width - filledLength - partial.length); const displayedSamples = `${styleText(["yellow"], samples.toString().padStart(2))} samples`; @@ -99,7 +91,10 @@ function chartReport(results, options = { labelWidth: 45, printHeader: true }) { * @param {BenchmarkResult[]} results - Array of benchmark results * @param options {object} layout options */ -function toChart(results, options = { labelWidth: 45, printHeader: true }) { +function toChart( + results, + options = { labelWidth: 45, printHeader: true, barWidth: 25 }, +) { // Determine the primary metric and calculate max value for scaling const primaryMetric = results[0]?.opsSec !== undefined ? "opsSec" : "totalTime"; @@ -151,6 +146,7 @@ function toChart(results, options = { labelWidth: 45, printHeader: true }) { maxValue, result.histogram.samples, primaryMetric, + options.barWidth ?? 25, comment, ); } diff --git a/test/reporter.js b/test/reporter.js index 7458ae3..a64efde 100644 --- a/test/reporter.js +++ b/test/reporter.js @@ -515,7 +515,7 @@ describe("baseline comparisons", async (t) => { let output = ""; before(async () => { - output = toChart(results, { labelWidth: 30 }); + output = toChart(results, {}); }); it("should include a summary section", () => { @@ -532,9 +532,42 @@ describe("baseline comparisons", async (t) => { assert.ok(summary.includes("slower")); }); - it("can set a specific column width", () => { + it("uses the default column width for the name", () => { const summary = output.split("Summary (vs. baseline):")[1]; - assert.ok(summary.includes("baseline-test ▏")); + assert.ok( + // 123456789012345678901234567890123456789012345 ▏ + summary.includes("baseline-test ▏"), + ); + }); + + it("can adjust the display widths to suite", () => { + const inputs = [ + { + iterations: 1635480, + histogram: { + samples: 11, + min: 301.5006542361793, + max: 313.07250487172166, + sampleData: [ + 301.5006542361793, 301.5593469278305, 302.8870084803949, + 304.6617804001423, 304.71883159667146, 305.0352153017722, + 306.6694294422758, 306.9953128406366, 309.27860394347147, + 310.3016037436935, 313.07250487172166, + ], + }, + name: "baseline-test", + baseline: true, + opsSec: 3268352.671656186, + opsSecPerRun: [3268352.671656186], + }, + ]; + + output = toChart(inputs, { labelWidth: 20, barWidth: 10 }); + + const summary = output.split("Summary (vs. baseline):")[1]; + + // 12345678901234567890 ▏1234567890 ▏ + assert.ok(summary.includes("baseline-test ▏██████████▕ ")); }); }); });