SqliteWorker: only call perf functions when debugging is enabled

This commit is contained in:
Alex Gleason
2024-04-08 08:51:04 -05:00
parent f17e5d57f9
commit 4e999d0f39
2 changed files with 15 additions and 10 deletions

View File

@@ -1,10 +1,10 @@
/// <reference lib="webworker" />
import { ScopedPerformance } from 'https://deno.land/x/scoped_performance@v2.0.0/mod.ts';
import { Comlink, type CompiledQuery, Debug, DenoSqlite3, type QueryResult } from '@/deps.ts';
import { Comlink, type CompiledQuery, DenoSqlite3, type QueryResult, Stickynotes } from '@/deps.ts';
import '@/sentry.ts';
let db: DenoSqlite3 | undefined;
const debug = Debug('ditto:sqlite.worker');
const console = new Stickynotes('ditto:sqlite.worker');
export const SqliteWorker = {
open(path: string): void {
@@ -13,8 +13,11 @@ export const SqliteWorker = {
executeQuery<R>({ sql, parameters }: CompiledQuery): QueryResult<R> {
if (!db) throw new Error('Database not open');
const perf = new ScopedPerformance();
perf.mark('start');
const perf = (console.enabled && console.level >= 4) ? new ScopedPerformance() : undefined;
if (perf) {
perf.mark('start');
}
const result = {
rows: db!.prepare(sql).all(...parameters as any[]) as R[],
@@ -22,11 +25,13 @@ export const SqliteWorker = {
insertId: BigInt(db!.lastInsertRowId),
};
const { duration } = perf.measure('end', 'start');
debug(`${sql} \x1b[90m(${(duration / 1000).toFixed(2)}s)\x1b[0m`);
if (perf) {
const { duration } = perf.measure('end', 'start');
console.debug(`${sql} \x1b[90m(${(duration / 1000).toFixed(2)}s)\x1b[0m`);
perf.clearMarks();
perf.clearMeasures();
perf.clearMarks();
perf.clearMeasures();
}
return result;
},