Skip to content

Commit

Permalink
benchmark: add MessagePort benchmark
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Denys Otrishko <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
addaleax authored and codebytere committed Feb 17, 2020
1 parent 23da559 commit ebdcafa
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions benchmark/worker/messageport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const common = require('../common.js');
const { MessageChannel } = require('worker_threads');
const bench = common.createBenchmark(main, {
payload: ['string', 'object'],
n: [1e6]
});

function main(conf) {
const n = conf.n;
let payload;

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 { port1, port2 } = new MessageChannel();

let messages = 0;
port2.onmessage = () => {
if (messages++ === n) {
bench.end(n);
port1.close();
} else {
write();
}
};
bench.start();
write();

function write() {
port1.postMessage(payload);
}
}

0 comments on commit ebdcafa

Please sign in to comment.