Measure execution time

This commit is contained in:
Valentin Brandl 2024-08-17 15:06:28 +02:00
parent 824852fe7a
commit 1b4bb1d2e2
Signed by: vbrandl
GPG Key ID: CAD4DA1A789125F9

View File

@ -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<T>(
fn: () => MaybePromise<T>
): 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 };
}