From e9b6f89b5569f7fdb14f8cab34a9b198e09c87a3 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 29 Jan 2020 16:43:40 +0000 Subject: [PATCH 1/3] 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. --- benchmark/worker/messageport.js | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 benchmark/worker/messageport.js diff --git a/benchmark/worker/messageport.js b/benchmark/worker/messageport.js new file mode 100644 index 00000000000000..9dc2d9f2daae43 --- /dev/null +++ b/benchmark/worker/messageport.js @@ -0,0 +1,46 @@ +'use strict'; + +const common = require('../common.js'); +const path = require('path'); +const { MessageChannel } = require('worker_threads'); +const bench = common.createBenchmark(main, { + payload: ['string', 'object'], + n: [1e6] +}); + +function main(conf) { + + const n = +conf.n; + const sends = +conf.sendsPerBroadcast; + + 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); + } +} From eb4fafd408e9ab7cf869d48a3df66150571b0c05 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 29 Jan 2020 17:55:52 +0000 Subject: [PATCH 2/3] fixup! benchmark: add MessagePort benchmark --- benchmark/worker/messageport.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/benchmark/worker/messageport.js b/benchmark/worker/messageport.js index 9dc2d9f2daae43..15eecb2e0ea1ab 100644 --- a/benchmark/worker/messageport.js +++ b/benchmark/worker/messageport.js @@ -1,7 +1,6 @@ 'use strict'; const common = require('../common.js'); -const path = require('path'); const { MessageChannel } = require('worker_threads'); const bench = common.createBenchmark(main, { payload: ['string', 'object'], @@ -11,7 +10,6 @@ const bench = common.createBenchmark(main, { function main(conf) { const n = +conf.n; - const sends = +conf.sendsPerBroadcast; let payload; From 80ead91fb5ad91aeb026412edf2d0d448cdbc300 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 30 Jan 2020 16:03:59 +0000 Subject: [PATCH 3/3] fixup! fixup! benchmark: add MessagePort benchmark --- benchmark/worker/messageport.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/benchmark/worker/messageport.js b/benchmark/worker/messageport.js index 15eecb2e0ea1ab..8e2ddae73ff3ab 100644 --- a/benchmark/worker/messageport.js +++ b/benchmark/worker/messageport.js @@ -8,9 +8,7 @@ const bench = common.createBenchmark(main, { }); function main(conf) { - - const n = +conf.n; - + const n = conf.n; let payload; switch (conf.payload) {