| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.tools; |
| 7 |
import java.io.IOException; |
| 8 |
import java.io.LineNumberReader; |
| 9 |
import java.io.PrintWriter; |
| 10 |
import java.sql.SQLException; |
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.Collections; |
| 13 |
import java.util.HashMap; |
| 14 |
import java.util.StringTokenizer; |
| 15 |
import org.h2.message.DbException; |
| 16 |
import org.h2.store.fs.FileUtils; |
| 17 |
import org.h2.util.IOUtils; |
| 18 |
import org.h2.util.StringUtils; |
| 19 |
import org.h2.util.Tool; |
| 20 |
|
| 21 |
/** |
| 22 |
* Converts a .trace.db file to a SQL script and Java source code. |
| 23 |
* |
| 24 |
* SQL statement statistics are listed as well. |
| 25 |
*/ |
| 26 |
public class ConvertTraceFile extends Tool { |
| 27 |
|
| 28 |
private final HashMap<String, Stat> stats = new HashMap<>(); |
| 29 |
private long timeTotal; |
| 30 |
|
| 31 |
/** |
| 32 |
* This class holds statistics about a SQL statement. |
| 33 |
*/ |
| 34 |
static class Stat implements Comparable<Stat> { |
| 35 |
String sql; |
| 36 |
int executeCount; |
| 37 |
long time; |
| 38 |
long resultCount; |
| 39 |
|
| 40 |
@Override |
| 41 |
public int compareTo(Stat other) { |
| 42 |
if (other == this) { |
| 43 |
return 0; |
| 44 |
} |
| 45 |
int c = Long.compare(other.time, time); |
| 46 |
if (c == 0) { |
| 47 |
c = Integer.compare(other.executeCount, executeCount); |
| 48 |
if (c == 0) { |
| 49 |
c = sql.compareTo(other.sql); |
| 50 |
} |
| 51 |
} |
| 52 |
return c; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Options are case sensitive. |
| 58 |
* <table> |
| 59 |
* <caption>Supported options</caption> |
| 60 |
* <tr><td>[-help] or [-?]</td> |
| 61 |
* <td>Print the list of options</td></tr> |
| 62 |
* <tr><td>[-traceFile <file>]</td> |
| 63 |
* <td>The trace file name (default: test.trace.db)</td></tr> |
| 64 |
* <tr><td>[-script <file>]</td> |
| 65 |
* <td>The script file name (default: test.sql)</td></tr> |
| 66 |
* <tr><td>[-javaClass <file>]</td> |
| 67 |
* <td>The Java directory and class file name (default: Test)</td></tr> |
| 68 |
* </table> |
| 69 |
* |
| 70 |
* @param args the command line arguments |
| 71 |
* @throws SQLException on failure |
| 72 |
*/ |
| 73 |
public static void main(String... args) throws SQLException { |
| 74 |
new ConvertTraceFile().runTool(args); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void runTool(String... args) throws SQLException { |
| 79 |
String traceFile = "test.trace.db"; |
| 80 |
String javaClass = "Test"; |
| 81 |
String script = "test.sql"; |
| 82 |
for (int i = 0; args != null && i < args.length; i++) { |
| 83 |
String arg = args[i]; |
| 84 |
if (arg.equals("-traceFile")) { |
| 85 |
traceFile = args[++i]; |
| 86 |
} else if (arg.equals("-javaClass")) { |
| 87 |
javaClass = args[++i]; |
| 88 |
} else if (arg.equals("-script")) { |
| 89 |
script = args[++i]; |
| 90 |
} else if (arg.equals("-help") || arg.equals("-?")) { |
| 91 |
showUsage(); |
| 92 |
return; |
| 93 |
} else { |
| 94 |
showUsageAndThrowUnsupportedOption(arg); |
| 95 |
} |
| 96 |
} |
| 97 |
try { |
| 98 |
convertFile(traceFile, javaClass, script); |
| 99 |
} catch (IOException e) { |
| 100 |
throw DbException.convertIOException(e, traceFile); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Converts a trace file to a Java class file and a script file. |
| 106 |
*/ |
| 107 |
private void convertFile(String traceFileName, String javaClassName, |
| 108 |
String script) throws IOException { |
| 109 |
LineNumberReader reader = new LineNumberReader( |
| 110 |
IOUtils.getReader( |
| 111 |
FileUtils.newInputStream(traceFileName))); |
| 112 |
PrintWriter javaWriter = new PrintWriter( |
| 113 |
IOUtils.getBufferedWriter( |
| 114 |
FileUtils.newOutputStream(javaClassName + ".java", false))); |
| 115 |
PrintWriter scriptWriter = new PrintWriter( |
| 116 |
IOUtils.getBufferedWriter( |
| 117 |
FileUtils.newOutputStream(script, false))); |
| 118 |
javaWriter.println("import java.io.*;"); |
| 119 |
javaWriter.println("import java.sql.*;"); |
| 120 |
javaWriter.println("import java.math.*;"); |
| 121 |
javaWriter.println("import java.util.Calendar;"); |
| 122 |
String cn = javaClassName.replace('\\', '/'); |
| 123 |
int idx = cn.lastIndexOf('/'); |
| 124 |
if (idx > 0) { |
| 125 |
cn = cn.substring(idx + 1); |
| 126 |
} |
| 127 |
javaWriter.println("public class " + cn + " {"); |
| 128 |
javaWriter.println(" public static void main(String... args) " + |
| 129 |
"throws Exception {"); |
| 130 |
javaWriter.println(" Class.forName(\"org.h2.Driver\");"); |
| 131 |
while (true) { |
| 132 |
String line = reader.readLine(); |
| 133 |
if (line == null) { |
| 134 |
break; |
| 135 |
} |
| 136 |
if (line.startsWith("/**/")) { |
| 137 |
line = " " + line.substring(4); |
| 138 |
javaWriter.println(line); |
| 139 |
} else if (line.startsWith("/*SQL")) { |
| 140 |
int end = line.indexOf("*/"); |
| 141 |
String sql = line.substring(end + "*/".length()); |
| 142 |
sql = StringUtils.javaDecode(sql); |
| 143 |
line = line.substring("/*SQL".length(), end); |
| 144 |
if (line.length() > 0) { |
| 145 |
String statement = sql; |
| 146 |
int count = 0; |
| 147 |
long time = 0; |
| 148 |
line = line.trim(); |
| 149 |
if (line.length() > 0) { |
| 150 |
StringTokenizer tk = new StringTokenizer(line, " :"); |
| 151 |
while (tk.hasMoreElements()) { |
| 152 |
String token = tk.nextToken(); |
| 153 |
if ("l".equals(token)) { |
| 154 |
int len = Integer.parseInt(tk.nextToken()); |
| 155 |
statement = sql.substring(0, len) + ";"; |
| 156 |
} else if ("#".equals(token)) { |
| 157 |
count = Integer.parseInt(tk.nextToken()); |
| 158 |
} else if ("t".equals(token)) { |
| 159 |
time = Long.parseLong(tk.nextToken()); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
addToStats(statement, count, time); |
| 164 |
} |
| 165 |
scriptWriter.println(sql); |
| 166 |
} |
| 167 |
} |
| 168 |
javaWriter.println(" }"); |
| 169 |
javaWriter.println('}'); |
| 170 |
reader.close(); |
| 171 |
javaWriter.close(); |
| 172 |
if (stats.size() > 0) { |
| 173 |
scriptWriter.println("-----------------------------------------"); |
| 174 |
scriptWriter.println("-- SQL Statement Statistics"); |
| 175 |
scriptWriter.println("-- time: total time in milliseconds (accumulated)"); |
| 176 |
scriptWriter.println("-- count: how many times the statement ran"); |
| 177 |
scriptWriter.println("-- result: total update count or row count"); |
| 178 |
scriptWriter.println("-----------------------------------------"); |
| 179 |
scriptWriter.println("-- self accu time count result sql"); |
| 180 |
int accumTime = 0; |
| 181 |
ArrayList<Stat> list = new ArrayList<>(stats.values()); |
| 182 |
Collections.sort(list); |
| 183 |
if (timeTotal == 0) { |
| 184 |
timeTotal = 1; |
| 185 |
} |
| 186 |
for (Stat stat : list) { |
| 187 |
accumTime += stat.time; |
| 188 |
StringBuilder buff = new StringBuilder(100); |
| 189 |
buff.append("-- "). |
| 190 |
append(padNumberLeft(100 * stat.time / timeTotal, 3)). |
| 191 |
append("% "). |
| 192 |
append(padNumberLeft(100 * accumTime / timeTotal, 3)). |
| 193 |
append('%'). |
| 194 |
append(padNumberLeft(stat.time, 8)). |
| 195 |
append(padNumberLeft(stat.executeCount, 8)). |
| 196 |
append(padNumberLeft(stat.resultCount, 8)). |
| 197 |
append(' '). |
| 198 |
append(removeNewlines(stat.sql)); |
| 199 |
scriptWriter.println(buff.toString()); |
| 200 |
} |
| 201 |
} |
| 202 |
scriptWriter.close(); |
| 203 |
} |
| 204 |
|
| 205 |
private static String removeNewlines(String s) { |
| 206 |
return s == null ? s : s.replace('\r', ' ').replace('\n', ' '); |
| 207 |
} |
| 208 |
|
| 209 |
private static String padNumberLeft(long number, int digits) { |
| 210 |
return StringUtils.pad(Long.toString(number), digits, " ", false); |
| 211 |
} |
| 212 |
|
| 213 |
private void addToStats(String sql, int resultCount, long time) { |
| 214 |
Stat stat = stats.get(sql); |
| 215 |
if (stat == null) { |
| 216 |
stat = new Stat(); |
| 217 |
stat.sql = sql; |
| 218 |
stats.put(sql, stat); |
| 219 |
} |
| 220 |
stat.executeCount++; |
| 221 |
stat.resultCount += resultCount; |
| 222 |
stat.time += time; |
| 223 |
timeTotal += time; |
| 224 |
} |
| 225 |
|
| 226 |
} |