0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 13:09:21 +01:00

benchmark: enhance dc publish benchmark

PR-URL: https://github.com/nodejs/node/pull/54745
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Rafael Gonzaga 2024-09-05 17:21:24 -03:00 committed by GitHub
parent 143aca7d1f
commit fff1549551
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,29 +1,58 @@
'use strict';
const common = require('../common.js');
const dc = require('diagnostics_channel');
const dc = require('node:diagnostics_channel');
const bench = common.createBenchmark(main, {
n: [1e8],
checkSubscribers: [1, 0],
objSize: [10, 1e2, 1e3],
subscribers: [0, 1, 10],
});
function noop() {}
function main({ n, subscribers }) {
function createObj(size) {
return Array.from({ length: size }, (n) => ({
foo: 'yarp',
nope: {
bar: '123',
a: [1, 2, 3],
baz: n,
c: {},
b: [],
},
}));
}
function main({ n, subscribers, checkSubscribers, objSize }) {
const channel = dc.channel('test');
for (let i = 0; i < subscribers; i++) {
channel.subscribe(noop);
}
const data = {
foo: 'bar',
const publishWithCheck = () => {
const data = createObj(objSize);
bench.start();
for (let i = 0; i < n; i++) {
if (channel.hasSubscribers) {
channel.publish(data);
}
}
bench.end(n);
};
bench.start();
for (let i = 0; i < n; i++) {
if (channel.hasSubscribers) {
const publishWithoutCheck = () => {
const data = createObj(objSize);
bench.start();
for (let i = 0; i < n; i++) {
channel.publish(data);
}
bench.end(n);
};
if (checkSubscribers) {
publishWithCheck();
} else {
publishWithoutCheck();
}
bench.end(n);
}