feat: print performance logs to console.

This commit is contained in:
鲁树人
2023-05-21 17:58:54 +01:00
parent c4e3999546
commit 4cfc672646
12 changed files with 171 additions and 92 deletions

View File

@@ -8,6 +8,6 @@ export class DecryptionQueue extends ConcurrentQueue<{ id: string; blobURI: stri
}
async handler(item: { id: string; blobURI: string }): Promise<DecryptionResult> {
return this.workerClientBus.request(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, item.blobURI);
return this.workerClientBus.request(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, item);
}
}

View File

@@ -24,6 +24,9 @@ test('should be able to forward request to worker client bus', async () => {
const queue = new DecryptionQueue(bus, 1);
await expect(queue.add({ id: 'file://1', blobURI: 'blob://mock-file' })).resolves.toEqual({
actionName: DECRYPTION_WORKER_ACTION_NAME.DECRYPT,
payload: 'blob://mock-file',
payload: {
blobURI: 'blob://mock-file',
id: 'file://1',
},
});
});

22
src/util/timedLogger.ts Normal file
View File

@@ -0,0 +1,22 @@
function isPromise<T = unknown>(p: unknown): p is Promise<T> {
return !!p && typeof p === 'object' && 'then' in p && 'catch' in p && 'finally' in p;
}
export function timedLogger<R = unknown>(label: string, fn: () => R): R {
console.time(label);
try {
const result = fn();
if (isPromise(result)) {
result.finally(() => {
console.timeEnd(label);
});
}
return result;
} catch (e) {
console.timeEnd(label);
throw e;
}
}