From f22f3361d5f8e853fce29290909aa62ea9ab3c15 Mon Sep 17 00:00:00 2001 From: mikiher Date: Fri, 17 Nov 2023 08:48:09 +0200 Subject: [PATCH] Add timing utils --- server/utils/timing.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 server/utils/timing.js diff --git a/server/utils/timing.js b/server/utils/timing.js new file mode 100644 index 00000000..af019e78 --- /dev/null +++ b/server/utils/timing.js @@ -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 } \ No newline at end of file