-
Notifications
You must be signed in to change notification settings - Fork 18
/
test.js
121 lines (116 loc) · 3.35 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
import test from 'ava';
import joi from 'joi';
import m from '.';
const schema = {
integer: joi
.number()
.integer()
.min(0),
time: joi.object().keys({
start: joi
.number()
.integer()
.min(0),
end: joi
.number()
.integer()
.min(0)
}),
avarage: joi.object().keys({
mean: joi.number().min(0),
median: joi.number().min(0),
stdev: joi.number().min(0),
max: joi.number().min(0),
min: joi.number().min(0)
}),
sample: joi.object().keys({
cpu: joi.number().min(0),
memory: joi
.number()
.integer()
.min(0),
processes: joi.array().items(
joi.object().keys({
cpu: joi.number().min(0),
memory: joi
.number()
.integer()
.min(0),
ppid: joi
.number()
.integer()
.min(0),
pid: joi
.number()
.integer()
.min(0),
ctime: joi
.number()
.integer()
.min(0),
elapsed: joi
.number()
.integer()
.min(0),
timestamp: joi
.number()
.integer()
.min(0)
})
)
})
};
test('should monitor pid without childs correctly', async t => {
const report = await m(`
let r = 2;
let c = 10e5;
while (c--) r = Math.pow(r, r);
return r;
`);
t.is(typeof report, 'object');
t.is(typeof report.times, 'object');
t.is(joi.validate(report.times.sampling, schema.time).error, null);
t.is(joi.validate(report.times.execution, schema.time).error, null);
t.is(typeof report.stats, 'object');
t.is(joi.validate(report.stats.cpu, schema.avarage).error, null);
t.is(joi.validate(report.stats.memory, schema.avarage).error, null);
t.is(typeof report.samples, 'object');
t.is(joi.validate(report.samples.count, schema.integer).error, null);
t.is(joi.validate(report.samples.period, schema.integer).error, null);
t.is(typeof report.samples.list, 'object');
// eslint-disable-next-line guard-for-in
for (const key in report.samples.list) {
t.is(joi.validate(report.samples.list[key], schema.sample).error, null);
}
});
test('should monitor pid with childs correctly', async t => {
const report = await m(`
const {spawn} = require('child_process');
let childno = 10;
let childs = [];
for (let i = 0; i < childno; i++) {
childs.push(spawn('node', ['-e', 'setInterval(()=>{let c=10e2;while(c--);},1000)']));
}
let c = 10e6;
let m = {};
while (c--) m[c] = c;
for (let i = 0; i < childno; i++) {
childs[i].kill();
}
`);
t.is(typeof report, 'object');
t.is(typeof report.times, 'object');
t.is(joi.validate(report.times.sampling, schema.time).error, null);
t.is(joi.validate(report.times.execution, schema.time).error, null);
t.is(typeof report.stats, 'object');
t.is(joi.validate(report.stats.cpu, schema.avarage).error, null);
t.is(joi.validate(report.stats.memory, schema.avarage).error, null);
t.is(typeof report.samples, 'object');
t.is(joi.validate(report.samples.count, schema.integer).error, null);
t.is(joi.validate(report.samples.period, schema.integer).error, null);
t.is(typeof report.samples.list, 'object');
// eslint-disable-next-line guard-for-in
for (const key in report.samples.list) {
t.is(joi.validate(report.samples.list[key], schema.sample).error, null);
}
});