Created
February 13, 2017 16:39
-
-
Save pmuellr/20d570af9d22d056371752666895cbf2 to your computer and use it in GitHub Desktop.
run an N|Solid profile on your startup code
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
'use strict' | |
// Use this module as in the following command, to profile the startup code: | |
// nsolid --require ./profile-at-startup nsolid-storage | |
const path = require('path') | |
const program = path.basename(__filename).replace(/\.js$/, '') | |
let nsolid | |
run() | |
// run main code as a function so we can early return | |
function run () { | |
try { | |
nsolid = require('nsolid') | |
} catch (err) { | |
log('Y U NOT USING N|Solid? SAD! https://nodesource.com/products/nsolid') | |
return | |
} | |
global.nsolidProfileStart = nsolidProfileStart | |
global.nsolidProfileStop = nsolidProfileStop | |
nsolidProfileStart() | |
setTimeout(nsolidProfileStop, 5000) | |
} | |
// this function gets globally installed! | |
function nsolidProfileStart () { | |
log('starting profile') | |
nsolid.profile(onProfileStarted) | |
} | |
// this function gets globally installed! | |
function nsolidProfileStop () { | |
log('stopping profile') | |
nsolid.profileEnd(onProfileEnded) | |
} | |
function onProfileStarted (err, data) { | |
log('profile started', err || '') | |
} | |
function onProfileEnded (err, data) { | |
log('profile ended', err || '') | |
} | |
// log a message | |
function log (message) { | |
console.error(`${new Date().toISOString()} ${program}: ${message}`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment