mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2024-12-20 19:06:06 +01:00
Add timing utils
This commit is contained in:
parent
4dec8c265d
commit
f22f3361d5
27
server/utils/timing.js
Normal file
27
server/utils/timing.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
const { performance } = require('perf_hooks')
|
||||||
|
const Logger = require('../Logger')
|
||||||
|
|
||||||
|
|
||||||
|
async function measure(tag, func) {
|
||||||
|
const start = performance.now()
|
||||||
|
const result = await func()
|
||||||
|
const end = performance.now()
|
||||||
|
Logger.debug(`[${tag}] Time elapsed: ${(end - start) | 0} ms`)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
function measureMiddleware(req, res, next) {
|
||||||
|
const start = performance.now()
|
||||||
|
res.on('finish', () => {
|
||||||
|
const end = performance.now()
|
||||||
|
if (!req.originalUrl.includes('cover'))
|
||||||
|
Logger.debug(`[${req.method} ${req.originalUrl}] Finish: Time elapsed: ${(end - start) | 0} ms`)
|
||||||
|
})
|
||||||
|
res.on('close', () => {
|
||||||
|
const end = performance.now()
|
||||||
|
if (!req.originalUrl.includes('cover'))
|
||||||
|
Logger.debug(`[${req.method} ${req.originalUrl}] Close: Time elapsed: ${(end - start) | 0} ms`)
|
||||||
|
})
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
module.exports = { measure, measureMiddleware }
|
Loading…
Reference in New Issue
Block a user