-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathheapSizeAndUsedTest.js
More file actions
131 lines (114 loc) · 3.58 KB
/
Copy pathheapSizeAndUsedTest.js
File metadata and controls
131 lines (114 loc) · 3.58 KB
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
'use strict';
const Registry = require('../../index').Registry;
describe.each([
['Prometheus', Registry.PROMETHEUS_CONTENT_TYPE],
['OpenMetrics', Registry.OPENMETRICS_CONTENT_TYPE],
])('heapSizeAndUsed with %s registry', (tag, regType) => {
const memoryUsedFn = process.memoryUsage;
afterEach(() => {
process.memoryUsage = memoryUsedFn;
});
it('should set gauge values from memoryUsage and heap statistics', async () => {
await jest.isolateModulesAsync(async () => {
jest.doMock('v8', () => {
return {
getHeapStatistics() {
return { heap_size_limit: 2147483648 };
},
};
});
const heapSizeAndUsed = require('../../lib/metrics/heapSizeAndUsed');
const globalRegistry = require('../../lib/registry').globalRegistry;
globalRegistry.setContentType(regType);
process.memoryUsage = function () {
return { heapTotal: 1000, heapUsed: 500, external: 100 };
};
try {
heapSizeAndUsed();
// Note: these gauges' values are set by the _total gauge's
// "collect" function.
const totalGauge = globalRegistry.getSingleMetric(
'nodejs_heap_size_total_bytes',
);
expect((await totalGauge.get()).values[0].value).toEqual(1000);
const usedGauge = globalRegistry.getSingleMetric(
'nodejs_heap_size_used_bytes',
);
expect((await usedGauge.get()).values[0].value).toEqual(500);
const externalGauge = globalRegistry.getSingleMetric(
'nodejs_external_memory_bytes',
);
expect((await externalGauge.get()).values[0].value).toEqual(100);
const limitGauge = globalRegistry.getSingleMetric(
'nodejs_heap_size_limit_bytes',
);
expect((await limitGauge.get()).values[0].value).toEqual(2147483648);
} finally {
globalRegistry.clear();
}
});
});
});
describe('heapSizeAndUsed isolated v8 behaviour', () => {
it('should read heap_size_limit from getHeapStatistics on each collect', async () => {
await jest.isolateModulesAsync(async () => {
let n = 0;
jest.doMock('v8', () => {
return {
getHeapStatistics() {
n++;
return { heap_size_limit: n * 1000 };
},
};
});
const { Registry } = require('../../index');
const heapSizeAndUsed = require('../../lib/metrics/heapSizeAndUsed');
const reg = new Registry();
const savedMem = process.memoryUsage;
process.memoryUsage = () => {
return {
heapTotal: 1,
heapUsed: 1,
external: 0,
};
};
try {
heapSizeAndUsed(reg);
const totalGauge = reg.getSingleMetric('nodejs_heap_size_total_bytes');
await totalGauge.get();
const limitGauge = reg.getSingleMetric('nodejs_heap_size_limit_bytes');
const data = await limitGauge.get();
// 1st call: isHeapStatisticsSupported(); 2nd: collect()
expect(n).toBe(2);
expect(data.values[0].value).toBe(2000);
} finally {
process.memoryUsage = savedMem;
}
});
});
it('should omit limit metric when getHeapStatistics is unsupported', async () => {
await jest.isolateModulesAsync(async () => {
jest.doMock('v8', () => {
return {
getHeapStatistics() {
const err = new Error('not implemented');
err.code = 'ERR_NOT_IMPLEMENTED';
throw err;
},
};
});
const { Registry } = require('../../index');
const heapSizeAndUsed = require('../../lib/metrics/heapSizeAndUsed');
expect(heapSizeAndUsed.metricNames).toEqual([
'nodejs_heap_size_total_bytes',
'nodejs_heap_size_used_bytes',
'nodejs_external_memory_bytes',
]);
const reg = new Registry();
heapSizeAndUsed(reg);
expect(
reg.getSingleMetric('nodejs_heap_size_limit_bytes'),
).toBeUndefined();
});
});
});