Skip to content

Commit

Permalink
benchmark: switch to spawn intead of fork (#2799)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Sep 14, 2020
1 parent 156c76e commit 16009cb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 60 deletions.
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'));
});
});
}

0 comments on commit 16009cb

Please sign in to comment.