Skip to content

Instantly share code, notes, and snippets.

@barbagrigia
Forked from sochix/app.js
Created April 12, 2022 10:41
Show Gist options
  • Save barbagrigia/2519c28c0881f93f969b006f884cb912 to your computer and use it in GitHub Desktop.
Save barbagrigia/2519c28c0881f93f969b006f884cb912 to your computer and use it in GitHub Desktop.
Proper way to measure request duration in a Node.js + Express application
const app = require('express')()
const getDurationInMilliseconds = (start) => {
const NS_PER_SEC = 1e9
const NS_TO_MS = 1e6
const diff = process.hrtime(start)
return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS
}
app.use((req, res, next) => {
console.log(`${req.method} ${req.originalUrl} [STARTED]`)
const start = process.hrtime()
res.on('finish', () => {
const durationInMilliseconds = getDurationInMilliseconds (start)
console.log(`${req.method} ${req.originalUrl} [FINISHED] ${durationInMilliseconds.toLocaleString()} ms`)
})
res.on('close', () => {
const durationInMilliseconds = getDurationInMilliseconds (start)
console.log(`${req.method} ${req.originalUrl} [CLOSED] ${durationInMilliseconds.toLocaleString()} ms`)
})
next()
})
// send response immediately
app.get('/fast/', (req, res) => {
res.sendStatus(200)
})
// mock heavy load, send response after 10 seconds
app.get('/slow/', (req, res) => {
setTimeout(() => res.sendStatus(200), 10 * 1000)
})
app.listen(3000, () => {
console.log('Server started')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment