Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pmuellr
Created February 13, 2017 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmuellr/20d570af9d22d056371752666895cbf2 to your computer and use it in GitHub Desktop.
Save pmuellr/20d570af9d22d056371752666895cbf2 to your computer and use it in GitHub Desktop.
run an N|Solid profile on your startup code
'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