Skip to content

Commit 9e47209

Browse files
RafaelGSStpoisseau
authored andcommitted
benchmark: enhance dc publish benchmark
PR-URL: nodejs#54745 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent a3b9413 commit 9e47209

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed
+37-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,58 @@
11
'use strict';
22
const common = require('../common.js');
3-
const dc = require('diagnostics_channel');
3+
const dc = require('node:diagnostics_channel');
44

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

1012
function noop() {}
1113

12-
function main({ n, subscribers }) {
14+
function createObj(size) {
15+
return Array.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+
function main({ n, subscribers, checkSubscribers, objSize }) {
1328
const channel = dc.channel('test');
1429
for (let i = 0; i < subscribers; i++) {
1530
channel.subscribe(noop);
1631
}
1732

18-
const data = {
19-
foo: 'bar',
33+
const publishWithCheck = () => {
34+
const data = createObj(objSize);
35+
bench.start();
36+
for (let i = 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 (let i = 0; i < n; i++) {
24-
if (channel.hasSubscribers) {
44+
const publishWithoutCheck = () => {
45+
const data = createObj(objSize);
46+
bench.start();
47+
for (let i = 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)