-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
150 lines (139 loc) · 4.5 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const fs = require('fs')
const path = require('path')
const bytes = require('bytes')
const pusage = require('pidusage')
const heapdump = require('heapdump')
const profiler = require('v8-profiler')
const memwatch = require('memwatch-next')
const RateLimiter = require('limiter').RateLimiter
const processing = {
cpu: false,
memory: false
}
const counter = {
cpu: 0,
memory: 0
}
function genProfilePath (profileDir, prefix, suffix) {
return path.join(profileDir, `${prefix}-${process.pid}-${Date.now()}.${suffix}`)
}
function dumpCpu (cpuProfileDir, cpuDuration) {
profiler.startProfiling()
processing.cpu = true
setTimeout(() => {
const profile = profiler.stopProfiling()
const filepath = genProfilePath(cpuProfileDir, 'cpu', 'cpuprofile')
profile.export()
.pipe(fs.createWriteStream(filepath))
.on('finish', () => {
processing.cpu = false
profile.delete()
console.error(`cpuprofile export success: ${filepath}`)
})
.on('error', (error) => {
processing.cpu = false
console.error(`cpuprofile export error: ${error.message}`)
console.error(error.stack)
})
}, cpuDuration)
}
function dumpMemory (memProfileDir, isLeak = false) {
processing.memory = true
const filepath = genProfilePath(memProfileDir, isLeak ? 'leak-memory' : 'memory', 'heapsnapshot')
heapdump.writeSnapshot(filepath, (error, filename) => {
processing.memory = false
if (error) {
console.error(`heapsnapshot dump error: ${error.message}`)
console.error(error.stack)
return
}
console.log(`heapsnapshot dump success: ${filename}`)
})
}
module.exports = function cpuMemoryMonitor (options = {}) {
const cpuOptions = options.cpu || {}
const cpuInterval = cpuOptions.interval || 1000
const cpuDuration = cpuOptions.duration || 30000
const cpuThreshold = cpuOptions.threshold || 90
const cpuProfileDir = cpuOptions.profileDir || process.cwd()
const cpuCounter = cpuOptions.counter || 1
const cpuLimiterOpt = cpuOptions.limiter || []
const cpuLimiter = new RateLimiter(cpuLimiterOpt[0] || 3, cpuLimiterOpt[1] || 'hour', true)
const memOptions = options.memory || {}
const memInterval = memOptions.interval || 1000
const memThreshold = bytes.parse(memOptions.threshold || '1.2gb')
const memProfileDir = memOptions.profileDir || process.cwd()
const memCounter = memOptions.counter || 1
const memLimiterOpt = memOptions.limiter || []
const memLimiter = new RateLimiter(memLimiterOpt[0] || 3, memLimiterOpt[1] || 'hour', true)
if (options.cpu) {
const cpuTimer = setInterval(() => {
if (processing.cpu) {
return
}
pusage.stat(process.pid, (err, stat) => {
if (err) {
console.error(`cpu stat error: ${err.message}`)
console.error(err.stack)
clearInterval(cpuTimer)
return
}
if (stat.cpu > cpuThreshold) {
counter.cpu += 1
if (counter.cpu >= cpuCounter) {
memLimiter.removeTokens(1, (limiterErr, remaining) => {
if (limiterErr) {
console.error(`limiterErr: ${limiterErr.message}`)
console.error(limiterErr.stack)
return
}
if (remaining > -1) {
dumpCpu(cpuProfileDir, cpuDuration)
counter.cpu = 0
}
})
}
} else {
counter.cpu = 0
}
})
}, cpuInterval)
}
if (options.memory) {
const memTimer = setInterval(() => {
if (processing.memory) {
return
}
pusage.stat(process.pid, (err, stat) => {
if (err) {
console.error(`memory stat error: ${err.message}`)
console.error(err.stack)
clearInterval(memTimer)
return
}
if (stat.memory > memThreshold) {
counter.memory += 1
if (counter.memory >= memCounter) {
cpuLimiter.removeTokens(1, (limiterErr, remaining) => {
if (limiterErr) {
console.error(`limiterErr: ${limiterErr.message}`)
console.error(limiterErr.stack)
return
}
if (remaining > -1) {
dumpMemory(memProfileDir)
counter.memory = 0
}
})
}
} else {
counter.memory = 0
}
})
}, memInterval)
memwatch.on('leak', (info) => {
console.warn('memory leak: %j', info)
dumpMemory(memProfileDir, true)
})
}
}