-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: enhance dc publish benchmark
PR-URL: #54745 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Rich Trott <[email protected]>
- Loading branch information
Showing
1 changed file
with
37 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |