Skip to content

Commit 4e40c77

Browse files
addaleaxcodebytere
authored andcommittedMar 30, 2020
benchmark: add MessagePort benchmark
Add a raw `MessagePort` benchmark that does not ping back and forth between different threads, unlike the `echo.js` benchmark, as there are some performance differences between single-threaded and multi- threaded operation, and a single-threaded Environment can be somewhat easier to work with when profiling. PR-URL: #31568 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 0bc3bd7 commit 4e40c77

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
 

‎benchmark/worker/messageport.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const { MessageChannel } = require('worker_threads');
5+
const bench = common.createBenchmark(main, {
6+
payload: ['string', 'object'],
7+
n: [1e6]
8+
});
9+
10+
function main(conf) {
11+
const n = conf.n;
12+
let payload;
13+
14+
switch (conf.payload) {
15+
case 'string':
16+
payload = 'hello world!';
17+
break;
18+
case 'object':
19+
payload = { action: 'pewpewpew', powerLevel: 9001 };
20+
break;
21+
default:
22+
throw new Error('Unsupported payload type');
23+
}
24+
25+
const { port1, port2 } = new MessageChannel();
26+
27+
let messages = 0;
28+
port2.onmessage = () => {
29+
if (messages++ === n) {
30+
bench.end(n);
31+
port1.close();
32+
} else {
33+
write();
34+
}
35+
};
36+
bench.start();
37+
write();
38+
39+
function write() {
40+
port1.postMessage(payload);
41+
}
42+
}

0 commit comments

Comments
 (0)
Please sign in to comment.