-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmemory-leak-detect.mjs
More file actions
48 lines (36 loc) · 1.21 KB
/
Copy pathmemory-leak-detect.mjs
File metadata and controls
48 lines (36 loc) · 1.21 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
import { promises as fs } from 'node:fs'
import { setTimeout } from 'node:timers/promises'
import chalk from 'chalk'
import prettyBytes from 'pretty-bytes'
import { displayMemoryUsageFromNode } from './util.mjs'
import { compress, uncompress } from './index.js'
const YARN_LOCK = await fs.readFile('yarn.lock')
const COMPRESSED_YARN_LOCK = await compress(YARN_LOCK)
let initial = process.memoryUsage()
async function detect(job) {
for (let i = 0; i <= 100000; i++) {
await job()
if (i % 1000 === 0) {
await setTimeout(100)
if (typeof global.gc === 'function') {
global.gc()
}
if (i === 1000) {
initial = process.memoryUsage()
}
displayMemoryUsageFromNode(initial)
}
const memoryDiff = process.memoryUsage().rss - initial.rss
if (memoryDiff >= 1024 * 1024 * 200) {
throw new Error(`Memory limit reached ${prettyBytes(memoryDiff)}`)
}
}
}
console.info(chalk.green('Decompress broken buffer...'))
await detect(async () => uncompress(COMPRESSED_YARN_LOCK).catch(() => {}))
await setTimeout(1000)
if (typeof global.gc === 'function') {
global.gc()
}
console.info(chalk.green('Compress file...'))
await detect(async () => compress(YARN_LOCK))