Created
September 16, 2021 22:40
-
-
Save krisselden/98a6e2ced21cad5696161005ac27c0bd to your computer and use it in GitHub Desktop.
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
const { Session } = require('inspector'); | |
/** | |
* @param task {() => Promise<void>} | |
* @returns {import('inspector').Profiler.Profile} | |
*/ | |
async function runProfile(task) { | |
const session = new Session(); | |
session.connect(); | |
try { | |
await new Promise((resolve, reject) => { | |
session.post('Profiler.enable', { enabled: true }, (err) => { | |
if (err) reject(err); | |
else resolve(); | |
}); | |
}); | |
await new Promise((resolve, reject) => { | |
session.post('Profiler.start', (err) => { | |
if (err) reject(err); | |
else resolve(); | |
}); | |
}); | |
await task(); | |
return await new Promise((resolve, reject) => { | |
session.post('Profiler.stop', (err, { profile }) => { | |
if (err) reject(err); | |
else resolve(profile); | |
}); | |
}); | |
} finally { | |
session.disconnect(); | |
} | |
} | |
module.exports = runProfile; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment