/
Histogram.test.ts
194 lines (177 loc) · 6.49 KB
/
Histogram.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as assert from 'assert';
import { HistogramAggregator } from '../../../src/export/aggregators';
import { Histogram } from '../../../src';
import { hrTime, hrTimeToMilliseconds } from '@opentelemetry/core';
import sinon = require('sinon');
describe('HistogramAggregator', () => {
describe('constructor()', () => {
it('should construct a histogramAggregator', () => {
assert.doesNotThrow(() => {
new HistogramAggregator([1, 2]);
});
});
it('should sort boundaries', () => {
const aggregator = new HistogramAggregator([
200,
500,
300,
700,
1000,
1500,
]);
const point = aggregator.toPoint().value as Histogram;
assert.deepEqual(point.buckets.boundaries, [
200,
300,
500,
700,
1000,
1500,
]);
});
it('should throw if no boundaries are defined', () => {
// @ts-expect-error verify use without boundaries
assert.throws(() => new HistogramAggregator());
assert.throws(() => new HistogramAggregator([]));
});
});
describe('.update()', () => {
it('should update the second bucket', () => {
const aggregator = new HistogramAggregator([100, 200]);
aggregator.update(150);
const point = aggregator.toPoint().value as Histogram;
assert.equal(point.count, 1);
assert.equal(point.sum, 150);
assert.equal(point.buckets.counts[0], 0);
assert.equal(point.buckets.counts[1], 1);
assert.equal(point.buckets.counts[2], 0);
});
it('should update the second bucket', () => {
const aggregator = new HistogramAggregator([100, 200]);
aggregator.update(50);
const point = aggregator.toPoint().value as Histogram;
assert.equal(point.count, 1);
assert.equal(point.sum, 50);
assert.equal(point.buckets.counts[0], 1);
assert.equal(point.buckets.counts[1], 0);
assert.equal(point.buckets.counts[2], 0);
});
it('should update the third bucket since value is above all boundaries', () => {
const aggregator = new HistogramAggregator([100, 200]);
aggregator.update(250);
const point = aggregator.toPoint().value as Histogram;
assert.equal(point.count, 1);
assert.equal(point.sum, 250);
assert.equal(point.buckets.counts[0], 0);
assert.equal(point.buckets.counts[1], 0);
assert.equal(point.buckets.counts[2], 1);
});
it('should update the third bucket since boundaries are inclusive lower bounds', () => {
const aggregator = new HistogramAggregator([100, 200]);
aggregator.update(200);
const point = aggregator.toPoint().value as Histogram;
assert.equal(point.count, 1);
assert.equal(point.sum, 200);
assert.equal(point.buckets.counts[0], 0);
assert.equal(point.buckets.counts[1], 0);
assert.equal(point.buckets.counts[2], 1);
});
});
describe('.timestamp', () => {
let clock: sinon.SinonFakeTimers;
before(() => {
clock = sinon.useFakeTimers({ toFake: ['hrtime'] });
});
it('should update point timestamp', () => {
const aggregator = new HistogramAggregator([100, 200]);
const timestamp = hrTimeToMilliseconds(hrTime());
const timeDiff = 10;
clock.tick(timeDiff);
aggregator.update(150);
assert.equal(
hrTimeToMilliseconds(aggregator.toPoint().timestamp) >=
timestamp + timeDiff,
true
);
});
after(() => {
clock.restore();
});
});
describe('.count', () => {
it('should return last checkpoint count', () => {
const aggregator = new HistogramAggregator([100]);
let point = aggregator.toPoint().value as Histogram;
assert.equal(point.count, point.count);
aggregator.update(10);
point = aggregator.toPoint().value as Histogram;
assert.equal(point.count, 1);
assert.equal(point.count, point.count);
});
});
describe('.sum', () => {
it('should return last checkpoint sum', () => {
const aggregator = new HistogramAggregator([100]);
let point = aggregator.toPoint().value as Histogram;
assert.equal(point.sum, point.sum);
aggregator.update(10);
point = aggregator.toPoint().value as Histogram;
assert.equal(point.sum, 10);
});
});
describe('.reset()', () => {
it('should create a empty checkoint by default', () => {
const aggregator = new HistogramAggregator([100]);
const point = aggregator.toPoint().value as Histogram;
assert.deepEqual(point.buckets.boundaries, [100]);
assert(point.buckets.counts.every(count => count === 0));
// should contains one bucket for each boundary + one for values outside of the largest boundary
assert.equal(point.buckets.counts.length, 2);
assert.deepEqual(point.buckets.boundaries, [100]);
assert.equal(point.count, 0);
assert.equal(point.sum, 0);
});
it('should update checkpoint', () => {
const aggregator = new HistogramAggregator([100]);
aggregator.update(10);
const point = aggregator.toPoint().value as Histogram;
assert.equal(point.count, 1);
assert.equal(point.sum, 10);
assert.deepEqual(point.buckets.boundaries, [100]);
assert.equal(point.buckets.counts.length, 2);
assert.deepEqual(point.buckets.counts, [1, 0]);
});
});
describe('.toPoint()', () => {
it('should return default checkpoint', () => {
const aggregator = new HistogramAggregator([100]);
const point = aggregator.toPoint().value as Histogram;
assert.deepEqual(aggregator.toPoint().value, point);
assert(aggregator.toPoint().timestamp.every(nbr => nbr > 0));
});
it('should return last checkpoint if updated', () => {
const aggregator = new HistogramAggregator([100]);
aggregator.update(100);
assert(
aggregator
.toPoint()
.timestamp.every(nbr => typeof nbr === 'number' && nbr !== 0)
);
});
});
});