From 1b4bb1d2e2a18765ce97e2e790f0f34d12cbcb95 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sat, 17 Aug 2024 15:06:28 +0200 Subject: [PATCH] Measure execution time --- src/lib/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/lib/index.ts b/src/lib/index.ts index fe1270a..3106804 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,6 +1,7 @@ // place files you want to import through the `$lib` alias in this folder. import path from 'path'; import bunyan from 'bunyan'; +import type { MaybePromise } from '@sveltejs/kit'; export const log = bunyan.createLogger({ name: 'fotochallenge', @@ -29,3 +30,13 @@ if (!('STORAGE_PATH' in process.env)) { export const storagePath: string = process.env.STORAGE_PATH ?? defaultPath; export default safePath; + +export async function timedExecution( + fn: () => MaybePromise +): Promise<{ executionTime: number; result: T }> { + const start = process.hrtime(); + const result = await fn(); + const end = process.hrtime(start); + const executionTime = (end[0] * 1e6 + end[1]) / 1e6; + return { executionTime, result }; +}