Skip to content

Commit 315d551

Browse files
feat: Allow for configurable column width for the chart output. (#104)
* fix: Linter is complaining about coverage output on second run. * feat: Allow for configurable column width for the chart output. For my use case, the labels are just not that wide, and being able to see the full report in Github would be good. * Update lib/reporter/chart.js Co-authored-by: Rafael Gonzaga <rafael.nunu@hotmail.com> --------- Co-authored-by: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 39d81dc commit 315d551

5 files changed

Lines changed: 23 additions & 12 deletions

File tree

biome.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
"enabled": true
2121
},
2222
"files": {
23-
"ignore": ["examples/*", "test/plugin-api-doc.js", "package.json"]
23+
"ignore": [
24+
"examples/*",
25+
"coverage/*",
26+
"test/plugin-api-doc.js",
27+
"package.json"
28+
]
2429
}
2530
}

lib/reporter/chart.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function drawBar(
6767
"─".repeat(length - filledLength - partial.length);
6868

6969
const displayedSamples = `${styleText(["yellow"], samples.toString().padStart(2))} samples`;
70-
const line = `${label.padEnd(45)} | ${bar} | ${displayedValue} ${displayedMetric} | ${displayedSamples} ${comment}\n`;
70+
const line = `${label} | ${bar} | ${displayedValue} ${displayedMetric} | ${displayedSamples} ${comment}\n`;
7171

7272
process.stdout.write(line);
7373
}
@@ -92,14 +92,15 @@ const environment = {
9292
* Generates a chart visualization of benchmark results in the console
9393
* Displays system information and a bar chart of operations per second or time per operation
9494
* @param {import('../report').BenchmarkResult[]} results - Array of benchmark results
95+
* @param options {object} layout options
9596
*/
96-
function chartReport(results, options) {
97+
function chartReport(results, options = { labelWidth: 45, printHeader: true }) {
9798
// Determine the primary metric and calculate max value for scaling
9899
const primaryMetric =
99100
results[0]?.opsSec !== undefined ? "opsSec" : "totalTime";
100101
const maxValue = Math.max(...results.map((b) => b[primaryMetric]));
101102

102-
if (options?.printHeader) {
103+
if (options.printHeader) {
103104
process.stdout.write(
104105
`${environment.nodeVersion}\n` +
105106
`Platform: ${environment.platform}\n` +
@@ -128,8 +129,10 @@ function chartReport(results, options) {
128129
}
129130
}
130131

132+
const columnWidth = options.labelWidth ?? 45;
133+
131134
drawBar(
132-
result.name,
135+
result.name.padEnd(columnWidth),
133136
result[primaryMetric],
134137
maxValue,
135138
result.histogram.samples,

lib/reporter/pretty.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,9 @@ function prettyReport(results) {
5454
const maxNameLength = Math.max(...sortedResults.map((r) => r.name.length));
5555

5656
for (const result of sortedResults) {
57-
const namePart = ` ${result.name}`;
57+
const namePart = ` ${result.name.padEnd(maxNameLength)} `;
5858
process.stdout.write(namePart);
5959

60-
const padding = maxNameLength - result.name.length + 2;
61-
process.stdout.write(" ".repeat(padding));
62-
6360
if (result.baseline) {
6461
process.stdout.write(styleText("magenta", "(baseline)"));
6562
} else if (!result.comparison.startsWith("-")) {

lib/reporter/text.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ const formatter = Intl.NumberFormat(undefined, {
1111
* Generates a text report of benchmark results, displaying each benchmark's name,
1212
* operations per second, number of samples, plugin results, and min/max timings
1313
* @param {import('../report').BenchmarkResult[]} results - Array of benchmark results
14+
* @param options {object} layout options
1415
*/
15-
function textReport(results) {
16+
function textReport(results, options = {}) {
1617
for (const result of results) {
17-
process.stdout.write(result.name.padEnd(45));
18+
process.stdout.write(result.name.padEnd(options.labelWidth ?? 45));
1819
process.stdout.write(" x ");
1920

2021
if (result.opsSec !== undefined) {

test/reporter.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ describe("baseline comparisons", async (t) => {
430430
output += data;
431431
};
432432

433-
chartReport(results);
433+
chartReport(results, { labelWidth: 30 });
434434
process.stdout.write = originalStdoutWrite;
435435
});
436436

@@ -447,6 +447,11 @@ describe("baseline comparisons", async (t) => {
447447
const summary = output.split("Summary (vs. baseline):")[1];
448448
assert.ok(summary.includes("slower"));
449449
});
450+
451+
it("can set a specific column width", () => {
452+
const summary = output.split("Summary (vs. baseline):")[1];
453+
assert.ok(summary.includes("baseline-test |"));
454+
});
450455
});
451456
});
452457

0 commit comments

Comments
 (0)