Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for worker thread support #17

Merged
merged 2 commits into from
Jul 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion test/v8-profiler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
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, '..');

function escape(str) {
return str
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d');
};

let 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 +212,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('${escape(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)
});
});
}
});