forked from mcollina/native-hdr-histogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
118 lines (106 loc) · 3.07 KB
/
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
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
'use strict'
const test = require('tap').test
const Histogram = require('./')
test('create an histogram', (t) => {
t.doesNotThrow(() => Histogram(1, 100))
t.end()
})
test('create an histogram with a constructor', (t) => {
t.doesNotThrow(() => new Histogram(1, 100))
t.end()
})
test('create an histogram arguments checks', (t) => {
t.throws(() => Histogram(-1, 100))
t.throws(() => Histogram(0, 100))
t.throws(() => Histogram(1, 100, 20))
t.throws(() => Histogram(1, 100, 0))
t.throws(() => Histogram(1, 100, 6))
for (let i = 1; i < 5; i++) {
t.doesNotThrow(() => Histogram(1, 100, i))
}
t.end()
})
test('record values in an histogram', (t) => {
const instance = new Histogram(1, 100)
t.ok(instance.record(42))
t.ok(instance.record(45))
t.end()
})
test('recording a non-value returns false', (t) => {
const instance = Histogram(1, 100)
t.notOk(instance.record())
t.notOk(instance.record(-42))
t.end()
})
test('stdev, mean, min, max', (t) => {
const instance = Histogram(1, 100)
t.ok(instance.record(42))
t.ok(instance.record(45))
t.equal(instance.min(), 42, 'min is available')
t.equal(instance.max(), 45, 'max is available')
t.equal(instance.mean(), 43.5, 'mean is available')
t.equal(instance.stddev(), 1.5, 'stdev is available')
t.end()
})
test('percentile', (t) => {
const instance = Histogram(1, 100)
t.ok(instance.record(42))
t.ok(instance.record(42))
t.ok(instance.record(45))
t.equal(instance.percentile(10), 42, 'percentile match')
t.equal(instance.percentile(99), 45, 'percentile match')
t.equal(instance.percentile(100), 45, 'percentile match')
t.end()
})
test('wrong percentile', (t) => {
const instance = Histogram(1, 100)
t.ok(instance.record(42))
t.ok(instance.record(42))
t.ok(instance.record(45))
t.throws(() => instance.percentile(), 'no percentile throws')
t.throws(() => instance.percentile(101), 'percentile > 100 throws')
t.throws(() => instance.percentile(0), 'percentile == 0 throws')
t.throws(() => instance.percentile(-1), 'percentile < 0 throws')
t.end()
})
test('encode/decode', (t) => {
const instance = Histogram(1, 100)
t.ok(instance.record(42))
t.ok(instance.record(42))
t.ok(instance.record(45))
const instance2 = Histogram.decode(instance.encode())
t.equal(instance2.percentile(10), 42, 'percentile match')
t.equal(instance2.percentile(99), 45, 'percentile match')
t.end()
})
test('fail decode', (t) => {
t.throws(() => Histogram.decode())
t.throws(() => Histogram.decode('hello'))
t.throws(() => Histogram.decode({}))
t.throws(() => Histogram.decode(42))
t.end()
})
test('percentiles', (t) => {
const instance = Histogram(1, 100)
t.deepEqual(instance.percentiles(), [{
percentile: 100,
value: 0
}], 'empty percentiles has 0 till 100%')
t.ok(instance.record(42))
t.ok(instance.record(42))
t.ok(instance.record(45))
t.deepEqual(instance.percentiles(), [{
percentile: 0,
value: 42
}, {
percentile: 50,
value: 42
}, {
percentile: 75,
value: 45
}, {
percentile: 100,
value: 45
}], 'percentiles matches')
t.end()
})