import java.text.DecimalFormat;
import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
/**
void logStatement(StatementType type, String statement);
}
+ private static final ExecutorService EXEC = Executors.newSingleThreadExecutor();
private static final Set<StatementListener> LISTENERS = Utils.newHashSet();
private static final StatementListener CONSOLE = new StatementListener() {
logStatement(StatementType.SELECT, statement);
}
- private static void logStatement(StatementType type, String statement) {
- for (StatementListener listener : LISTENERS) {
- try {
- listener.logStatement(type, statement);
- } catch (Throwable t) {
- }
+ private static void logStatement(final StatementType type, final String statement) {
+ for (final StatementListener listener : LISTENERS) {
+ EXEC.execute(new Runnable() {
+ public void run() {
+ listener.logStatement(type, statement);
+ }
+ });
}
}
*/\r
package com.iciql.test;\r
\r
+import java.io.FileWriter;\r
+import java.io.IOException;\r
import java.io.PrintStream;\r
import java.sql.SQLException;\r
import java.text.MessageFormat;\r
import com.beust.jcommander.Parameters;\r
import com.iciql.Constants;\r
import com.iciql.Db;\r
+import com.iciql.util.StatementLogger;\r
+import com.iciql.util.StatementLogger.StatementListener;\r
+import com.iciql.util.StatementLogger.StatementType;\r
import com.iciql.util.StringUtils;\r
\r
/**\r
RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UUIDTest.class })\r
public class IciqlSuite {\r
\r
- private static final TestDb[] TEST_DBS = { new TestDb("H2", "jdbc:h2:mem:db{0,number,000}"),\r
+ private static final TestDb[] TEST_DBS = {\r
+ new TestDb("H2", "jdbc:h2:mem:db{0,number,000}"),\r
new TestDb("HSQL", "jdbc:hsqldb:mem:db{0,number,000}"),\r
new TestDb("Derby", "jdbc:derby:memory:db{0,number,000};create=true") };\r
\r
}\r
\r
// Replace System.out with a file\r
- if (!StringUtils.isNullOrEmpty(params.outputFile)) {\r
- out = new PrintStream(params.outputFile);\r
+ if (!StringUtils.isNullOrEmpty(params.dbPerformanceFile)) {\r
+ out = new PrintStream(params.dbPerformanceFile);\r
System.setErr(out);\r
}\r
\r
+ // Statement logging\r
+ final FileWriter statementWriter;\r
+ if (StringUtils.isNullOrEmpty(params.sqlStatementsFile)) {\r
+ statementWriter = null;\r
+ } else {\r
+ statementWriter = new FileWriter(params.sqlStatementsFile); \r
+ }\r
+ StatementListener statementListener = new StatementListener() {\r
+ @Override\r
+ public void logStatement(StatementType type, String statement) {\r
+ if (statementWriter == null) {\r
+ return;\r
+ }\r
+ try {\r
+ statementWriter.append(statement);\r
+ statementWriter.append('\n');\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }; \r
+ StatementLogger.registerListener(statementListener);\r
+ \r
+ \r
SuiteClasses suiteClasses = IciqlSuite.class.getAnnotation(SuiteClasses.class);\r
long quickestDatabase = Long.MAX_VALUE;\r
String dividerMajor = buildDivider('*', 70);\r
showProperty("java.vm.name");\r
showProperty("os.name");\r
showProperty("os.version");\r
- showProperty("os.arch");\r
+ showProperty("os.arch"); \r
+ showProperty("available processors", "" + Runtime.getRuntime().availableProcessors());\r
+ showProperty("available memory", MessageFormat.format("{0,number,#.0} GB", ((double) Runtime.getRuntime().maxMemory())/(1024*1024)));\r
out.println();\r
\r
// Test a database\r
out.println(dividerMinor);\r
out.println("Testing " + testDb.name + " " + testDb.getVersion());\r
out.println(dividerMinor);\r
+ \r
+ // inject a database section delimiter in the statement log\r
+ if (statementWriter != null) {\r
+ statementWriter.append("\n\n");\r
+ statementWriter.append("# ").append(dividerMinor).append('\n');\r
+ statementWriter.append("# ").append("Testing " + testDb.name + " " + testDb.getVersion()).append('\n');\r
+ statementWriter.append("# ").append(dividerMinor).append('\n');\r
+ statementWriter.append("\n\n");\r
+ }\r
+ \r
System.setProperty("iciql.url", testDb.url);\r
Result result = JUnitCore.runClasses(suiteClasses.value());\r
testDb.runtime = result.getRunTime();\r
}\r
\r
// close PrintStream and restore System.err\r
+ StatementLogger.unregisterListener(statementListener);\r
out.close();\r
System.setErr(ERR);\r
+ if (statementWriter != null) {\r
+ statementWriter.close();\r
+ }\r
+ System.exit(0);\r
}\r
\r
private static void showProperty(String name) {\r
+ showProperty(name, System.getProperty(name));\r
+ }\r
+\r
+ private static void showProperty(String name, String value) {\r
+ out.print(' ');\r
out.print(StringUtils.pad(name, 25, " ", true));\r
- out.println(System.getProperty(name));\r
+ out.println(value);\r
}\r
\r
private static void usage(JCommander jc, ParameterException t) {\r
@Parameters(separators = " ")\r
private static class Params {\r
\r
- @Parameter(names = { "--outputFile" }, description = "Results text file", required = false)\r
- public String outputFile;\r
+ @Parameter(names = { "--dbFile" }, description = "Database performance results text file", required = false)\r
+ public String dbPerformanceFile;\r
+ \r
+ @Parameter(names = { "--sqlFile" }, description = "SQL statements log file", required = false)\r
+ public String sqlStatementsFile;\r
}\r
}
\ No newline at end of file