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
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 15 additions and 10 deletions

View File

@ -77,8 +77,8 @@ export { sentry as sentryMiddleware } from 'npm:@hono/sentry@^1.0.0';
export * as Comlink from 'npm:comlink@^4.4.1'; export * as Comlink from 'npm:comlink@^4.4.1';
export { EventEmitter } from 'npm:tseep@^1.1.3'; export { EventEmitter } from 'npm:tseep@^1.1.3';
export { default as stringifyStable } from 'npm:fast-stable-stringify@^1.0.0'; export { default as stringifyStable } from 'npm:fast-stable-stringify@^1.0.0';
export { default as Debug } from 'https://gitlab.com/soapbox-pub/stickynotes/-/raw/v0.2.0/debug.ts'; export { default as Debug } from 'https://gitlab.com/soapbox-pub/stickynotes/-/raw/v0.3.0/debug.ts';
export { Stickynotes } from 'https://gitlab.com/soapbox-pub/stickynotes/-/raw/v0.2.0/mod.ts'; export { Stickynotes } from 'https://gitlab.com/soapbox-pub/stickynotes/-/raw/v0.3.0/mod.ts';
export { export {
LNURL, LNURL,
type LNURLDetails, type LNURLDetails,

View File

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