mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 14:38:50 +00:00
refactor(logger): non-debug query profiling
Use less precise query profiling (script based, rather than sql server based). Re-enables slow query warning, and reduces overhead when ui is enabled.
This commit is contained in:
1 parent
d0683b7f30
commit
e31d84019e
4 files changed
+30
-14
No files matched your search
+15
-10
@@ -10,9 +10,11 @@ export function logError(
|
||||
err: any,
|
||||
query?: string,
|
||||
parameters?: CFXParameters,
|
||||
includeParameters?: boolean,
|
||||
includeParameters?: boolean
|
||||
) {
|
||||
const message = `${invokingResource} was unable to execute a query!${query ? `\n${`Query: ${query}`}` : ''}${includeParameters ? `\n${JSON.stringify(parameters)}` : ''}\n${err.message}`;
|
||||
const message = `${invokingResource} was unable to execute a query!${query ? `\n${`Query: ${query}`}` : ''}${
|
||||
includeParameters ? `\n${JSON.stringify(parameters)}` : ''
|
||||
}\n${err.message}`;
|
||||
|
||||
TriggerEvent('oxmysql:error', {
|
||||
query: query,
|
||||
@@ -34,9 +36,13 @@ export const profilerStatements = [
|
||||
'SET profiling = 1',
|
||||
];
|
||||
|
||||
/**
|
||||
* Executes MySQL queries to fetch accurate query profiling results when `mysql_debug` is enabled.
|
||||
*/
|
||||
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;
|
||||
if (!mysql_debug) return;
|
||||
|
||||
if (Array.isArray(mysql_debug) && !mysql_debug.includes(invokingResource)) return;
|
||||
|
||||
for (const statement of profilerStatements) await connection.query(statement);
|
||||
|
||||
@@ -62,12 +68,13 @@ export async function profileBatchStatements(
|
||||
|
||||
if (typeof query === 'string' && parameters)
|
||||
for (let i = 0; i < profiler.length; i++) {
|
||||
logQuery(invokingResource, query, profiler[i].duration, parameters[offset + i]);
|
||||
logQuery(invokingResource, query, parseFloat(profiler[i].duration), parameters[offset + i]);
|
||||
}
|
||||
else if (typeof query === 'object')
|
||||
for (let i = 0; i < profiler.length; i++) {
|
||||
const transaction = query[offset + i];
|
||||
if (transaction) logQuery(invokingResource, transaction.query, profiler[i].duration, transaction.params);
|
||||
if (transaction)
|
||||
logQuery(invokingResource, transaction.query, parseFloat(profiler[i].duration), transaction.params);
|
||||
else break;
|
||||
}
|
||||
}
|
||||
@@ -86,17 +93,15 @@ const logStorage: QueryLog = {};
|
||||
export const logQuery = (
|
||||
invokingResource: string,
|
||||
query: string,
|
||||
executionTime: string | number,
|
||||
executionTime: number,
|
||||
parameters?: CFXParameters
|
||||
) => {
|
||||
executionTime = parseFloat(executionTime as string);
|
||||
|
||||
if (
|
||||
executionTime >= mysql_slow_query_warning ||
|
||||
(mysql_debug && (!Array.isArray(mysql_debug) || mysql_debug.includes(invokingResource)))
|
||||
) {
|
||||
console.log(
|
||||
`${dbVersion} ^3${invokingResource} took ${executionTime}ms to execute a query!\n${query}${
|
||||
`${dbVersion} ^3${invokingResource} took ${executionTime.toFixed(4)}ms to execute a query!\n${query}${
|
||||
parameters ? ` ${JSON.stringify(parameters)}` : ''
|
||||
}^0`
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user