|
| 1 | +'use strict' |
| 2 | + |
| 3 | +require('../../setup/tap') |
| 4 | +const assert = require('assert') |
| 5 | + |
| 6 | +const PoissonProcessSamplingFilter = require('../../../src/profiling/profilers/poisson') |
| 7 | + |
| 8 | +describe('PoissonProcessSamplingFilter', () => { |
| 9 | + let nowValue |
| 10 | + const now = () => nowValue |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + nowValue = 0 |
| 14 | + }) |
| 15 | + |
| 16 | + it('should throw if resetInterval < samplingInterval', () => { |
| 17 | + assert.throws(() => new PoissonProcessSamplingFilter({ |
| 18 | + samplingInterval: 100, |
| 19 | + resetInterval: 50, |
| 20 | + now |
| 21 | + }), RangeError) |
| 22 | + }) |
| 23 | + |
| 24 | + it('should throw if now is not a function', () => { |
| 25 | + assert.throws(() => new PoissonProcessSamplingFilter({ |
| 26 | + samplingInterval: 100, |
| 27 | + resetInterval: 200, |
| 28 | + now: 123 |
| 29 | + }), (err) => err instanceof TypeError && err.message === 'now must be a function') |
| 30 | + }) |
| 31 | + |
| 32 | + it('should throw if now() returns a non-number', () => { |
| 33 | + const badNow = () => 'not-a-number' |
| 34 | + assert.throws(() => new PoissonProcessSamplingFilter({ |
| 35 | + samplingInterval: 100, |
| 36 | + resetInterval: 200, |
| 37 | + now: badNow |
| 38 | + }), (err) => err instanceof TypeError && err.message === 'now() must return a number') |
| 39 | + }) |
| 40 | + |
| 41 | + it('should throw if now() returns a decreasing value', () => { |
| 42 | + let callCount = 0 |
| 43 | + const decreasingNow = () => { |
| 44 | + callCount++ |
| 45 | + return callCount === 1 ? 100 : 50 |
| 46 | + } |
| 47 | + const filter = new PoissonProcessSamplingFilter({ |
| 48 | + samplingInterval: 10, |
| 49 | + resetInterval: 20, |
| 50 | + now: decreasingNow |
| 51 | + }) |
| 52 | + const event = { startTime: 0, duration: filter.nextSamplingInstant + 1 } |
| 53 | + assert.throws(() => filter.filter(event), (err) => err instanceof RangeError && |
| 54 | + err.message === 'now() must return a value greater than or equal to the last returned value') |
| 55 | + }) |
| 56 | + |
| 57 | + it('should allow now() to return the same value as last time', () => { |
| 58 | + let callCount = 0 |
| 59 | + const constantNow = () => { |
| 60 | + callCount++ |
| 61 | + return 42 |
| 62 | + } |
| 63 | + const filter = new PoissonProcessSamplingFilter({ |
| 64 | + samplingInterval: 10, |
| 65 | + resetInterval: 20, |
| 66 | + now: constantNow |
| 67 | + }) |
| 68 | + const event = { startTime: 0, duration: 100 } |
| 69 | + assert.doesNotThrow(() => filter.filter(event)) |
| 70 | + assert.strictEqual(callCount, 2) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should initialize with correct properties', () => { |
| 74 | + const filter = new PoissonProcessSamplingFilter({ |
| 75 | + samplingInterval: 100, |
| 76 | + resetInterval: 200, |
| 77 | + now |
| 78 | + }) |
| 79 | + assert.strictEqual(typeof filter.currentSamplingInstant, 'number') |
| 80 | + assert.strictEqual(filter.currentSamplingInstant, 0) |
| 81 | + assert.strictEqual(typeof filter.nextSamplingInstant, 'number') |
| 82 | + assert.ok(filter.nextSamplingInstant > 0) |
| 83 | + assert.strictEqual(filter.samplingInstantCount, 1) |
| 84 | + }) |
| 85 | + |
| 86 | + it('should advance sampling instant when event endTime >= nextSamplingInstant', () => { |
| 87 | + const filter = new PoissonProcessSamplingFilter({ |
| 88 | + samplingInterval: 100, |
| 89 | + resetInterval: 200, |
| 90 | + now |
| 91 | + }) |
| 92 | + const prevNextSamplingInstant = filter.nextSamplingInstant |
| 93 | + const event = { |
| 94 | + startTime: -10, |
| 95 | + duration: prevNextSamplingInstant + 15 |
| 96 | + } |
| 97 | + assert.strictEqual(filter.currentSamplingInstant, 0) |
| 98 | + nowValue = prevNextSamplingInstant + 15 |
| 99 | + filter.filter(event) |
| 100 | + assert.ok(filter.nextSamplingInstant > prevNextSamplingInstant) |
| 101 | + assert.ok(filter.currentSamplingInstant > 0) |
| 102 | + assert.ok(filter.samplingInstantCount > 1) |
| 103 | + }) |
| 104 | + |
| 105 | + it('should not advance sampling instant if event endTime < nextSamplingInstant', () => { |
| 106 | + const filter = new PoissonProcessSamplingFilter({ |
| 107 | + samplingInterval: 100, |
| 108 | + resetInterval: 200, |
| 109 | + now |
| 110 | + }) |
| 111 | + const prevSamplingInstant = filter.currentSamplingInstant |
| 112 | + const prevNextSamplingInstant = filter.nextSamplingInstant |
| 113 | + const event = { startTime: prevSamplingInstant - 10, duration: 1 } |
| 114 | + filter.filter(event) |
| 115 | + assert.strictEqual(filter.currentSamplingInstant, prevSamplingInstant) |
| 116 | + assert.strictEqual(filter.nextSamplingInstant, prevNextSamplingInstant) |
| 117 | + assert.strictEqual(filter.samplingInstantCount, 1) |
| 118 | + }) |
| 119 | + |
| 120 | + it('should cap endTime to now() if event endTime is in the future', () => { |
| 121 | + const filter = new PoissonProcessSamplingFilter({ |
| 122 | + samplingInterval: 100, |
| 123 | + resetInterval: 200, |
| 124 | + now |
| 125 | + }) |
| 126 | + const prevNextSamplingInstant = filter.nextSamplingInstant |
| 127 | + nowValue = 1000 |
| 128 | + const event = { startTime: 0, duration: 1e6 } |
| 129 | + filter.filter(event) |
| 130 | + assert.ok(filter.currentSamplingInstant >= prevNextSamplingInstant) |
| 131 | + assert.strictEqual(typeof filter.nextSamplingInstant, 'number') |
| 132 | + assert.ok(filter.nextSamplingInstant < 500000) |
| 133 | + assert.ok(filter.samplingInstantCount < 10) |
| 134 | + }) |
| 135 | + |
| 136 | + it('should reset nextSamplingInstant if it is too far in the past', () => { |
| 137 | + const filter = new PoissonProcessSamplingFilter({ |
| 138 | + samplingInterval: 100, |
| 139 | + resetInterval: 200, |
| 140 | + now |
| 141 | + }) |
| 142 | + const event = { startTime: 100000, duration: 100 } |
| 143 | + nowValue = event.startTime + event.duration |
| 144 | + filter.filter(event) |
| 145 | + assert.ok(filter.nextSamplingInstant > nowValue) |
| 146 | + assert.ok(filter.samplingInstantCount < 10) |
| 147 | + }) |
| 148 | + |
| 149 | + it('should return true if event.startTime < currentSamplingInstant', () => { |
| 150 | + const filter = new PoissonProcessSamplingFilter({ |
| 151 | + samplingInterval: 100, |
| 152 | + resetInterval: 200, |
| 153 | + now |
| 154 | + }) |
| 155 | + const event = { startTime: filter.currentSamplingInstant - 1, duration: 1 } |
| 156 | + assert.strictEqual(filter.filter(event), true) |
| 157 | + }) |
| 158 | + |
| 159 | + it('should return false if event.startTime >= currentSamplingInstant', () => { |
| 160 | + const filter = new PoissonProcessSamplingFilter({ |
| 161 | + samplingInterval: 100, |
| 162 | + resetInterval: 200, |
| 163 | + now |
| 164 | + }) |
| 165 | + const event = { startTime: filter.currentSamplingInstant, duration: 1 } |
| 166 | + assert.strictEqual(filter.filter(event), false) |
| 167 | + }) |
| 168 | + |
| 169 | + it('should increment samplingInstantCount on each sampling instant', () => { |
| 170 | + const filter = new PoissonProcessSamplingFilter({ |
| 171 | + samplingInterval: 10, |
| 172 | + resetInterval: 100, |
| 173 | + now |
| 174 | + }) |
| 175 | + const initialCount = filter.samplingInstantCount |
| 176 | + for (let i = 0; i < 5; i++) { |
| 177 | + nowValue += 20 |
| 178 | + const event = { startTime: 0, duration: filter.nextSamplingInstant - 0 } |
| 179 | + filter.filter(event) |
| 180 | + } |
| 181 | + assert.ok(filter.samplingInstantCount > initialCount) |
| 182 | + }) |
| 183 | +}) |
0 commit comments