Skip to content
This repository was archived by the owner on Apr 29, 2025. It is now read-only.

Commit 35c6a9e

Browse files
committed
refactor(profiler): move from logger module
1 parent c38f646 commit 35c6a9e

File tree

5 files changed

+71
-55
lines changed

5 files changed

+71
-55
lines changed

src/database/rawExecute.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { logError, logQuery, profileBatchStatements, runProfiler } from '../logger';
1+
import { logError, logQuery } from '../logger';
22
import { CFXCallback, CFXParameters, QueryType } from '../types';
33
import { parseResponse } from '../utils/parseResponse';
44
import { executeType, parseExecute } from '../utils/parseExecute';
@@ -7,6 +7,7 @@ import { setCallback } from '../utils/setCallback';
77
import { performance } from 'perf_hooks';
88
import validateResultSet from 'utils/validateResultSet';
99
import { RowDataPacket } from 'mysql2';
10+
import { profileBatchStatements, runProfiler } from 'profiler';
1011

1112
export const rawExecute = async (
1213
invokingResource: string,

src/database/rawQuery.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { parseArguments } from '../utils/parseArguments';
22
import { setCallback } from '../utils/setCallback';
33
import { parseResponse } from '../utils/parseResponse';
4-
import { logQuery, logError, runProfiler } from '../logger';
4+
import { logQuery, logError } from '../logger';
55
import type { CFXCallback, CFXParameters } from '../types';
66
import type { QueryType } from '../types';
77
import { getConnection } from './connection';
88
import { RowDataPacket } from 'mysql2';
99
import { performance } from 'perf_hooks';
1010
import validateResultSet from 'utils/validateResultSet';
11+
import { runProfiler } from 'profiler';
1112

1213
export const rawQuery = async (
1314
type: QueryType,

src/database/rawTransaction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { getConnection } from './connection';
2-
import { logError, logQuery, profileBatchStatements, runProfiler } from '../logger';
2+
import { logError, logQuery } from '../logger';
33
import { CFXCallback, CFXParameters, TransactionQuery } from '../types';
44
import { parseTransaction } from '../utils/parseTransaction';
55
import { setCallback } from '../utils/setCallback';
66
import { performance } from 'perf_hooks';
7+
import { profileBatchStatements, runProfiler } from 'profiler';
78

89
const transactionError = (queries: { query: string; params?: CFXParameters }[], parameters: CFXParameters) => {
910
`${queries.map((query) => `${query.query} ${JSON.stringify(query.params || [])}`).join('\n')}\n${JSON.stringify(

src/logger/index.ts

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { RowDataPacket } from 'mysql2/promise';
21
import { mysql_debug, mysql_log_size, mysql_slow_query_warning, mysql_ui } from '../config';
32
import type { CFXCallback, CFXParameters } from '../types';
4-
import { MySql, dbVersion } from '../database';
3+
import { dbVersion } from '../database';
54

65
export function logError(
76
invokingResource: string,
@@ -31,56 +30,6 @@ export function logError(
3130
console.error(output);
3231
}
3332

34-
export const profilerStatements = [
35-
'SET profiling_history_size = 0',
36-
'SET profiling = 0',
37-
'SET profiling_history_size = 100',
38-
'SET profiling = 1',
39-
];
40-
41-
/**
42-
* Executes MySQL queries to fetch accurate query profiling results when `mysql_debug` is enabled.
43-
*/
44-
export async function runProfiler(connection: MySql, invokingResource: string) {
45-
if (!mysql_debug) return;
46-
47-
if (Array.isArray(mysql_debug) && !mysql_debug.includes(invokingResource)) return;
48-
49-
for (const statement of profilerStatements) await connection.query(statement);
50-
51-
return true;
52-
}
53-
54-
export async function profileBatchStatements(
55-
connection: MySql,
56-
invokingResource: string,
57-
query: string | { query: string; params?: CFXParameters }[],
58-
parameters: CFXParameters | null,
59-
offset: number
60-
) {
61-
const profiler = <RowDataPacket[]>(
62-
await connection.query(
63-
'SELECT FORMAT(SUM(DURATION) * 1000, 4) AS `duration` FROM INFORMATION_SCHEMA.PROFILING GROUP BY QUERY_ID'
64-
)
65-
);
66-
67-
for (const statement of profilerStatements) await connection.query(statement);
68-
69-
if (profiler.length === 0) return;
70-
71-
if (typeof query === 'string' && parameters)
72-
for (let i = 0; i < profiler.length; i++) {
73-
logQuery(invokingResource, query, parseFloat(profiler[i].duration), parameters[offset + i]);
74-
}
75-
else if (typeof query === 'object')
76-
for (let i = 0; i < profiler.length; i++) {
77-
const transaction = query[offset + i];
78-
if (transaction)
79-
logQuery(invokingResource, transaction.query, parseFloat(profiler[i].duration), transaction.params);
80-
else break;
81-
}
82-
}
83-
8433
interface QueryData {
8534
date: number;
8635
query: string;

src/profiler/index.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { mysql_debug } from 'config';
2+
import type { MySql } from 'database';
3+
import { logQuery } from 'logger';
4+
import type { RowDataPacket } from 'mysql2';
5+
import type { CFXParameters } from 'types';
6+
7+
const profilerStatements = [
8+
'SET profiling_history_size = 0',
9+
'SET profiling = 0',
10+
'SET profiling_history_size = 100',
11+
'SET profiling = 1',
12+
];
13+
14+
/**
15+
* Executes MySQL queries to enable accurate query profiling results when `mysql_debug` is enabled.
16+
*/
17+
export async function runProfiler(connection: MySql, invokingResource: string) {
18+
if (!mysql_debug) return;
19+
20+
if (Array.isArray(mysql_debug) && !mysql_debug.includes(invokingResource)) return;
21+
22+
for (const statement of profilerStatements) await connection.query(statement);
23+
24+
return true;
25+
}
26+
27+
/**
28+
* Fetches the duration of the last 100 queries and resets profiling history.
29+
*/
30+
export async function profileBatchStatements(
31+
connection: MySql,
32+
invokingResource: string,
33+
query: string | { query: string; params?: CFXParameters }[],
34+
parameters: CFXParameters | null,
35+
offset: number
36+
) {
37+
const profiler = <RowDataPacket[]>(
38+
await connection.query(
39+
'SELECT FORMAT(SUM(DURATION) * 1000, 4) AS `duration` FROM INFORMATION_SCHEMA.PROFILING GROUP BY QUERY_ID'
40+
)
41+
);
42+
43+
for (const statement of profilerStatements) await connection.query(statement);
44+
45+
if (profiler.length === 0) return;
46+
47+
if (typeof query === 'string' && parameters) {
48+
for (let i = 0; i < profiler.length; i++) {
49+
logQuery(invokingResource, query, parseFloat(profiler[i].duration), parameters[offset + i]);
50+
}
51+
52+
return;
53+
}
54+
55+
if (typeof query === 'object') {
56+
for (let i = 0; i < profiler.length; i++) {
57+
const transaction = query[offset + i];
58+
59+
if (!transaction) break;
60+
61+
logQuery(invokingResource, transaction.query, parseFloat(profiler[i].duration), transaction.params);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)