mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 14:38:50 +00:00
refactor(logger): add profileBatchStatements
This commit is contained in:
1 parent
51c84a30ca
commit
a8cdc5d1a1
2 files changed
+36
-4
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { createPool, Pool } from 'mysql2/promise';
|
||||
import { connectionOptions, mysql_transaction_isolation_level, setDebug } from '../config';
|
||||
import { profilerStatements } from '../logger';
|
||||
|
||||
let pool: Pool;
|
||||
let isServerConnected = false;
|
||||
@@ -25,10 +26,7 @@ function setConnectionPool() {
|
||||
});
|
||||
|
||||
pool.on('acquire', (connection) => {
|
||||
connection.query('SET profiling_history_size = 0');
|
||||
connection.query('SET profiling = 0');
|
||||
connection.query('SET profiling_history_size = 1000');
|
||||
connection.query('SET profiling = 1');
|
||||
for (const statement of profilerStatements) connection.query(statement);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,40 @@
|
||||
import { PoolConnection, RowDataPacket } from 'mysql2/promise';
|
||||
import { mysql_debug, mysql_slow_query_warning, mysql_ui } from '../config';
|
||||
import type { CFXParameters } from '../types';
|
||||
|
||||
export const profilerStatements = [
|
||||
'SET profiling_history_size = 0',
|
||||
'SET profiling = 0',
|
||||
'SET profiling_history_size = 100',
|
||||
'SET profiling = 1',
|
||||
];
|
||||
|
||||
export async function profileBatchStatements(
|
||||
connection: PoolConnection,
|
||||
invokingResource: string,
|
||||
query: string | { query: string; params: CFXParameters }[],
|
||||
parameters: CFXParameters | null,
|
||||
offset: number
|
||||
) {
|
||||
const [profiler] = <RowDataPacket[]>(
|
||||
await connection.query('SELECT SUM(DURATION) AS `duration` FROM INFORMATION_SCHEMA.PROFILING GROUP BY QUERY_ID')
|
||||
);
|
||||
|
||||
for (const statement of profilerStatements) await connection.query(statement);
|
||||
|
||||
if (profiler.length === 0) return;
|
||||
|
||||
if (typeof query === 'string' && parameters)
|
||||
for (let i = 0; i < profiler.length; i++) {
|
||||
logQuery(invokingResource, query, parseFloat(profiler[i].duration), parameters[offset]);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
interface QueryData {
|
||||
date: number;
|
||||
query: string;
|
||||
|
||||
Reference in new issue
Block a user