-
Notifications
You must be signed in to change notification settings - Fork 6
/
slowLogAnalyzer.js
182 lines (160 loc) · 6.31 KB
/
slowLogAnalyzer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"use strict";
const LineByLineReader = require('line-by-line');
const fs = require("fs");
const moment = require("moment");
const queryTimingsCSVFilename = "query-timings-mysql.csv";
const connectionsCSVFilename = "connections-mysql.csv";
/**
* Google sheets allows a maximum of 50k characters in a cell.
*/
const MAX_CELL_STRING_LENGTH = 50000;
async function slowLogAnalyzer() {
const firstArg = process.argv[2];
if ((process.argv.length < 3 || process.argv.length > 4)
|| firstArg === "-h"
|| firstArg === "--help"
|| firstArg === "-?") {
console.log("\nMySQL Slow Log Analyzer help:\n\n");
console.log("node slowLogAnalyzer.js [--cloudwatch-format] <filename-of-slow-query-log.log>");
process.exit(1);
}
let cloudwatchFormat = firstArg === "--cloudwatch-format";
let filename;
if (cloudwatchFormat) {
filename = process.argv[3];
} else {
filename = firstArg;
}
console.log(`Reading ${filename}...`);
const lineReader = new LineByLineReader(filename);
let queryToTimingsMap = new Map();
let currentTiming = {count: 1, query: ""};
let linesReadSoFar = 0;
let startTime = Date.now();
lineReader.on("error", error => {
console.error("Error:", error);
})
lineReader.on("line", line => {
lineReader.pause();
linesReadSoFar++;
if (linesReadSoFar % 10000 === 0) {
const timeSoFar = (Date.now() - startTime) / 1000;
console.log(`${linesReadSoFar.toLocaleString()} lines read so far. Unique queries found: ${queryToTimingsMap.size.toLocaleString()}. Time taken: ${timeSoFar.toFixed(3).toLocaleString()}s`);
startTime = Date.now();
}
if (cloudwatchFormat ? line.includes("# Time:") : line.startsWith("# Time:")) {
// Commit this query to the map
let query = currentTiming.query;
if (query) {
query = cleanUpQuery(query);
if (queryToTimingsMap.has(query)) {
let queryTiming = queryToTimingsMap.get(query);
queryTiming.totalTime += currentTiming.queryTime + currentTiming.lockTime;
queryTiming.queryTime += currentTiming.queryTime;
queryTiming.lockTime += currentTiming.lockTime;
queryTiming.count += 1;
} else {
currentTiming.totalTime = currentTiming.queryTime + currentTiming.lockTime;
currentTiming.query = query;
queryToTimingsMap.set(query, currentTiming);
}
}
// Reset everything
currentTiming = {count: 1, query: ""};
} else if (line.startsWith("# Query_time")) {
const match = line.match(/# Query_time: ([0-9.]+) +Lock_time: ([0-9.]+) +Rows_sent: ([0-9.]+) +Rows_examined: ([0-9.]+)/);
currentTiming.queryTime = parseFloat(match[1]);
currentTiming.lockTime = parseFloat(match[2]);
} else if (line.startsWith("SET timestamp=")) {
const match = line.match(/SET timestamp=([0-9]+)/);
currentTiming.unixTimestamp = parseInt(match[1]);
} else if (line.startsWith("# Thread_id:")) {
const match = line.match(/# Thread_id: *([0-9]+)/);
currentTiming.connectionId = parseInt(match[1]);
} else if (line.startsWith("# User@Host")) {
const match = line.match(/# User@Host: .* Id: *([0-9]+)/);
currentTiming.connectionId = parseInt(match[1]);
} else if (line.startsWith("#")
|| line.startsWith("Tcp port:")
|| line.startsWith("use ")
|| line.startsWith("/* ")
|| line.startsWith("/rdsdbbin")
|| line.startsWith("@timestamp,@message")
|| line.startsWith("Time ")) {
// Ignore
} else {
currentTiming.query += line;
}
lineReader.resume();
});
lineReader.on("end", () => {
writeTimingsCSV(queryToTimingsMap);
writeConnectionsCSV(queryToTimingsMap);
});
}
function cleanUpQuery(query) {
let cleanQuery = query;
// Convert all numbers to ?
cleanQuery = cleanQuery.replace(/[-+]?[0-9]*\.?[0-9]+/g, "?");
// Convert all strings to ?
cleanQuery = cleanQuery.replace(/'[^']*'/g, "?");
// Remove trailing double quotes
if(cleanQuery.endsWith("\"")) {
cleanQuery = cleanQuery.substring(0, cleanQuery.length-2);
}
return cleanQuery;
}
function writeTimingsCSV(queryToTimingsMap) {
console.log("Writing timings to " + queryTimingsCSVFilename);
let wstream = fs.createWriteStream(queryTimingsCSVFilename, {encoding: "utf8"});
wstream.write(`"Total Time","Total Query Time","Total Lock Time","Average Time","Count","Query"\n`);
// Arrange the data
const values = queryToTimingsMap.values();
const sortedByTime = Array.from(values).sort((v1, v2) => v2.totalTime - v1.totalTime);
// Write to the CSV file
for (const timing of sortedByTime) {
wstream.write(`${timing.totalTime},${timing.queryTime},${timing.lockTime},${timing.totalTime / timing.count},${timing.count},"${cleanStringForCSV(timing.query)}"\n`);
}
wstream.end();
}
/**
* This function make sure that Google Sheets/Excel can read the text.
*/
function cleanStringForCSV(someText) {
let returnVal = someText.replace(/"/g, '""');
return returnVal.length > MAX_CELL_STRING_LENGTH ?
returnVal.substring(0, MAX_CELL_STRING_LENGTH - 3) + "..." :
returnVal;
}
function writeConnectionsCSV(queryToTimingsMap) {
console.log("Writing connections to " + connectionsCSVFilename);
let wstream = fs.createWriteStream(connectionsCSVFilename, {encoding: "utf8"});
// Arrange the data
let values = queryToTimingsMap.values();
values = Array.from(values);
const timestampToQueriesMap = new Map();
for (const value of values) {
if (timestampToQueriesMap.has(value.unixTimestamp)) {
const queries = timestampToQueriesMap.get(value.unixTimestamp);
queries.push(value.query);
} else {
timestampToQueriesMap.set(value.unixTimestamp, [value.query]);
}
}
const timings = Array.from(timestampToQueriesMap.keys()).map(timestamp => {
let queries = timestampToQueriesMap.get(timestamp);
return {
unixTimestamp: timestamp,
count: queries.length,
queries: queries,
};
});
const sortedByTime = timings.sort((v1, v2) => v1.unixTimestamp - v2.unixTimestamp);
// Write to the CSV file
wstream.write(`"Time","Connection Count","Queries"\n`);
for (const timing of sortedByTime) {
wstream.write(`${moment.unix(timing.unixTimestamp).format("YYYY-MM-DD HH:mm:ss")},${timing.count},"${cleanStringForCSV(timing.queries.join("\n"))}"\n`);
}
wstream.end();
}
slowLogAnalyzer();