Last active
December 26, 2021 21:25
-
-
Save slutske22/8b46f7c5471ee64966ce0633c8954fb9 to your computer and use it in GitHub Desktop.
Profiler class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
import v8profiler from 'v8-profiler-next'; | |
export class Profiler { | |
constructor(options) { | |
const { title, active, outputDir } = options; | |
this.title = title; | |
this.active = active; | |
this.outputDir = outputDir; | |
} | |
start() { | |
if (this.active) { | |
v8profiler.startProfiling(this.title, true); | |
} | |
} | |
finish() { | |
if (this.active) { | |
const profile = v8profiler.stopProfiling(this.title); | |
profile.export((error, result) => { | |
if (error) { | |
console.log('error', error); | |
} else { | |
fs.writeFileSync( | |
`${process.cwd()}/${this.outputDir}/${this.title}.cpuprofile`, | |
result | |
); | |
} | |
}); | |
} | |
} | |
} | |
export default Profiler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment