From fff15495519699e3bff7c3fae95c9e8b7b9eae00 Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Thu, 5 Sep 2024 17:21:24 -0300 Subject: [PATCH] benchmark: enhance dc publish benchmark PR-URL: https://github.com/nodejs/node/pull/54745 Reviewed-By: Stephen Belanger Reviewed-By: Rich Trott --- benchmark/diagnostics_channel/publish.js | 45 +++++++++++++++++++----- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/benchmark/diagnostics_channel/publish.js b/benchmark/diagnostics_channel/publish.js index 3a30012ba692b8..6d8e40a6b733f6 100644 --- a/benchmark/diagnostics_channel/publish.js +++ b/benchmark/diagnostics_channel/publish.js @@ -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); }