-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
165 lines (132 loc) · 3.08 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
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
'use strict'
const test = require('tape')
const stopwatch = require('.')
test('start/stop', async function (t) {
t.plan(1)
const plugin = stopwatch('test.duration')
const collector = plugin()
const stop1 = plugin.start()
const stop2 = plugin.start()
await sleep(100)
stop1()
await sleep(100)
stop2()
const stop3 = plugin.start()
stop3()
t.same(await collect(collector, 'ping'), [{
name: 'test.duration',
unit: 'milliseconds',
resolution: 60,
value: 100
}, {
name: 'test.duration',
unit: 'milliseconds',
resolution: 60,
value: 200
}, {
name: 'test.duration',
unit: 'milliseconds',
resolution: 60,
value: 0
}])
})
test('start on creation', async function (t) {
t.plan(1)
const plugin = stopwatch('test.duration', { start: true })
const collector = plugin()
await sleep(100)
plugin.stop()
t.same(await collect(collector, 'ping'), [{
name: 'test.duration',
unit: 'milliseconds',
resolution: 60,
value: 100
}])
})
test('collector.stop() flushes metrics', async function (t) {
t.plan(1)
const plugin = stopwatch('test.duration')
const collector = plugin()
const stop1 = plugin.start()
const stop2 = plugin.start()
await sleep(100)
stop1()
stop2()
t.same(await collect(collector, 'stop'), [{
name: 'test.duration',
unit: 'milliseconds',
resolution: 60,
value: 100
}, {
name: 'test.duration',
unit: 'milliseconds',
resolution: 60,
value: 100
}])
})
test('plugin.stop() stops all stopwatches', async function (t) {
t.plan(1)
const plugin = stopwatch('test.duration')
const collector = plugin()
plugin.start()
plugin.start()
await sleep(100)
plugin.stop()
t.same(await collect(collector, 'stop'), [{
name: 'test.duration',
unit: 'milliseconds',
resolution: 60,
value: 100
}, {
name: 'test.duration',
unit: 'milliseconds',
resolution: 60,
value: 100
}])
})
test('can be collected by multiple tasks', async function (t) {
t.plan(2)
const plugin = stopwatch('test.duration')
const collector1 = plugin()
const collector2 = plugin()
const stop = plugin.start()
await sleep(100)
stop()
t.same(await collect(collector1, 'ping'), [{
name: 'test.duration',
unit: 'milliseconds',
resolution: 60,
value: 100
}])
t.same(await collect(collector2, 'ping'), [{
name: 'test.duration',
unit: 'milliseconds',
resolution: 60,
value: 100
}])
})
function collect (collector, method) {
return new Promise((resolve, reject) => {
const metrics = []
const push = metrics.push.bind(metrics)
collector.on('metric', push)
collector[method || 'ping']((err) => {
collector.removeListener('metric', push)
if (err) return reject(err)
metrics.forEach(simplify)
resolve(metrics)
})
})
}
function sleep (ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
function simplify (metric) {
delete metric.tags
delete metric.date
delete metric.statistic
metric.value = Math.round(metric.value / 50) * 50
return metric
}