Last active
January 21, 2016 13:54
-
-
Save naholyr/e1dc36cc519f05fd3954 to your computer and use it in GitHub Desktop.
Time a middleware
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
// generic reusable | |
app.use(timeMiddleware('session', session)); | |
// … | |
function timeMiddleware (description, middleware) { | |
return function (req, res, next) { | |
var start = process.hrtime(); | |
middleware(req, res, function (err) { | |
var diff = process.hrtime(req.timeStart); | |
var time = diff[0] / 1000 + diff[1] / 1000000; | |
console.log('middleware ' + description + ' (ms):', time); | |
next(err); | |
}); | |
}; | |
} |
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
// time the whole thing | |
app.use(function (req, res, next) { | |
req.timeStart = process.hrtime(); | |
next(); | |
}); | |
// all the app.use you want to time globally | |
app.use(session); | |
app.use(function (req, res, next) { | |
var diff = process.hrtime(req.timeStart); | |
var time = diff[0] / 1000 + diff[1] / 1000000; | |
console.log('middlewares (ms):', time); | |
next(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment