-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory-leak-detect.mjs
52 lines (44 loc) · 1.49 KB
/
memory-leak-detect.mjs
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
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import chalk from 'chalk'
import prettyBytes from 'pretty-bytes'
import { table } from 'table'
import { unzip } from './index.js'
const initial = process.memoryUsage()
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
function displayMemoryUsageFromNode(initialMemoryUsage) {
const finalMemoryUsage = process.memoryUsage()
const titles = Object.keys(initialMemoryUsage).map((k) => chalk.whiteBright(k))
const tableData = [titles]
const diffColumn = []
for (const [key, value] of Object.entries(initialMemoryUsage)) {
const diff = finalMemoryUsage[key] - value
const prettyDiff = prettyBytes(diff, { signed: true })
if (diff > 0) {
diffColumn.push(chalk.red(prettyDiff))
} else if (diff < 0) {
diffColumn.push(chalk.green(prettyDiff))
} else {
diffColumn.push(chalk.grey(prettyDiff))
}
}
tableData.push(diffColumn)
console.info(table(tableData))
}
async function detect(job) {
for (let i = 0; i <= 1000; i++) {
await job()
if (i % 1000 === 0) {
displayMemoryUsageFromNode(initial)
}
if (process.memoryUsage().rss - initial.rss >= 1024 * 1024 * 100) {
throw new Error('Memory limit reached')
}
}
}
async function memoryLeakDetect() {
console.info(chalk.green('Unzip file...'))
await detect(async () => unzip(path.resolve(__dirname, 'node_modules.zip')))
}
memoryLeakDetect().catch((err) => chalk.red(err))