mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-04 22:21:30 +00:00
refactor(profiler): move from logger module
This commit is contained in:
1 parent
c38f646bb9
commit
35c6a9ee7f
5 files changed
+71
-55
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
import { logError, logQuery, profileBatchStatements, runProfiler } from '../logger';
|
import { logError, logQuery } from '../logger';
|
||||||
import { CFXCallback, CFXParameters, QueryType } from '../types';
|
import { CFXCallback, CFXParameters, QueryType } from '../types';
|
||||||
import { parseResponse } from '../utils/parseResponse';
|
import { parseResponse } from '../utils/parseResponse';
|
||||||
import { executeType, parseExecute } from '../utils/parseExecute';
|
import { executeType, parseExecute } from '../utils/parseExecute';
|
||||||
@@ -7,6 +7,7 @@ import { setCallback } from '../utils/setCallback';
|
|||||||
import { performance } from 'perf_hooks';
|
import { performance } from 'perf_hooks';
|
||||||
import validateResultSet from 'utils/validateResultSet';
|
import validateResultSet from 'utils/validateResultSet';
|
||||||
import { RowDataPacket } from 'mysql2';
|
import { RowDataPacket } from 'mysql2';
|
||||||
|
import { profileBatchStatements, runProfiler } from 'profiler';
|
||||||
|
|
||||||
export const rawExecute = async (
|
export const rawExecute = async (
|
||||||
invokingResource: string,
|
invokingResource: string,
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import { parseArguments } from '../utils/parseArguments';
|
import { parseArguments } from '../utils/parseArguments';
|
||||||
import { setCallback } from '../utils/setCallback';
|
import { setCallback } from '../utils/setCallback';
|
||||||
import { parseResponse } from '../utils/parseResponse';
|
import { parseResponse } from '../utils/parseResponse';
|
||||||
import { logQuery, logError, runProfiler } from '../logger';
|
import { logQuery, logError } from '../logger';
|
||||||
import type { CFXCallback, CFXParameters } from '../types';
|
import type { CFXCallback, CFXParameters } from '../types';
|
||||||
import type { QueryType } from '../types';
|
import type { QueryType } from '../types';
|
||||||
import { getConnection } from './connection';
|
import { getConnection } from './connection';
|
||||||
import { RowDataPacket } from 'mysql2';
|
import { RowDataPacket } from 'mysql2';
|
||||||
import { performance } from 'perf_hooks';
|
import { performance } from 'perf_hooks';
|
||||||
import validateResultSet from 'utils/validateResultSet';
|
import validateResultSet from 'utils/validateResultSet';
|
||||||
|
import { runProfiler } from 'profiler';
|
||||||
|
|
||||||
export const rawQuery = async (
|
export const rawQuery = async (
|
||||||
type: QueryType,
|
type: QueryType,
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import { getConnection } from './connection';
|
import { getConnection } from './connection';
|
||||||
import { logError, logQuery, profileBatchStatements, runProfiler } from '../logger';
|
import { logError, logQuery } from '../logger';
|
||||||
import { CFXCallback, CFXParameters, TransactionQuery } from '../types';
|
import { CFXCallback, CFXParameters, TransactionQuery } from '../types';
|
||||||
import { parseTransaction } from '../utils/parseTransaction';
|
import { parseTransaction } from '../utils/parseTransaction';
|
||||||
import { setCallback } from '../utils/setCallback';
|
import { setCallback } from '../utils/setCallback';
|
||||||
import { performance } from 'perf_hooks';
|
import { performance } from 'perf_hooks';
|
||||||
|
import { profileBatchStatements, runProfiler } from 'profiler';
|
||||||
|
|
||||||
const transactionError = (queries: { query: string; params?: CFXParameters }[], parameters: CFXParameters) => {
|
const transactionError = (queries: { query: string; params?: CFXParameters }[], parameters: CFXParameters) => {
|
||||||
`${queries.map((query) => `${query.query} ${JSON.stringify(query.params || [])}`).join('\n')}\n${JSON.stringify(
|
`${queries.map((query) => `${query.query} ${JSON.stringify(query.params || [])}`).join('\n')}\n${JSON.stringify(
|
||||||
|
|||||||
+1
-52
@@ -1,7 +1,6 @@
|
|||||||
import type { RowDataPacket } from 'mysql2/promise';
|
|
||||||
import { mysql_debug, mysql_log_size, mysql_slow_query_warning, mysql_ui } from '../config';
|
import { mysql_debug, mysql_log_size, mysql_slow_query_warning, mysql_ui } from '../config';
|
||||||
import type { CFXCallback, CFXParameters } from '../types';
|
import type { CFXCallback, CFXParameters } from '../types';
|
||||||
import { MySql, dbVersion } from '../database';
|
import { dbVersion } from '../database';
|
||||||
|
|
||||||
export function logError(
|
export function logError(
|
||||||
invokingResource: string,
|
invokingResource: string,
|
||||||
@@ -31,56 +30,6 @@ export function logError(
|
|||||||
console.error(output);
|
console.error(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const profilerStatements = [
|
|
||||||
'SET profiling_history_size = 0',
|
|
||||||
'SET profiling = 0',
|
|
||||||
'SET profiling_history_size = 100',
|
|
||||||
'SET profiling = 1',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes MySQL queries to fetch accurate query profiling results when `mysql_debug` is enabled.
|
|
||||||
*/
|
|
||||||
export async function runProfiler(connection: MySql, invokingResource: string) {
|
|
||||||
if (!mysql_debug) return;
|
|
||||||
|
|
||||||
if (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: MySql,
|
|
||||||
invokingResource: string,
|
|
||||||
query: string | { query: string; params?: CFXParameters }[],
|
|
||||||
parameters: CFXParameters | null,
|
|
||||||
offset: number
|
|
||||||
) {
|
|
||||||
const profiler = <RowDataPacket[]>(
|
|
||||||
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);
|
|
||||||
|
|
||||||
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 + 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, parseFloat(profiler[i].duration), transaction.params);
|
|
||||||
else break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface QueryData {
|
interface QueryData {
|
||||||
date: number;
|
date: number;
|
||||||
query: string;
|
query: string;
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
import { mysql_debug } from 'config';
|
||||||
|
import type { MySql } from 'database';
|
||||||
|
import { logQuery } from 'logger';
|
||||||
|
import type { RowDataPacket } from 'mysql2';
|
||||||
|
import type { CFXParameters } from 'types';
|
||||||
|
|
||||||
|
const profilerStatements = [
|
||||||
|
'SET profiling_history_size = 0',
|
||||||
|
'SET profiling = 0',
|
||||||
|
'SET profiling_history_size = 100',
|
||||||
|
'SET profiling = 1',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes MySQL queries to enable accurate query profiling results when `mysql_debug` is enabled.
|
||||||
|
*/
|
||||||
|
export async function runProfiler(connection: MySql, invokingResource: string) {
|
||||||
|
if (!mysql_debug) return;
|
||||||
|
|
||||||
|
if (Array.isArray(mysql_debug) && !mysql_debug.includes(invokingResource)) return;
|
||||||
|
|
||||||
|
for (const statement of profilerStatements) await connection.query(statement);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the duration of the last 100 queries and resets profiling history.
|
||||||
|
*/
|
||||||
|
export async function profileBatchStatements(
|
||||||
|
connection: MySql,
|
||||||
|
invokingResource: string,
|
||||||
|
query: string | { query: string; params?: CFXParameters }[],
|
||||||
|
parameters: CFXParameters | null,
|
||||||
|
offset: number
|
||||||
|
) {
|
||||||
|
const profiler = <RowDataPacket[]>(
|
||||||
|
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);
|
||||||
|
|
||||||
|
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 + i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof query === 'object') {
|
||||||
|
for (let i = 0; i < profiler.length; i++) {
|
||||||
|
const transaction = query[offset + i];
|
||||||
|
|
||||||
|
if (!transaction) break;
|
||||||
|
|
||||||
|
logQuery(invokingResource, transaction.query, parseFloat(profiler[i].duration), transaction.params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user