From 45aadf744261a7d546635afaa6dc1a082cea06ad Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Wed, 12 Jul 2023 15:38:47 +1000 Subject: [PATCH] refactor(logger): only enable profiling with debugging Breaks slow query warnings, but most people just set the number higher once they get warnings anyway. Also fixes some issues: - profile duration formatting and time (sec to ms). - incorrect log query/param index. --- src/database/index.ts | 4 ---- src/database/rawExecute.ts | 7 ++++--- src/database/rawQuery.ts | 15 +++++++++++---- src/database/rawTransaction.ts | 7 ++++--- src/logger/index.ts | 29 ++++++++++++++++++++++++----- 5 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/database/index.ts b/src/database/index.ts index 8e5283b..d7c90fb 100644 --- a/src/database/index.ts +++ b/src/database/index.ts @@ -24,10 +24,6 @@ function setConnectionPool() { pool.on('connection', (connection) => { connection.query(mysql_transaction_isolation_level); }); - - pool.on('acquire', (connection) => { - for (const statement of profilerStatements) connection.query(statement); - }); } setInterval(() => { diff --git a/src/database/rawExecute.ts b/src/database/rawExecute.ts index 9cf5b8f..84314c1 100644 --- a/src/database/rawExecute.ts +++ b/src/database/rawExecute.ts @@ -1,5 +1,5 @@ import { pool } from '.'; -import { profileBatchStatements } from '../logger'; +import { profileBatchStatements, runProfiler } from '../logger'; import { CFXCallback, CFXParameters } from '../types'; import { parseResponse } from '../utils/parseResponse'; import { executeType, parseExecute } from '../utils/parseExecute'; @@ -36,6 +36,7 @@ export const rawExecute = ( if (!connection) return; const parametersLength = parameters.length == 0 ? 1 : parameters.length; + const hasProfiler = await runProfiler(connection, invokingResource); for (let index = 0; index < parametersLength; index++) { const values = parameters[index]; @@ -57,8 +58,8 @@ export const rawExecute = ( } else response.push(parseResponse(type, result)); } - if ((index > 0 && index % 99 === 0) || index === parametersLength - 1) { - await profileBatchStatements(connection, invokingResource, query, parameters, index); + if (hasProfiler && ((index > 0 && index % 100 === 0) || index === parametersLength - 1)) { + await profileBatchStatements(connection, invokingResource, query, parameters, index < 100 ? 0 : index); } if (index === parametersLength - 1) { diff --git a/src/database/rawQuery.ts b/src/database/rawQuery.ts index 6688070..fd12b1d 100644 --- a/src/database/rawQuery.ts +++ b/src/database/rawQuery.ts @@ -1,7 +1,7 @@ import { pool, isServerConnected, waitForConnection } from '.'; import { parseArguments } from '../utils/parseArguments'; import { parseResponse } from '../utils/parseResponse'; -import { logQuery } from '../logger'; +import { logQuery, runProfiler } from '../logger'; import type { CFXCallback, CFXParameters } from '../types'; import type { QueryType } from '../types'; import { scheduleTick } from '../utils/scheduleTick'; @@ -32,11 +32,18 @@ export const rawQuery = ( if (!connection) return; + const hasProfiler = await runProfiler(connection, invokingResource); + try { const [result] = await connection.query(query, parameters); - const [profiler] = await connection.query('SELECT SUM(DURATION) AS `duration` FROM INFORMATION_SCHEMA.PROFILING'); - - if (profiler[0]?.duration) logQuery(invokingResource, query, parseFloat(profiler[0].duration), parameters); + + if (hasProfiler) { + const [profiler] = ( + await connection.query('SELECT FORMAT(SUM(DURATION) * 1000, 4) AS `duration` FROM INFORMATION_SCHEMA.PROFILING') + ); + + if (profiler[0]) logQuery(invokingResource, query, profiler[0].duration, parameters); + } resolve(cb ? parseResponse(type, result) : null); } catch (err) { diff --git a/src/database/rawTransaction.ts b/src/database/rawTransaction.ts index 911d0b0..feec1d6 100644 --- a/src/database/rawTransaction.ts +++ b/src/database/rawTransaction.ts @@ -1,5 +1,5 @@ import { pool, isServerConnected, waitForConnection } from '.'; -import { profileBatchStatements } from '../logger'; +import { profileBatchStatements, runProfiler } from '../logger'; import { CFXParameters, TransactionQuery } from '../types'; import { parseTransaction } from '../utils/parseTransaction'; import { scheduleTick } from '../utils/scheduleTick'; @@ -21,6 +21,7 @@ export const rawTransaction = async ( const { transactions, cb } = parseTransaction(invokingResource, queries, parameters, callback); const connection = await pool.getConnection(); + const hasProfiler = await runProfiler(connection, invokingResource); let response = false; try { @@ -32,8 +33,8 @@ export const rawTransaction = async ( await connection.query(transaction.query, transaction.params); - if ((i > 0 && i % 99 === 0) || i === transactionsLength - 1) { - await profileBatchStatements(connection, invokingResource, transactions, null, i); + if (hasProfiler && ((i > 0 && i % 100 === 0) || i === transactionsLength - 1)) { + await profileBatchStatements(connection, invokingResource, transactions, null, i < 100 ? 0 : i); } } diff --git a/src/logger/index.ts b/src/logger/index.ts index 4b692a1..0a1fd22 100644 --- a/src/logger/index.ts +++ b/src/logger/index.ts @@ -9,6 +9,15 @@ export const profilerStatements = [ 'SET profiling = 1', ]; +export async function runProfiler(connection: PoolConnection, invokingResource: string) { + if (!mysql_debug && !mysql_ui) return; + if (!mysql_ui && mysql_debug && Array.isArray(mysql_debug) && !mysql_debug.includes(invokingResource)) return; + + for (const statement of profilerStatements) await connection.query(statement); + + return true; +} + export async function profileBatchStatements( connection: PoolConnection, invokingResource: string, @@ -17,7 +26,9 @@ export async function profileBatchStatements( offset: number ) { const [profiler] = ( - await connection.query('SELECT SUM(DURATION) AS `duration` FROM INFORMATION_SCHEMA.PROFILING GROUP BY QUERY_ID') + await connection.query( + 'SELECT FORMAT(SUM(DURATION) * 1000, 4) AS `duration` FROM INFORMATION_SCHEMA.PROFILING GROUP BY QUERY_ID' + ) ); for (const statement of profilerStatements) await connection.query(statement); @@ -26,12 +37,13 @@ export async function profileBatchStatements( if (typeof query === 'string' && parameters) for (let i = 0; i < profiler.length; i++) { - logQuery(invokingResource, query, parseFloat(profiler[i].duration), parameters[offset]); + logQuery(invokingResource, query, profiler[i].duration, parameters[offset + i]); } else if (typeof query === 'object') for (let i = 0; i < profiler.length; i++) { - const transaction = query[offset]; - logQuery(invokingResource, transaction.query, parseFloat(profiler[i].duration), transaction.params); + const transaction = query[offset + i]; + if (transaction) logQuery(invokingResource, transaction.query, profiler[i].duration, transaction.params); + else break; } } @@ -46,7 +58,14 @@ type QueryLog = Record; const logStorage: QueryLog = {}; -export const logQuery = (invokingResource: string, query: string, executionTime: number, parameters: CFXParameters) => { +export const logQuery = ( + invokingResource: string, + query: string, + executionTime: string | number, + parameters: CFXParameters +) => { + executionTime = parseFloat(executionTime as string); + if (mysql_debug && Array.isArray(mysql_debug)) { if (mysql_debug.includes(invokingResource)) { console.log(