-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
86 lines (71 loc) · 2.86 KB
/
test.js
File metadata and controls
86 lines (71 loc) · 2.86 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
const test = require('brittle')
const setupSlabHunter = require('.')
const DEBUG = false
test('slab leaks are detected', async (t) => {
const leakCutoffMs = 0 // no delay to simplify test
const getLeaks = setupSlabHunter(leakCutoffMs)
{
const leaks = getLeaks()
t.is(leaks.bigBufferLeaks.length, 0, 'sanity check: no initial big-buffer leaks')
t.is(leaks.slabLeaks.length, 0, 'sanity check: no initial slab leaks')
}
const retainedBuffers = []
let nonRetainedBuffers = []
for (let i = 0; i < 1000; i++) {
// DEVNOTE: apparently defining a const inside the loop and storing it
// conditionally retains the last instance of that const, also after the
// for loop and a gc run. (curious why)
// To simplify the test, we do not define any variables inside the for loop
Buffer.allocUnsafe(100)
if (i % 100 === 0) {
retainedBuffers.push(Buffer.allocUnsafe(100))
} else {
nonRetainedBuffers.push(Buffer.allocUnsafe(100))
}
}
nonRetainedBuffers = null // delete all refs
t.is(retainedBuffers.length, 10, 'sanity check: 10 retained buffers')
global.gc() // assumes node is run with --expose-gc flag
await new Promise(resolve => setTimeout(resolve, 100))
{
const leaks = getLeaks()
if (DEBUG) console.log(leaks)
t.is(leaks.bigBufferLeaks.length, 0, 'no big-buffer leaks')
t.is(leaks.slabLeaks.length, 1, 'slab leak detected')
t.is(leaks.slabLeaks[0].amount, 10, 'all 10 leaks are detected')
}
})
test('big-buffer leaks are detected', async (t) => {
const leakCutoffMs = 0 // no delay to simplify test
const getLeaks = setupSlabHunter(leakCutoffMs)
{
const leaks = getLeaks()
t.is(leaks.bigBufferLeaks.length, 0, 'sanity check: no initial big-buffer leaks')
t.is(leaks.slabLeaks.length, 0, 'sanity check: no initial slab leaks')
}
const retainedBuffers = []
let nonRetainedBuffers = []
for (let i = 0; i < 1000; i++) {
// DEVNOTE: apparently defining a const inside the loop and storing it
// conditionally retains the last instance of that const, also after the
// for loop and a gc run. (curious why)
// To simplify the test, we do not define any variables inside the for loop
Buffer.allocUnsafe(100)
if (i % 100 === 0) {
retainedBuffers.push(Buffer.allocUnsafe(10_000))
} else {
nonRetainedBuffers.push(Buffer.allocUnsafe(10_000))
}
}
nonRetainedBuffers = null // delete all refs
t.is(retainedBuffers.length, 10, 'sanity check: 10 retained buffers')
global.gc() // assumes node is run with --expose-gc flag
await new Promise(resolve => setTimeout(resolve, 100))
{
const leaks = getLeaks()
if (DEBUG) console.log(leaks)
t.is(leaks.bigBufferLeaks.length, 1, 'big-buffer leak detected')
t.is(leaks.bigBufferLeaks[0].amount, 10, 'all 10 leaks are detected')
t.is(leaks.slabLeaks.length, 0, 'no slab leaks detected')
}
})