1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const cp = require('child_process'); 5const fs = require('fs'); 6const path = require('path'); 7const tmpdir = require('../common/tmpdir'); 8 9if (process.argv[2] === 'child') { 10 const { performance } = require('perf_hooks'); 11 12 // Will emit mark and measure trace events 13 performance.mark('A'); 14 setTimeout(() => { 15 performance.mark('B'); 16 performance.measure('A to B', 'A', 'B'); 17 }, 1); 18 19 // Intentional non-op, part of the test 20 function f() {} 21 const ff = performance.timerify(f); 22 ff(); // Will emit a timerify trace event 23} else { 24 tmpdir.refresh(); 25 26 const expectedMarks = ['A', 'B']; 27 const expectedBegins = [ 28 { cat: 'node,node.perf,node.perf.timerify', name: 'f' }, 29 { cat: 'node,node.perf,node.perf.usertiming', name: 'A to B' }, 30 ]; 31 const expectedEnds = [ 32 { cat: 'node,node.perf,node.perf.timerify', name: 'f' }, 33 { cat: 'node,node.perf,node.perf.usertiming', name: 'A to B' }, 34 ]; 35 36 const proc = cp.fork(__filename, 37 [ 38 'child', 39 ], { 40 cwd: tmpdir.path, 41 execArgv: [ 42 '--trace-event-categories', 43 'node.perf', 44 ] 45 }); 46 47 proc.once('exit', common.mustCall(() => { 48 const file = path.join(tmpdir.path, 'node_trace.1.log'); 49 50 assert(fs.existsSync(file)); 51 fs.readFile(file, common.mustCall((err, data) => { 52 const traces = JSON.parse(data.toString()).traceEvents 53 .filter((trace) => trace.cat !== '__metadata'); 54 assert.strictEqual(traces.length, 55 expectedMarks.length + 56 expectedBegins.length + 57 expectedEnds.length); 58 59 traces.forEach((trace) => { 60 assert.strictEqual(trace.pid, proc.pid); 61 switch (trace.ph) { 62 case 'R': 63 assert.strictEqual(trace.cat, 64 'node,node.perf,node.perf.usertiming'); 65 assert.strictEqual(trace.name, 66 expectedMarks.shift()); 67 break; 68 case 'b': 69 const expectedBegin = expectedBegins.shift(); 70 assert.strictEqual(trace.cat, expectedBegin.cat); 71 assert.strictEqual(trace.name, expectedBegin.name); 72 break; 73 case 'e': 74 const expectedEnd = expectedEnds.shift(); 75 assert.strictEqual(trace.cat, expectedEnd.cat); 76 assert.strictEqual(trace.name, expectedEnd.name); 77 break; 78 default: 79 assert.fail('Unexpected trace event phase'); 80 } 81 }); 82 })); 83 })); 84} 85