Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 9 additions & 13 deletions lib/reporter/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,15 +44,15 @@ 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 ? "▌" : "";

const bar =
"█".repeat(filledLength) +
partial +
"─".repeat(length - filledLength - partial.length);
"─".repeat(width - filledLength - partial.length);

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

Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -151,6 +146,7 @@ function toChart(results, options = { labelWidth: 45, printHeader: true }) {
maxValue,
result.histogram.samples,
primaryMetric,
options.barWidth ?? 25,
comment,
);
}
Expand Down
39 changes: 36 additions & 3 deletions test/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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 ▏██████████▕ "));
});
});
});
Expand Down
Loading