mirror of
https://github.com/nodejs/node.git
synced 2025-05-22 22:11:41 +00:00

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: https://github.com/nodejs/node/pull/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>
43 lines
805 B
JavaScript
43 lines
805 B
JavaScript
'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);
|
|
}
|
|
}
|