mirror of
https://github.com/nodejs/node.git
synced 2024-11-30 07:27:22 +01:00
63d4cae009
Having an experimental feature behind a flag makes change if we are expecting significant breaking changes to its API. Since the Worker API has been essentially stable since its initial introduction, and no noticeable doubt about possibly not keeping the feature around has been voiced, removing the flag and thereby reducing the barrier to experimentation, and consequently receiving feedback on the implementation, seems like a good idea. PR-URL: https://github.com/nodejs/node/pull/25361 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Masashi Hirano <shisama07@gmail.com> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
74 lines
1.5 KiB
JavaScript
74 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common.js');
|
|
const path = require('path');
|
|
const bench = common.createBenchmark(main, {
|
|
workers: [1],
|
|
payload: ['string', 'object'],
|
|
sendsPerBroadcast: [1, 10],
|
|
n: [1e5]
|
|
});
|
|
|
|
const workerPath = path.resolve(__dirname, '..', 'fixtures', 'echo.worker.js');
|
|
|
|
function main(conf) {
|
|
const { Worker } = require('worker_threads');
|
|
|
|
const n = +conf.n;
|
|
const workers = +conf.workers;
|
|
const sends = +conf.sendsPerBroadcast;
|
|
const expectedPerBroadcast = sends * workers;
|
|
var payload;
|
|
var readies = 0;
|
|
var broadcasts = 0;
|
|
var msgCount = 0;
|
|
|
|
switch (conf.payload) {
|
|
case 'string':
|
|
payload = 'hello world!';
|
|
break;
|
|
case 'object':
|
|
payload = { action: 'pewpewpew', powerLevel: 9001 };
|
|
break;
|
|
default:
|
|
throw new Error('Unsupported payload type');
|
|
}
|
|
|
|
const workerObjs = [];
|
|
|
|
for (var i = 0; i < workers; ++i) {
|
|
const worker = new Worker(workerPath);
|
|
workerObjs.push(worker);
|
|
worker.on('online', onOnline);
|
|
worker.on('message', onMessage);
|
|
}
|
|
|
|
function onOnline() {
|
|
if (++readies === workers) {
|
|
bench.start();
|
|
broadcast();
|
|
}
|
|
}
|
|
|
|
function broadcast() {
|
|
if (broadcasts++ === n) {
|
|
bench.end(n);
|
|
for (const worker of workerObjs) {
|
|
worker.unref();
|
|
}
|
|
return;
|
|
}
|
|
for (const worker of workerObjs) {
|
|
for (var i = 0; i < sends; ++i)
|
|
worker.postMessage(payload);
|
|
}
|
|
}
|
|
|
|
function onMessage() {
|
|
if (++msgCount === expectedPerBroadcast) {
|
|
msgCount = 0;
|
|
broadcast();
|
|
}
|
|
}
|
|
}
|