From 6d59bfb4f823e30e291a6841410e324879f6486e Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Sat, 23 Sep 2017 17:35:07 -0700 Subject: benchmark: port cluster/echo to worker $ ./node benchmark/cluster/echo.js cluster/echo.js n=100000 sendsPerBroadcast=1 payload="string" workers=1: 33,647.30473442063 cluster/echo.js n=100000 sendsPerBroadcast=10 payload="string" workers=1: 12,927.907405288383 cluster/echo.js n=100000 sendsPerBroadcast=1 payload="object" workers=1: 28,496.37373941151 cluster/echo.js n=100000 sendsPerBroadcast=10 payload="object" workers=1: 8,975.53747186485 $ ./node --experimental-worker benchmark/worker/echo.js worker/echo.js n=100000 sendsPerBroadcast=1 payload="string" workers=1: 88,044.32902365089 worker/echo.js n=100000 sendsPerBroadcast=10 payload="string" workers=1: 39,873.33697018837 worker/echo.js n=100000 sendsPerBroadcast=1 payload="object" workers=1: 64,451.29132425621 worker/echo.js n=100000 sendsPerBroadcast=10 payload="object" workers=1: 22,325.635443739284 Refs: https://github.com/ayojs/ayo/pull/115 Reviewed-By: Anna Henningsen PR-URL: https://github.com/nodejs/node/pull/20876 Reviewed-By: Gireesh Punathil Reviewed-By: Benjamin Gruenbaum Reviewed-By: Shingo Inoue Reviewed-By: Matteo Collina Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: John-David Dalton Reviewed-By: Gus Caplan --- benchmark/fixtures/echo.worker.js | 7 ++++ benchmark/worker/echo.js | 73 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 benchmark/fixtures/echo.worker.js create mode 100644 benchmark/worker/echo.js (limited to 'benchmark') diff --git a/benchmark/fixtures/echo.worker.js b/benchmark/fixtures/echo.worker.js new file mode 100644 index 0000000000..fac5551a66 --- /dev/null +++ b/benchmark/fixtures/echo.worker.js @@ -0,0 +1,7 @@ +'use strict'; + +const { parentPort } = require('worker'); + +parentPort.on('message', (msg) => { + parentPort.postMessage(msg); +}); diff --git a/benchmark/worker/echo.js b/benchmark/worker/echo.js new file mode 100644 index 0000000000..6ab471a0a8 --- /dev/null +++ b/benchmark/worker/echo.js @@ -0,0 +1,73 @@ +'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] +}, { flags: ['--experimental-worker'] }); + +const workerPath = path.resolve(__dirname, '..', 'fixtures', 'echo.worker.js'); + +function main(conf) { + const { Worker } = require('worker'); + + 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(); + } + } +} -- cgit v1.2.3