Skip to content

Commit

Permalink
Add test for worker thread support
Browse files Browse the repository at this point in the history
Just a simple test, loading the module, capturing a small profile and
sending it back over the wire.

The test will be skipped if the worker_threads module can't be required.
  • Loading branch information
Widdershin committed Jul 7, 2020
1 parent 885da7c commit 15ec637
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion test/v8-profiler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
const expect = require('chai').expect,
"use strict";
const expect = require('chai').expect,
path = require('path'),
profiler = require('../');

const NODE_V_010 = /^v0\.10\.\d+$/.test(process.version);
const SOURCE_PATH = path.join(__dirname, '..');

var workerThreads;

try {
workerThreads = require('worker_threads');
} catch (e) {
// worker threads are not supported
}

describe('v8-profiler', function() {
describe('CPU', function() {
Expand Down Expand Up @@ -195,4 +206,43 @@ describe('v8-profiler', function() {
});
}
});

if (workerThreads) {
describe('worker_threads compatibility', function() {
it('supports profiling workers', function(done) {
const worker = new workerThreads.Worker(`
const {parentPort} = require('worker_threads');
const profiler = require('${SOURCE_PATH}');
profiler.startProfiling('worker');
let a = 1;
for (let i = 0; i<1e6; i++) { a += a };
const profile = profiler.stopProfiling('worker');
parentPort.postMessage(JSON.stringify(profile));
`, {eval: true});

worker.on('message', function (serializedProfile) {
const profile = JSON.parse(serializedProfile);

expect(Object.keys(profile).sort()).to.deep.eq([
"typeId",
"uid",
"title",
"head",
"startTime",
"endTime",
"samples",
"timestamps"
].sort());

done();
});

worker.on('error', done)
});
});
}
});

0 comments on commit 15ec637

Please sign in to comment.