-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
272 lines (252 loc) · 6.45 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
'use strict';
/* eslint import/no-unresolved: 0 */
const v8 = require('v8');
const camelCase = require('camelcase');
const MB = 1024 * 1024;
const GB = MB * 1204;
function noop() {}
function isFunction(value) {
return typeof value === 'function';
}
function isNumber(value) {
return typeof value === 'number';
}
function isString(value) {
return typeof value === 'string';
}
function isObject(value) {
const type = typeof value;
return value != null && type === 'object';
}
/**
* Get the dalay of interval
* @param {Array} start The start time
* @param {Number} interval The value of interval, ms
* @returns {Number} The dalay ms
*/
function getDelay(start, interval) {
const delta = process.hrtime(start);
const nanosec = (delta[0] * 1e9) + delta[1];
/* eslint no-bitwise: ["error", { "int32Hint": true }] */
return Math.max((nanosec / 1e6 | 0) - interval, 0);
}
/**
* Format the bytes by unit
* @param {Number} value Bytes count
* @param {Object} unitInfo {unit: String, precision: Number}
* unit: 'GB', 'MB' or 'B'
* precision: the precision of size, default is 0
* @returns {Number}
*/
function format(value, unitInfo) {
const unit = unitInfo.unit;
const precision = unitInfo.precision || 0;
let v = 0;
switch (unit) {
case 'GB':
v = value / GB;
break;
case 'MB':
v = value / MB;
break;
default:
v = value;
break;
}
v = parseFloat(Number(v).toFixed(precision));
return v;
}
/**
* Get the heap statistics
* @param {Object} unitInfo The unit format setting
* @returns {Object} The heap statistics
*/
function getHeapStatistics(unitInfo) {
const data = v8.getHeapStatistics();
const keys = Object.keys(data);
const result = {};
keys.forEach((key) => {
result[key] = format(data[key], unitInfo);
});
return result;
}
/**
* Get the heap space statistics
* @param {Object} unitInfo The unit format setting
* @returns {Object} The heap space statistics
*/
function getHeapSpaceStatistics(unitInfo) {
const arr = v8.getHeapSpaceStatistics();
const result = {};
arr.forEach((item) => {
const data = {};
const keys = Object.keys(item);
keys.forEach((key) => {
if (key === 'space_name') {
return;
}
// replace the space_ key prefix
data[key.replace('space_', '')] = format(item[key], unitInfo);
});
result[item.space_name] = data;
});
return result;
}
/**
* Get the memory usage
* @param {Object} unitInfo The unit format setting
* @returns {Object} The memory usage
*/
function getMemoryUsage(unitInfo) {
const data = process.memoryUsage();
const keys = Object.keys(data);
const result = {};
keys.forEach((key) => {
result[key] = format(data[key], unitInfo);
});
return result;
}
/**
* Get the value from array by filter
* @param {Array} arr The array to filter
* @param {Function} filter The filter function
* @param {any} defaultValue The default value for no value is valid
* @returns {any} The value of filter
*/
function get(arr, filter, defaultValue) {
let result;
arr.forEach((tmp) => {
if (tmp && filter(tmp)) {
result = tmp;
}
});
return result || defaultValue;
}
/**
* Convert the unit info
* @param {String} str The unit info
* @returns {Object} The format unit info
*/
function convertUnit(str) {
const reg = /\.\d*/;
const result = reg.exec(str);
if (result && result[0]) {
return {
precision: result[0].length - 1,
unit: str.substring(result[0].length + 1),
};
}
return {
unit: str,
};
}
/**
* Get the cpu usage
* @param {Object} previousValue The previous cpu usage
* @param {Array} start The previous process.hrtime
* @returns {Object} The cpu usage {
* user: Number,
* system: Number,
* usedPercent: Number,
* userUsedPercent: Number,
* systemUsedPercent: Number,
* total: total
* }
*/
function getCpuUsage(previousValue, start) {
if (!previousValue) {
return null;
}
const usage = process.cpuUsage(previousValue);
const delta = process.hrtime(start);
const total = Math.ceil(((delta[0] * 1e9) + delta[1]) / 1000);
const usedPercent = Math.round(((usage.user + usage.system) / total) * 100);
usage.usedPercent = usedPercent;
usage.userUsedPercent = Math.round((usage.user / total) * 100);
usage.systemUsedPercent = Math.round((usage.system / total) * 100);
usage.total = total;
return usage;
}
/**
* Convert the data to camel case
*
* @param {Object} data
* @returns
*/
function camelCaseData(data) {
const result = {};
const keys = Object.keys(data);
keys.forEach((k) => {
const key = camelCase(k);
const value = data[k];
if (isObject(value)) {
result[key] = camelCaseData(value);
} else {
result[key] = value;
}
});
return result;
}
/**
* Flatten the data
*
* @param {Object} data
* @returns
*/
function flatten(data, pre) {
const prefix = pre || '';
const keys = Object.keys(data);
const result = {};
keys.forEach((k) => {
const value = data[k];
const key = [prefix, k].join('-');
if (isObject(value)) {
Object.assign(result, flatten(value, key));
} else {
result[key] = value;
}
});
return result;
}
/**
* Get the performance of node, include lag, heap, heapSpace, cpuUsage, memoryUsage
* @param {Function} fn The callback function of performance
* @param {Interval} interval The interval of get performance
* @returns {Timer} The setInterval timer
*/
function performance() {
/* eslint prefer-rest-params: 0 */
const args = Array.from(arguments);
const interval = get(args, isNumber, 100);
const fn = get(args, isFunction, noop);
const unitInfo = convertUnit(get(args, isString, 'B').toUpperCase());
let start = process.hrtime();
let cpuUsage = process.cpuUsage && process.cpuUsage();
const timer = setInterval(() => {
let result = {
lag: getDelay(start, interval),
heap: getHeapStatistics(unitInfo),
memoryUsage: getMemoryUsage(unitInfo),
};
if (cpuUsage) {
result.cpuUsage = getCpuUsage(cpuUsage, start);
}
if (v8.getHeapSpaceStatistics) {
result.heapSpace = getHeapSpaceStatistics(unitInfo);
}
if (performance.flatten) {
result = flatten(result);
}
if (performance.camelCase) {
result = camelCaseData(result);
}
fn(result);
start = process.hrtime();
cpuUsage = process.cpuUsage && process.cpuUsage();
}, interval);
timer.unref();
return timer;
}
performance.camelCase = false;
performance.flatten = false;
module.exports = performance;