Skip to content

Commit b877972

Browse files
RafaelGSStargos
authored andcommitted
benchmark: enhance dc publish benchmark
PR-URL: #54745 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 77c5922 commit b877972

1 file changed

Lines changed: 37 additions & 8 deletions

File tree

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,58 @@
11
'use strict';
22
constcommon=require('../common.js');
3-
constdc=require('diagnostics_channel');
3+
constdc=require('node:diagnostics_channel');
44

55
constbench=common.createBenchmark(main,{
66
n: [1e8],
7+
checkSubscribers: [1,0],
8+
objSize: [10,1e2,1e3],
79
subscribers: [0,1,10],
810
});
911

1012
functionnoop(){}
1113

12-
functionmain({ n, subscribers }){
14+
functioncreateObj(size){
15+
returnArray.from({length: size},(n)=>({
16+
foo: 'yarp',
17+
nope: {
18+
bar: '123',
19+
a: [1,2,3],
20+
baz: n,
21+
c: {},
22+
b: [],
23+
},
24+
}));
25+
}
26+
27+
functionmain({ n, subscribers, checkSubscribers, objSize }){
1328
constchannel=dc.channel('test');
1429
for(leti=0;i<subscribers;i++){
1530
channel.subscribe(noop);
1631
}
1732

18-
constdata={
19-
foo: 'bar',
33+
constpublishWithCheck=()=>{
34+
constdata=createObj(objSize);
35+
bench.start();
36+
for(leti=0;i<n;i++){
37+
if(channel.hasSubscribers){
38+
channel.publish(data);
39+
}
40+
}
41+
bench.end(n);
2042
};
2143

22-
bench.start();
23-
for(leti=0;i<n;i++){
24-
if(channel.hasSubscribers){
44+
constpublishWithoutCheck=()=>{
45+
constdata=createObj(objSize);
46+
bench.start();
47+
for(leti=0;i<n;i++){
2548
channel.publish(data);
2649
}
50+
bench.end(n);
51+
};
52+
53+
if(checkSubscribers){
54+
publishWithCheck();
55+
}else{
56+
publishWithoutCheck();
2757
}
28-
bench.end(n);
2958
}

0 commit comments

Comments
 (0)