From da40167b91cd61e56b7f9946c1e9e1c000e8bb94 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Sat, 26 Oct 2024 19:59:57 +0900 Subject: [PATCH] Refactor globalApi.ts to add PerformanceDebugger class for performance measurement --- src/ts/storage/globalApi.ts | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/ts/storage/globalApi.ts b/src/ts/storage/globalApi.ts index d89ce327..fe89a82f 100644 --- a/src/ts/storage/globalApi.ts +++ b/src/ts/storage/globalApi.ts @@ -2052,4 +2052,58 @@ export async function loadInternalBackup(){ +} + +/** + * A debugging class for performance measurement. +*/ + +export class PerformanceDebugger{ + kv:{[key:string]:number[]} = {} + start:number + end:number + + /** + * Starts the timing measurement. + */ + startTiming(){ + this.start = performance.now() + } + + /** + * Ends the timing measurement and records the time difference. + * + * @param {string} key - The key to associate with the recorded time. + */ + endAndRecord(key:string){ + this.end = performance.now() + if(!this.kv[key]){ + this.kv[key] = [] + } + this.kv[key].push(this.end - this.start) + } + + /** + * Ends the timing measurement, records the time difference, and starts a new timing measurement. + * + * @param {string} key - The key to associate with the recorded time. + */ + endAndRecordAndStart(key:string){ + this.endAndRecord(key) + this.startTiming() + } + + /** + * Logs the average time for each key to the console. + */ + log(){ + let table:{[key:string]:number} = {} + + for(const key in this.kv){ + table[key] = this.kv[key].reduce((a,b) => a + b, 0) / this.kv[key].length + } + + + console.table(table) + } } \ No newline at end of file