@@ -4,10 +4,10 @@ const os = require('os');
4
4
const fs = require ( 'fs' ) ;
5
5
const path = require ( 'path' ) ;
6
6
const assert = require ( 'assert' ) ;
7
+ const cp = require ( 'child_process' ) ;
7
8
8
9
const { red, green, yellow, cyan, grey } = require ( './colors' ) ;
9
10
const { exec, rmdirRecursive, readdirRecursive } = require ( './utils' ) ;
10
- const { sampleModule } = require ( './benchmark-fork' ) ;
11
11
12
12
const NS_PER_SEC = 1e9 ;
13
13
const LOCAL = 'local' ;
@@ -312,3 +312,63 @@ if (require.main === module) {
312
312
process . exit ( 1 ) ;
313
313
} ) ;
314
314
}
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