Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create csv reporter #38

Merged
merged 4 commits into from
Jan 8, 2025
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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,35 @@ const suite = new Suite({
});
```

### CSV Reporter

The `csvReport` plugin generates benchmark results in CSV format.
It includes columns for key performance metrics like `ops/sec`, `samples`, `min` and `max` times,
as well as any reporter data provided by your plugins.

Example output:

```csv
name,ops/sec,samples,plugins,min,max
Using delete property,"7,732,517",10,v8-never-optimize=true,127.91ns,129.95ns
Using delete property (proto: null),"24,636,631",10,v8-never-optimize=true,39.57ns,40.91ns
Using delete property (cached proto: null),"7,497,893",11,v8-never-optimize=true,132.25ns,134.89ns
Using undefined assignment,"132,093,600",11,v8-never-optimize=true,7.53ns,7.64ns
Using undefined assignment (proto: null),"28,231,374",9,v8-never-optimize=true,35.27ns,35.42ns
Using undefined property (cached proto: null),"60,843,193",10,v8-never-optimize=true,16.24ns,16.65ns
[Managed] Using undefined property (cached proto: null),"35,394,060",10,v8-never-optimize=true,27.90ns,28.54ns
```

**Usage:**

```cjs
const { Suite, csvReport } = require('bench-node');

const suite = new Suite({
reporter: csvReport,
});
```

### Custom Reporter

Customize data reporting by providing a `reporter` function when creating the `Suite`:
Expand Down
9 changes: 8 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ const { Worker } = require("node:worker_threads");
const { types } = require("node:util");
const path = require("node:path");

const { textReport, chartReport, htmlReport, jsonReport } = require("./report");
const {
textReport,
chartReport,
htmlReport,
jsonReport,
csvReport,
} = require("./report");
const {
getInitialIterations,
runBenchmark,
Expand Down Expand Up @@ -229,4 +235,5 @@ module.exports = {
textReport,
htmlReport,
jsonReport,
csvReport,
};
2 changes: 2 additions & 0 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const { textReport } = require("./reporter/text");
const { chartReport } = require("./reporter/chart");
const { htmlReport } = require("./reporter/html");
const { jsonReport } = require("./reporter/json");
const { csvReport } = require("./reporter/csv");

module.exports = {
chartReport,
textReport,
htmlReport,
jsonReport,
csvReport,
};
36 changes: 36 additions & 0 deletions lib/reporter/csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { timer } = require("../clock");

const formatter = Intl.NumberFormat(undefined, {
notation: "standard",
maximumFractionDigits: 2,
});

function csvReport(results) {
process.stdout.write("name,ops/sec,samples,plugins,min,max\n");

for (const result of results) {
const opsSecReported =
result.opsSec < 100 ? result.opsSec.toFixed(2) : result.opsSec.toFixed(0);

process.stdout.write(`${result.name},`);

process.stdout.write(`"${formatter.format(opsSecReported)}",`);

process.stdout.write(`${result.histogram.samples},`);

process.stdout.write(
`"${result.plugins
.filter((p) => p.report)
.map((p) => p.report)
.join(",")}",`,
);

process.stdout.write(`${timer.format(result.histogram.min)},`);

process.stdout.write(`${timer.format(result.histogram.max)}\n`);
}
}

module.exports = {
csvReport,
};
75 changes: 74 additions & 1 deletion test/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ const { describe, it, before } = require("node:test");
const assert = require("node:assert");
const fs = require("node:fs");

const { Suite, chartReport, htmlReport, jsonReport } = require("../lib");
const {
Suite,
chartReport,
htmlReport,
jsonReport,
csvReport,
} = require("../lib");

describe("chartReport outputs benchmark results as a bar chart", async (t) => {
let output = "";
Expand Down Expand Up @@ -190,3 +196,70 @@ describe("jsonReport should produce valid JSON output", async () => {
}
});
});

describe("csvReport", () => {
it("should generate valid CSV output", async (t) => {
const fn = t.mock.method(process.stdout, "write");

// noop
fn.mock.mockImplementation(() => {});

csvReport([
{
opsSec: 749625.5652171721,
iterations: 374813,
histogram: {
samples: 10,
min: 1322.2615873857162,
max: 1345.4275821344213,
},
name: "single with matcher",
plugins: [
{
name: "V8NeverOptimizePlugin",
result: "enabled",
report: "v8-never-optimize=true",
},
],
},
{
opsSec: 634284.7401772924,
iterations: 317148,
histogram: {
samples: 11,
min: 1552.562466504839,
max: 1612.7852084972462,
},
name: "Multiple replaces",
plugins: [
{
name: "V8NeverOptimizePlugin",
result: "enabled",
report: "v8-never-optimize=true",
},
],
},
]);

const callArgs = process.stdout.write.mock.calls.map(
(call) => call.arguments[0],
);

assert.strictEqual(process.stdout.write.mock.callCount(), 13);
assert.deepStrictEqual(callArgs, [
"name,ops/sec,samples,plugins,min,max\n",
"single with matcher,",
'"749,626",',
"10,",
'"v8-never-optimize=true",',
"1.32us,",
"1.35us\n",
"Multiple replaces,",
'"634,285",',
"11,",
'"v8-never-optimize=true",',
"1.55us,",
"1.61us\n",
]);
});
});
Loading