forked from Netflix/spectator-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dist_summary.test.js
66 lines (53 loc) · 1.73 KB
/
dist_summary.test.js
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
'use strict';
const chai = require('chai');
const assert = chai.assert;
const DistributionSummary = require('../src/dist_summary');
const MeterId = require('../src/meter_id');
describe('Distribution summaries', () => {
it('should record values', () => {
const id = new MeterId('ds');
const ds = new DistributionSummary(id);
// quick sanity check
assert.equal(ds.count, 0);
assert.equal(ds.totalAmount, 0);
ds.record(100);
assert.equal(ds.count, 1);
assert.equal(ds.totalAmount, 100);
// 0 counts towards total number of events
ds.record(0);
assert.equal(ds.count, 2);
assert.equal(ds.totalAmount, 100);
ds.record(101);
assert.equal(ds.count, 3);
assert.equal(ds.totalAmount, 201);
// ignores negative values
ds.record(-1);
assert.equal(ds.count, 3);
assert.equal(ds.totalAmount, 201);
});
function assertDs(ds, count, total, totalSq, max) {
const ms = ds.measure();
assert.equal(ms.length, 4, 'Dist summaries should report 4 values');
const id = ds.id;
const expected = {};
expected[id.withStat('count').key] = count;
expected[id.withStat('totalAmount').key] = total;
expected[id.withStat('totalOfSquares').key] = totalSq;
expected[id.withStat('max').key] = max;
const actual = {};
for (let m of ms) {
actual[m.id.key] = m.v;
}
assert.deepEqual(actual, expected);
// resets after being measured
assert.equal(ds.measure().length, 0);
}
it('should report proper measurements', () => {
const id = new MeterId('ds');
const ds = new DistributionSummary(id);
assert.equal(ds.measure().length, 0);
ds.record(100);
ds.record(200);
assertDs(ds, 2, 300, 100 * 100 + 200 * 200, 200);
});
});