Skip to content

Commit ebdcafa

Browse files
addaleaxcodebytere
authored andcommitted
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 23da559 commit ebdcafa

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

‎benchmark/worker/messageport.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
constcommon=require('../common.js');
4+
const{ MessageChannel }=require('worker_threads');
5+
constbench=common.createBenchmark(main,{
6+
payload: ['string','object'],
7+
n: [1e6]
8+
});
9+
10+
functionmain(conf){
11+
constn=conf.n;
12+
letpayload;
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+
thrownewError('Unsupported payload type');
23+
}
24+
25+
const{ port1, port2 }=newMessageChannel();
26+
27+
letmessages=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+
functionwrite(){
40+
port1.postMessage(payload);
41+
}
42+
}

0 commit comments

Comments
 (0)