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

benchmark: switch to spawn intead of fork #2799

Merged
merged 1 commit into from
Sep 14, 2020
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
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"transpilation",
"instanceof",
"flowtype",
"noconcurrent",

// Different names used inside tests
"Skywalker",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"version": "node resources/gen-version.js && npm test && git add src/version.js",
"fuzzonly": "mocha --full-trace src/**/__tests__/**/*-fuzz.js",
"changelog": "node resources/gen-changelog.js",
"benchmark": "node --noconcurrent_sweeping --expose-gc --predictable ./resources/benchmark.js",
"benchmark": "node resources/benchmark.js",
"test": "npm run lint && npm run check && npm run testonly && npm run prettier:check && npm run check:spelling && npm run build:npm && npm run build:deno && npm run check:integrations",
"lint": "eslint --cache .",
"check": "flow check",
Expand Down
58 changes: 0 additions & 58 deletions resources/benchmark-fork.js

This file was deleted.

62 changes: 61 additions & 1 deletion resources/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const os = require('os');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const cp = require('child_process');

const { red, green, yellow, cyan, grey } = require('./colors');
const { exec, rmdirRecursive, readdirRecursive } = require('./utils');
const { sampleModule } = require('./benchmark-fork');

const NS_PER_SEC = 1e9;
const LOCAL = 'local';
Expand Down Expand Up @@ -312,3 +312,63 @@ if (require.main === module) {
process.exit(1);
});
}

function sampleModule(modulePath) {
const sampleCode = `
const assert = require('assert');

assert(global.gc);
assert(process.send);
const module = require('${modulePath}');

clock(7, module.measure); // warm up
global.gc();
process.nextTick(() => {
const memBaseline = process.memoryUsage().heapUsed;
const clocked = clock(module.count, module.measure);
process.send({
name: module.name,
clocked: clocked / module.count,
memUsed: (process.memoryUsage().heapUsed - memBaseline) / module.count,
});
});

// Clocks the time taken to execute a test per cycle (secs).
function clock(count, fn) {
const start = process.hrtime.bigint();
for (let i = 0; i < count; ++i) {
fn();
}
return Number(process.hrtime.bigint() - start);
}
`;

return new Promise((resolve, reject) => {
const child = cp.spawn(
process.argv[0],
[
'--noconcurrent_sweeping',
'--predictable',
'--expose-gc',
'-e',
sampleCode,
],
{
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'production' },
},
);

let message;
let error;

child.on('message', (msg) => (message = msg));
child.on('error', (e) => (error = e));
child.on('close', () => {
if (message) {
return resolve(message);
}
reject(error || new Error('Spawn process closed without error'));
});
});
}