Add option to log all query times
Co-authored-by: Scott Nonnenberg <scott@signal.org>
This commit is contained in:
parent
9d03d9b59c
commit
f400d39466
2 changed files with 13 additions and 3 deletions
|
@ -88,6 +88,8 @@ export class MainSQL {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
private onResponse = new Map<number, PromisePair<any>>();
|
private onResponse = new Map<number, PromisePair<any>>();
|
||||||
|
|
||||||
|
private shouldTimeQueries = false;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const scriptDir = join(app.getAppPath(), 'ts', 'sql', 'mainWorker.js');
|
const scriptDir = join(app.getAppPath(), 'ts', 'sql', 'mainWorker.js');
|
||||||
this.worker = new Worker(scriptDir);
|
this.worker = new Worker(scriptDir);
|
||||||
|
@ -132,6 +134,8 @@ export class MainSQL {
|
||||||
throw new Error('Already initialized');
|
throw new Error('Already initialized');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.shouldTimeQueries = Boolean(process.env.TIME_QUERIES);
|
||||||
|
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
|
||||||
this.onReady = this.send({
|
this.onReady = this.send({
|
||||||
|
@ -193,9 +197,15 @@ export class MainSQL {
|
||||||
args,
|
args,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (this.shouldTimeQueries && !app.isPackaged) {
|
||||||
|
const twoDecimals = Math.round(100 * duration) / 100;
|
||||||
|
this.logger?.info(`MainSQL query: ${method}, duration=${twoDecimals}ms`);
|
||||||
|
}
|
||||||
if (duration > MIN_TRACE_DURATION) {
|
if (duration > MIN_TRACE_DURATION) {
|
||||||
strictAssert(this.logger !== undefined, 'Logger not initialized');
|
strictAssert(this.logger !== undefined, 'Logger not initialized');
|
||||||
this.logger.info(`MainSQL: slow query ${method} duration=${duration}ms`);
|
this.logger.info(
|
||||||
|
`MainSQL: slow query ${method} duration=${Math.round(duration)}ms`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -96,9 +96,9 @@ port.on('message', async ({ seq, request }: WrappedWorkerRequest) => {
|
||||||
throw new Error(`Invalid sql method: ${method}`);
|
throw new Error(`Invalid sql method: ${method}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const start = Date.now();
|
const start = performance.now();
|
||||||
const result = await method.apply(db, request.args);
|
const result = await method.apply(db, request.args);
|
||||||
const end = Date.now();
|
const end = performance.now();
|
||||||
|
|
||||||
respond(seq, undefined, { result, duration: end - start });
|
respond(seq, undefined, { result, duration: end - start });
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue