Skip to content

Commit 16009cb

Browse files
benchmark: switch to spawn intead of fork (#2799)
1 parent 156c76e commit 16009cb

File tree

4 files changed

+63
-60
lines changed

4 files changed

+63
-60
lines changed

cspell.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"transpilation",
3333
"instanceof",
3434
"flowtype",
35+
"noconcurrent",
3536

3637
// Different names used inside tests
3738
"Skywalker",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"version": "node resources/gen-version.js && npm test && git add src/version.js",
3232
"fuzzonly": "mocha --full-trace src/**/__tests__/**/*-fuzz.js",
3333
"changelog": "node resources/gen-changelog.js",
34-
"benchmark": "node --noconcurrent_sweeping --expose-gc --predictable ./resources/benchmark.js",
34+
"benchmark": "node resources/benchmark.js",
3535
"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",
3636
"lint": "eslint --cache .",
3737
"check": "flow check",

resources/benchmark-fork.js

-58
This file was deleted.

resources/benchmark.js

+61-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const os = require('os');
44
const fs = require('fs');
55
const path = require('path');
66
const assert = require('assert');
7+
const cp = require('child_process');
78

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

1212
const NS_PER_SEC = 1e9;
1313
const LOCAL = 'local';
@@ -312,3 +312,63 @@ if (require.main === module) {
312312
process.exit(1);
313313
});
314314
}
315+
316+
function sampleModule(modulePath) {
317+
const sampleCode = `
318+
const assert = require('assert');
319+
320+
assert(global.gc);
321+
assert(process.send);
322+
const module = require('${modulePath}');
323+
324+
clock(7, module.measure); // warm up
325+
global.gc();
326+
process.nextTick(() => {
327+
const memBaseline = process.memoryUsage().heapUsed;
328+
const clocked = clock(module.count, module.measure);
329+
process.send({
330+
name: module.name,
331+
clocked: clocked / module.count,
332+
memUsed: (process.memoryUsage().heapUsed - memBaseline) / module.count,
333+
});
334+
});
335+
336+
// Clocks the time taken to execute a test per cycle (secs).
337+
function clock(count, fn) {
338+
const start = process.hrtime.bigint();
339+
for (let i = 0; i < count; ++i) {
340+
fn();
341+
}
342+
return Number(process.hrtime.bigint() - start);
343+
}
344+
`;
345+
346+
return new Promise((resolve, reject) => {
347+
const child = cp.spawn(
348+
process.argv[0],
349+
[
350+
'--noconcurrent_sweeping',
351+
'--predictable',
352+
'--expose-gc',
353+
'-e',
354+
sampleCode,
355+
],
356+
{
357+
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
358+
env: { NODE_ENV: 'production' },
359+
},
360+
);
361+
362+
let message;
363+
let error;
364+
365+
child.on('message', (msg) => (message = msg));
366+
child.on('error', (e) => (error = e));
367+
child.on('close', () => {
368+
if (message) {
369+
return resolve(message);
370+
}
371+
reject(error || new Error('Spawn process closed without error'));
372+
});
373+
});
374+
}

0 commit comments

Comments
 (0)