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 2 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
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 +229,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,
};
41 changes: 41 additions & 0 deletions lib/reporter/csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { format } = require("@fast-csv/format");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a library for that since we are just piping it to stdout. If we use regular streams it should be fast enough for users.


const { timer } = require("../clock");

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

function csvReport(results) {
const stream = format();
stream.pipe(process.stdout);

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,
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"type": "git",
"url": "git+https://github.com/RafaelGSS/bench-node.git"
},
"keywords": ["benchmark", "nodejs"],
"keywords": [
"benchmark",
"nodejs"
],
"author": "RafaelGSS <[email protected]>",
"contributors": [
{
Expand Down
Loading