forked from sadfsdfdsa/performance-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformanceWrapper.mjs
More file actions
103 lines (91 loc) · 2.5 KB
/
performanceWrapper.mjs
File metadata and controls
103 lines (91 loc) · 2.5 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @author Artem Shuvaev
* ! Example usage:
* * cli:
* node --expose-gc file.mjs
*
* * file.mjs:
* import { trackPerformance } from './performanceWrapper.mjs'
* const fn = () => console.log('Lets track it!')
* trackPerformance(fn, 'track it')
*
* * output:
* Lets track it!
* track it: 1.201ms
* track it heapTotal: 0mb
*/
import { performance } from 'perf_hooks'
console.warn('Memory usage metrics are still beta \n')
const formatMb = (memory) => Math.abs(memory / 1024 / 1024)
/**
* Track the perf!
* @see https://stackoverflow.com/questions/20018588/how-to-monitor-the-memory-usage-of-node-js
* @param {Function} fn
* @param {string} trackerName
* @param {'rss'|'heapTotal'|'heapUsed'|'external'} metric - heapTotal default
* @param {boolean} log - use logger
* @returns result of fn
*/
export const trackPerformance = (
fn,
trackerName,
metric = 'heapTotal',
log = true
) => {
if (global.gc) {
global.gc()
} else {
console.warn(
`It's can has errors in memory metrics,
prefer usage node with 'node --expose-gc file.mjs' flag`
)
}
const startedMemory = process.memoryUsage()[metric]
const beginTime = performance.now()
const result = fn()
const totalTime = performance.now() - beginTime
log && console.log(`${trackerName}: ${totalTime}ms`)
const endMemory = process.memoryUsage()[metric] - startedMemory
const formattedMemory = formatMb(endMemory)
log && console.log(`${trackerName} ${metric}: ${formattedMemory}mb \n`)
return [result, formattedMemory, totalTime]
}
/**
* Avg tracker
* @beta - it's WIP method
* @param {Function} fn
* @param {string} trackerName
* @param {'rss'|'heapTotal'|'heapUsed'|'external'} metric - heapTotal default
* @param {number} times - default 100
* @returns [avgMemoryMb, avgTimeMS]
*/
export const trackAveragePerformance = (
fn,
trackerName,
metric = 'heapTotal',
times = 100
) => {
if (global.gc) {
global.gc()
} else {
console.warn(
`It's can has errors in memory metrics,
prefer usage node with 'node --expose-gc file.mjs' flag`
)
}
let sumMemory = 0
let sumTime = 0
for (let index = 0; index < times; index++) {
const [_, memory, time] = trackPerformance(fn, trackerName, metric, false)
sumMemory += memory
sumTime += time
}
const avgMemory = sumMemory / times
const avgTime = sumTime / times
console.log(
`AVG for ${times} times
memory: ${avgMemory}mb
time: ${avgTime}ms\n`
)
return [avgMemory, avgTime]
}