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
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,33 @@ packages operations.
- [x] winston
- [x] underscore

## Running
## Install

```console
npm i -g nodejs-package-benchmark
```

## Comparison

To compare binaries, you can use the `bench-it` script.

### Syntax: `bench-it $BINARY ["baseline"]`

This script allows you to compare the performance of binaries. If it's your first run, you need to generate the `baseline` data using:

```console
$ bench-it ./node baseline
```

To compare subsequent runs, simply omit the "baseline" option:

```console
$ bench-it ./node
```

> **Note:** It's recommended to have `colordiff` installed for a clearer comparison of differences.

## Single run

To a pretty terminal output, run `index.js`

Expand Down
52 changes: 52 additions & 0 deletions bench-it.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node

const { execSync } = require('child_process');
const fs = require('fs');

const NODEJS_PACKAGE_BENCHMARK_PATH = __dirname;

if (process.argv.length < 3) {
console.log("You must pass the binary as argument. Example: bench-it ./node");
process.exit(1);
}

const BINARY = process.argv[2];

if (process.argv[3] === "baseline") {
const baseline = execSync('git rev-parse HEAD').toString().trim();
const result = execSync(`${BINARY} ${NODEJS_PACKAGE_BENCHMARK_PATH}/index.js`, {
env: { TTY: true },
}).toString();
fs.writeFileSync('baseline.out', `${baseline}\n${result}`);
console.log("Baseline generated.");
process.exit(0);
}

if (!fs.existsSync('./baseline.out')) {
console.log(`The baseline.out does not exist. Generate it with: $ bench-it ${BINARY} baseline.`);
process.exit(1);
}

let diffCmd = "colordiff";
try {
execSync("command -v colordiff");
} catch (error) {
console.log("⚠️ 'colordiff' was not found. Using 'diff' as fallback.");
diffCmd = "diff";
}

const currentResult = execSync(`${BINARY} ${NODEJS_PACKAGE_BENCHMARK_PATH}/index.js`, {
env: { TTY: true },
}).toString();
fs.writeFileSync('current.out', currentResult);

try {
const stdout = execSync(`${diffCmd} -y baseline.out current.out`, {
cwd: process.cwd()
})
console.log(stdout.toString())
} catch (e) {
// `diff` returns a non-0 exit code
console.log(e.message)
console.log(e.stdout.toString())
}
10 changes: 8 additions & 2 deletions console-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ module.exports = {
console.log(str);
},
printResults (results) {
const halfScreen = process.stdout.columns / 2;
const halfScreen = process.stdout.columns ?
process.stdout.columns / 2 : 44; // arbitrary number
console.log('-'.repeat(halfScreen));
for (const result of results) {
console.log(`${result.name}`);
// TODO(rafaelgss): support non-operation method
for (const operation of result.operations) {
const opName = ` ${operation.name}:`;
const spaces = ' '.repeat(halfScreen - opName.length);
let spaces = '';
if (opName.length > halfScreen) {
spaces = opName.length + 2;
} else {
spaces = ' '.repeat(halfScreen - opName.length);
}
console.log(opName +
`${spaces}${opsSecFormatter.format(operation.opsSec)} (${operation.samples} samples)`);
}
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

const fs = require('node:fs/promises');
const path = require('node:path');
const Piscina = require('piscina');
Expand Down
29 changes: 26 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
{
"name": "nodejs-package-benchmark",
"description": "This package allows you to benchmark different runtimes using popular packages operations.",
"version": "1.0.0-beta.1",
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Once PR is approved I'll publish this version.

Currently, it's available 0.1.x

"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/NodeSource/nodejs-package-benchmark.git"
},
"keywords": [
"benchmark",
"nodejs",
"core"
],
"author": "RafaelGSS <rafael.nunu@hotmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/NodeSource/nodejs-package-benchmark/issues"
},
"homepage": "https://github.com/NodeSource/nodejs-package-benchmark#readme",
"bin": {
"bench-it": "./bench-it.js"
},
"dependencies": {
"@babel/standalone": "7.24.0",
"dotenv": "16.4.5",
Expand All @@ -9,9 +34,7 @@
"piscina": "4.4.0",
"prettier": "3.2.5",
"underscore": "1.13.6",
"winston": "3.12.0"
},
"devDependencies": {
"winston": "3.12.0",
"autocannon": "7.15.0",
"benchmark": "2.1.4"
}
Expand Down
12 changes: 6 additions & 6 deletions src/prettier-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,36 @@ module.exports = {
type: 'operation',
operations: [
{
name: 'format (semi=true)',
name: 'format',
fn: () => {
let v = undefined;
for (const p of payloads) {
v= prettier.format(p, { semi: true, parser: 'babel' });
v= prettier.format(p, { parser: 'babel' });
}
return v;
},
},
{
name: 'format (singleQuote=true semi=true tabs=true)',
name: 'format (singleQuote=true useTabs=true)',
fn: () => {
let v = undefined;
for (const p of payloads) {
v = prettier.format(
p,
{ singleQuote: true, semi: true, tabs: true, parser: 'babel' },
{ singleQuote: true, useTabs: true, parser: 'babel' },
);
}
return v;
},
},
{
name: 'format (singleQuote=false semi=false tabs=false)',
name: 'format (semi=false)',
fn: () => {
let v = undefined;
for (const p of payloads) {
v = prettier.format(
p,
{ singleQuote: false, semi: false, tabs: false, parser: 'babel' }
{ semi: false, parser: 'babel' }
);
}
return v;
Expand Down