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
|
||||
private onResponse = new Map<number, PromisePair<any>>();
|
||||
|
||||
private shouldTimeQueries = false;
|
||||
|
||||
constructor() {
|
||||
const scriptDir = join(app.getAppPath(), 'ts', 'sql', 'mainWorker.js');
|
||||
this.worker = new Worker(scriptDir);
|
||||
|
@ -132,6 +134,8 @@ export class MainSQL {
|
|||
throw new Error('Already initialized');
|
||||
}
|
||||
|
||||
this.shouldTimeQueries = Boolean(process.env.TIME_QUERIES);
|
||||
|
||||
this.logger = logger;
|
||||
|
||||
this.onReady = this.send({
|
||||
|
@ -193,9 +197,15 @@ export class MainSQL {
|
|||
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) {
|
||||
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;
|
||||
|
|
|
@ -96,9 +96,9 @@ port.on('message', async ({ seq, request }: WrappedWorkerRequest) => {
|
|||
throw new Error(`Invalid sql method: ${method}`);
|
||||
}
|
||||
|
||||
const start = Date.now();
|
||||
const start = performance.now();
|
||||
const result = await method.apply(db, request.args);
|
||||
const end = Date.now();
|
||||
const end = performance.now();
|
||||
|
||||
respond(seq, undefined, { result, duration: end - start });
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue