|
1 | 1 | 'use strict';
|
2 | 2 | const common = require('../common.js');
|
3 |
| -const dc = require('diagnostics_channel'); |
| 3 | +const dc = require('node:diagnostics_channel'); |
4 | 4 |
|
5 | 5 | const bench = common.createBenchmark(main, {
|
6 | 6 | n: [1e8],
|
| 7 | + checkSubscribers: [1, 0], |
| 8 | + objSize: [10, 1e2, 1e3], |
7 | 9 | subscribers: [0, 1, 10],
|
8 | 10 | });
|
9 | 11 |
|
10 | 12 | function noop() {}
|
11 | 13 |
|
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 }) { |
13 | 28 | const channel = dc.channel('test');
|
14 | 29 | for (let i = 0; i < subscribers; i++) {
|
15 | 30 | channel.subscribe(noop);
|
16 | 31 | }
|
17 | 32 |
|
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); |
20 | 42 | };
|
21 | 43 |
|
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++) { |
25 | 48 | channel.publish(data);
|
26 | 49 | }
|
| 50 | + bench.end(n); |
| 51 | + }; |
| 52 | + |
| 53 | + if (checkSubscribers) { |
| 54 | + publishWithCheck(); |
| 55 | + } else { |
| 56 | + publishWithoutCheck(); |
27 | 57 | }
|
28 |
| - bench.end(n); |
29 | 58 | }
|
0 commit comments