Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ecba1c5

Browse filesBrowse files
TimothyGutargos
authored andcommitted
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: ayojs/ayo#115 Reviewed-By: Anna Henningsen <anna@addaleax.net> PR-URL: #20876 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
1 parent 6b1a887 commit ecba1c5
Copy full SHA for ecba1c5

File tree

Expand file treeCollapse file tree

2 files changed

+80
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+80
-0
lines changed
Open diff view settings
Collapse file

‎benchmark/fixtures/echo.worker.js‎

Copy file name to clipboard
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const { parentPort } = require('worker');
4+
5+
parentPort.on('message', (msg) => {
6+
parentPort.postMessage(msg);
7+
});
Collapse file

‎benchmark/worker/echo.js‎

Copy file name to clipboard
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const path = require('path');
5+
const bench = common.createBenchmark(main, {
6+
workers: [1],
7+
payload: ['string', 'object'],
8+
sendsPerBroadcast: [1, 10],
9+
n: [1e5]
10+
}, { flags: ['--experimental-worker'] });
11+
12+
const workerPath = path.resolve(__dirname, '..', 'fixtures', 'echo.worker.js');
13+
14+
function main(conf) {
15+
const { Worker } = require('worker');
16+
17+
const n = +conf.n;
18+
const workers = +conf.workers;
19+
const sends = +conf.sendsPerBroadcast;
20+
const expectedPerBroadcast = sends * workers;
21+
var payload;
22+
var readies = 0;
23+
var broadcasts = 0;
24+
var msgCount = 0;
25+
26+
switch (conf.payload) {
27+
case 'string':
28+
payload = 'hello world!';
29+
break;
30+
case 'object':
31+
payload = { action: 'pewpewpew', powerLevel: 9001 };
32+
break;
33+
default:
34+
throw new Error('Unsupported payload type');
35+
}
36+
37+
const workerObjs = [];
38+
39+
for (var i = 0; i < workers; ++i) {
40+
const worker = new Worker(workerPath);
41+
workerObjs.push(worker);
42+
worker.on('online', onOnline);
43+
worker.on('message', onMessage);
44+
}
45+
46+
function onOnline() {
47+
if (++readies === workers) {
48+
bench.start();
49+
broadcast();
50+
}
51+
}
52+
53+
function broadcast() {
54+
if (broadcasts++ === n) {
55+
bench.end(n);
56+
for (const worker of workerObjs) {
57+
worker.unref();
58+
}
59+
return;
60+
}
61+
for (const worker of workerObjs) {
62+
for (var i = 0; i < sends; ++i)
63+
worker.postMessage(payload);
64+
}
65+
}
66+
67+
function onMessage() {
68+
if (++msgCount === expectedPerBroadcast) {
69+
msgCount = 0;
70+
broadcast();
71+
}
72+
}
73+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.