UNPKG

1.85 kBJavaScriptView Raw
1'use strict'
2
3const test = require('tap').test
4const ProcessStat = require('../collect/process-stat.js')
5
6test('Collect - process stat - input validation', function (t) {
7 t.throws(
8 () => new ProcessStat(),
9 new TypeError('sample interval must be a number')
10 )
11 t.end()
12})
13
14test('Collect - process stat - timestamp', function (t) {
15 const stat = new ProcessStat(1)
16 const sample = stat.sample()
17 t.ok(sample.timestamp > Date.now() - 100 && sample.timestamp <= Date.now())
18 t.end()
19})
20
21test('Collect - process stat - number of handles', function (t) {
22 const stat = new ProcessStat(1)
23 const sample = stat.sample()
24 t.strictEqual(sample.handles, process._getActiveHandles().length)
25 t.end()
26})
27
28test('Collect - process stat - memory usage', function (t) {
29 const stat = new ProcessStat(1)
30 const sample = stat.sample()
31 t.ok(sample.memory.rss > 0)
32 t.ok(sample.memory.heapTotal > 0)
33 t.ok(sample.memory.heapUsed > 0)
34 t.ok(sample.memory.external > 0)
35
36 t.end()
37})
38
39function ms (now) {
40 const delta = process.hrtime(now)
41 return delta[0] * 1e3 + delta[1] * 1e-6
42}
43
44function sleep (time) {
45 const now = process.hrtime()
46 while (ms(now) < time);
47}
48
49test('Collect - process stat - delay usage', function (t) {
50 const stat = new ProcessStat(10)
51 stat.refresh()
52 sleep(20)
53 const sample = stat.sample()
54 t.ok(sample.delay > 8 && sample.delay < 15)
55
56 t.end()
57})
58
59test('Collect - process stat - cpu usage', function (t) {
60 const stat = new ProcessStat(10)
61 stat.refresh()
62 sleep(200)
63 const sample = stat.sample()
64 t.ok(sample.cpu >= 0 && sample.cpu <= 2,
65 'sleep has high usage, usage was: ' + sample.cpu)
66
67 stat.refresh()
68 setTimeout(function () {
69 const sample = stat.sample()
70 t.ok(sample.cpu >= 0 && sample.cpu <= 1,
71 'timeout has low usage, usage was: ' + sample.cpu)
72 t.end()
73 }, 200)
74})