|
1 | | -'use strict'; |
| 1 | +"use strict"; |
2 | 2 |
|
3 | | -/* eslint no-unused-vars: "off", import/no-extraneous-dependencies: "off", no-console: "off" */ |
4 | | - |
5 | | -const benchmark = require('fastbench'); |
6 | | -const stream = require('readable-stream'); |
7 | | -const Metric = require('@metrics/metric'); |
8 | | -const Guard = require('../'); |
| 3 | +const benchmark = require("fastbench"); |
| 4 | +const stream = require("readable-stream"); |
| 5 | +const Metric = require("@metrics/metric"); |
| 6 | +const Guard = require(".."); |
9 | 7 |
|
10 | 8 | const srcObjectStream = () => { |
11 | | - return new stream.Readable({ |
12 | | - objectMode: true, |
13 | | - read() { |
14 | | - |
15 | | - } |
16 | | - }); |
| 9 | + return new stream.Readable({ |
| 10 | + objectMode: true, |
| 11 | + read() {}, |
| 12 | + }); |
17 | 13 | }; |
18 | 14 |
|
19 | 15 | const destObjectStream = (done) => { |
20 | | - const dStream = new stream.Writable({ |
21 | | - objectMode: true, |
22 | | - write(chunk, encoding, callback) { |
23 | | - callback(); |
24 | | - }, |
25 | | - }); |
26 | | - dStream.on('finish', done); |
27 | | - return dStream; |
| 16 | + const dStream = new stream.Writable({ |
| 17 | + objectMode: true, |
| 18 | + write(chunk, encoding, callback) { |
| 19 | + callback(); |
| 20 | + }, |
| 21 | + }); |
| 22 | + dStream.on("finish", done); |
| 23 | + return dStream; |
28 | 24 | }; |
29 | 25 |
|
30 | 26 | const iterations = 10; |
31 | 27 | const max = 100000; |
32 | 28 |
|
33 | | -const run = benchmark([ |
34 | | - function NonGuardedMetricStream(done) { |
35 | | - const guard = new Guard({ enabled: false, permutationThreshold: (max + 1) }); |
36 | | - const dest = destObjectStream(done); |
37 | | - const src = srcObjectStream(); |
38 | | - |
39 | | - src.pipe(guard).pipe(dest); |
40 | | - |
41 | | - for (let i = 0; i < max; i++) { |
42 | | - const metric = new Metric({ name: 'foo', description: 'foo', labels: [{ name: 'foo', value: i }] }); |
43 | | - src.push(metric); |
44 | | - } |
45 | | - |
46 | | - process.nextTick(() => { |
47 | | - src.push(null); |
48 | | - }); |
49 | | - }, |
50 | | - |
51 | | - function GuardedMetricStreamNoDrop(done) { |
52 | | - const guard = new Guard({ enabled: true, permutationThreshold: (max * 2) }); |
53 | | - const dest = destObjectStream(done); |
54 | | - const src = srcObjectStream(); |
55 | | - |
56 | | - src.pipe(guard).pipe(dest); |
57 | | - |
58 | | - for (let i = 0; i < max; i++) { |
59 | | - const metric = new Metric({ name: 'foo', description: 'foo', labels: [{ name: 'foo', value: i }] }); |
60 | | - src.push(metric); |
61 | | - } |
62 | | - |
63 | | - process.nextTick(() => { |
64 | | - src.push(null); |
65 | | - }); |
66 | | - }, |
67 | | - |
68 | | - function GuardedMetricStreamDropping(done) { |
69 | | - const guard = new Guard({ enabled: true, permutationThreshold: 10000 }); |
70 | | - const dest = destObjectStream(done); |
71 | | - const src = srcObjectStream(); |
72 | | - |
73 | | - src.pipe(guard).pipe(dest); |
74 | | - |
75 | | - for (let i = 0; i < max; i++) { |
76 | | - const metric = new Metric({ name: 'foo', description: 'foo', labels: [{ name: 'foo', value: i }] }); |
77 | | - src.push(metric); |
78 | | - } |
79 | | - |
80 | | - process.nextTick(() => { |
81 | | - src.push(null); |
82 | | - }); |
83 | | - }, |
84 | | -], iterations); |
| 29 | +const run = benchmark( |
| 30 | + [ |
| 31 | + function NonGuardedMetricStream(done) { |
| 32 | + const guard = new Guard({ enabled: false, permutationThreshold: max + 1 }); |
| 33 | + const dest = destObjectStream(done); |
| 34 | + const src = srcObjectStream(); |
| 35 | + |
| 36 | + src.pipe(guard).pipe(dest); |
| 37 | + |
| 38 | + for (let i = 0; i < max; i++) { |
| 39 | + const metric = new Metric({ name: "foo", description: "foo", labels: [{ name: "foo", value: i }] }); |
| 40 | + src.push(metric); |
| 41 | + } |
| 42 | + |
| 43 | + process.nextTick(() => { |
| 44 | + src.push(null); |
| 45 | + }); |
| 46 | + }, |
| 47 | + |
| 48 | + function GuardedMetricStreamNoDrop(done) { |
| 49 | + const guard = new Guard({ enabled: true, permutationThreshold: max * 2 }); |
| 50 | + const dest = destObjectStream(done); |
| 51 | + const src = srcObjectStream(); |
| 52 | + |
| 53 | + src.pipe(guard).pipe(dest); |
| 54 | + |
| 55 | + for (let i = 0; i < max; i++) { |
| 56 | + const metric = new Metric({ name: "foo", description: "foo", labels: [{ name: "foo", value: i }] }); |
| 57 | + src.push(metric); |
| 58 | + } |
| 59 | + |
| 60 | + process.nextTick(() => { |
| 61 | + src.push(null); |
| 62 | + }); |
| 63 | + }, |
| 64 | + |
| 65 | + function GuardedMetricStreamDropping(done) { |
| 66 | + const guard = new Guard({ enabled: true, permutationThreshold: 10000 }); |
| 67 | + const dest = destObjectStream(done); |
| 68 | + const src = srcObjectStream(); |
| 69 | + |
| 70 | + src.pipe(guard).pipe(dest); |
| 71 | + |
| 72 | + for (let i = 0; i < max; i++) { |
| 73 | + const metric = new Metric({ name: "foo", description: "foo", labels: [{ name: "foo", value: i }] }); |
| 74 | + src.push(metric); |
| 75 | + } |
| 76 | + |
| 77 | + process.nextTick(() => { |
| 78 | + src.push(null); |
| 79 | + }); |
| 80 | + }, |
| 81 | + ], |
| 82 | + iterations, |
| 83 | +); |
85 | 84 |
|
86 | 85 | // run them two times |
87 | 86 | console.log(`Push ${max * iterations} metric objects through metric stream:`); |
|
0 commit comments