Surge_by_SukkaW/Build/lib/trace-runner.ts
2023-11-15 22:45:59 +08:00

34 lines
1.0 KiB
TypeScript

import path from 'path';
const traceSync = <T>(prefix: string, fn: () => T): T => {
const start = performance.now();
const result = fn();
const end = performance.now();
console.log(`${prefix}: ${(end - start).toFixed(3)}ms`);
return result;
};
export { traceSync };
const traceAsync = async <T>(prefix: string, fn: () => Promise<T>): Promise<T> => {
const start = performance.now();
const result = await fn();
const end = performance.now();
console.log(`${prefix}: ${(end - start).toFixed(3)}ms`);
return result;
};
export { traceAsync };
const task = <T>(__filename: string, fn: () => Promise<T>, customname: string | null = null) => {
const taskName = customname ?? path.basename(__filename, path.extname(__filename));
return async () => {
console.log(`🏃 [${taskName}] Start executing`);
const start = performance.now();
await fn();
const end = performance.now();
console.log(`✅ [${taskName}] Executed successfully: ${(end - start).toFixed(3)}ms`);
return { start, end, taskName } as const;
};
};
export { task };