You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IciqlSuite.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * Copyright 2011 James Moger.
  3. * Copyright 2012 Frédéric Gaillard.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql.test;
  18. import java.io.File;
  19. import java.io.FileWriter;
  20. import java.io.IOException;
  21. import java.io.PrintStream;
  22. import java.sql.SQLException;
  23. import java.text.DecimalFormat;
  24. import java.text.MessageFormat;
  25. import java.util.Arrays;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import java.util.Map;
  29. import org.apache.commons.dbcp.ConnectionFactory;
  30. import org.apache.commons.dbcp.DriverManagerConnectionFactory;
  31. import org.apache.commons.dbcp.PoolableConnectionFactory;
  32. import org.apache.commons.dbcp.PoolingDataSource;
  33. import org.apache.commons.pool.impl.GenericObjectPool;
  34. import org.hsqldb.persist.HsqlProperties;
  35. import org.junit.Assert;
  36. import org.junit.runner.JUnitCore;
  37. import org.junit.runner.Result;
  38. import org.junit.runner.RunWith;
  39. import org.junit.runner.notification.Failure;
  40. import org.junit.runners.Suite;
  41. import org.junit.runners.Suite.SuiteClasses;
  42. import com.beust.jcommander.JCommander;
  43. import com.beust.jcommander.Parameter;
  44. import com.beust.jcommander.ParameterException;
  45. import com.beust.jcommander.Parameters;
  46. import com.iciql.Constants;
  47. import com.iciql.Db;
  48. import com.iciql.Iciql.Mode;
  49. import com.iciql.test.DataTypeAdapterTest.SerializedObjectTypeAdapterTest;
  50. import com.iciql.test.models.BooleanModel;
  51. import com.iciql.test.models.CategoryAnnotationOnly;
  52. import com.iciql.test.models.ComplexObject;
  53. import com.iciql.test.models.Customer;
  54. import com.iciql.test.models.DefaultValuesModel;
  55. import com.iciql.test.models.EnumModels.EnumIdModel;
  56. import com.iciql.test.models.EnumModels.EnumOrdinalModel;
  57. import com.iciql.test.models.EnumModels.EnumStringModel;
  58. import com.iciql.test.models.MultipleBoolsModel;
  59. import com.iciql.test.models.Order;
  60. import com.iciql.test.models.PrimitivesModel;
  61. import com.iciql.test.models.Product;
  62. import com.iciql.test.models.ProductAnnotationOnly;
  63. import com.iciql.test.models.ProductAnnotationOnlyWithForeignKey;
  64. import com.iciql.test.models.ProductInheritedAnnotation;
  65. import com.iciql.test.models.ProductMixedAnnotation;
  66. import com.iciql.test.models.ProductView;
  67. import com.iciql.test.models.ProductViewFromQuery;
  68. import com.iciql.test.models.ProductViewInherited;
  69. import com.iciql.test.models.ProductViewInheritedComplex;
  70. import com.iciql.test.models.SupportedTypes;
  71. import com.iciql.util.IciqlLogger;
  72. import com.iciql.util.IciqlLogger.IciqlListener;
  73. import com.iciql.util.IciqlLogger.StatementType;
  74. import com.iciql.util.StringUtils;
  75. import com.iciql.util.Utils;
  76. /**
  77. * JUnit 4 iciql test suite.
  78. *
  79. * By default this test suite will run against the H2 database. You can change
  80. * this by switching the DEFAULT_TEST_DB value.
  81. * <p>
  82. * Alternatively, you can run this class an application which will run all tests
  83. * for all tested database configurations.
  84. * <p>
  85. * NOTE: If you want to test against MySQL or PostgreSQL you must create an
  86. * "iciql" database and allow user "sa" password "sa" complete control of that
  87. * database.
  88. *
  89. */
  90. @RunWith(Suite.class)
  91. @SuiteClasses({ AliasMapTest.class, AnnotationsTest.class, BooleanModelTest.class, ClobTest.class,
  92. ConcurrencyTest.class, EnumsTest.class, ModelsTest.class, PrimitivesTest.class, OneOfTest.class,
  93. RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UpgradesTest.class, JoinTest.class,
  94. UUIDTest.class, ViewsTest.class, ForeignKeyTest.class, TransactionTest.class, NestedConditionsTest.class,
  95. DataTypeAdapterTest.class, ProductDaoTest.class })
  96. public class IciqlSuite {
  97. private final static File baseFolder = new File(System.getProperty("user.dir"), "/testdbs");
  98. private static final TestDb[] TEST_DBS = {
  99. new TestDb("H2", "memory", "jdbc:h2:mem:iciql"),
  100. new TestDb("H2", "file", "jdbc:h2:file:"
  101. + new File(baseFolder, "/h2/iciql").getAbsolutePath()),
  102. new TestDb("H2", "tcp", "jdbc:h2:tcp://localhost/"
  103. + new File(baseFolder, "/h2tcp/iciql").getAbsolutePath()),
  104. new TestDb("HSQL", "memory", "jdbc:hsqldb:mem:iciql"),
  105. new TestDb("HSQL", "file", "jdbc:hsqldb:file:testdbs/hsql/iciql"),
  106. new TestDb("HSQL", "tcp", "jdbc:hsqldb:hsql://localhost/iciql"),
  107. new TestDb("Derby", "memory", "jdbc:derby:memory:iciql;create=true"),
  108. new TestDb("Derby", "file", "jdbc:derby:directory:testdbs/derby/iciql;create=true"),
  109. new TestDb("MySQL", "tcp", "jdbc:mysql://localhost:3306/iciql", "sa", "sa"),
  110. new TestDb("PostgreSQL", "tcp", "jdbc:postgresql://localhost:5432/iciql", "sa", "sa"),
  111. new TestDb("SQLite", "memory", "jdbc:sqlite:file:iciql?mode=memory&cache=shared"),
  112. new TestDb("SQLite", "delete,full sync", "jdbc:sqlite:"
  113. + new File(baseFolder, "/sqlite/iciql.db").getAbsolutePath())
  114. };
  115. private static final TestDb DEFAULT_TEST_DB = TEST_DBS[0];
  116. private static final PrintStream ERR = System.err;
  117. private static PrintStream out = System.out;
  118. private static Map<String, PoolableConnectionFactory> connectionFactories = Utils
  119. .newSynchronizedHashMap();
  120. private static Map<String, PoolingDataSource> dataSources = Utils.newSynchronizedHashMap();
  121. public static void assertStartsWith(String value, String startsWith) {
  122. Assert.assertTrue(MessageFormat.format("Expected \"{0}\", got: \"{1}\"", startsWith, value),
  123. value.startsWith(startsWith));
  124. }
  125. public static void assertEqualsIgnoreCase(String expected, String actual) {
  126. Assert.assertTrue(MessageFormat.format("Expected \"{0}\", got: \"{1}\"", expected, actual),
  127. expected.equalsIgnoreCase(actual));
  128. }
  129. public static boolean equivalentTo(double expected, double actual) {
  130. if (Double.compare(expected, actual) == 0) {
  131. return true;
  132. }
  133. return Math.abs(expected - actual) <= 0.000001d;
  134. }
  135. public static Db openNewDb() {
  136. return openNewDb(Mode.PROD);
  137. }
  138. /**
  139. * Open a new Db object. All connections are cached and re-used to eliminate
  140. * embedded database startup costs.
  141. *
  142. * @param mode
  143. * @return a fresh Db object
  144. */
  145. public static Db openNewDb(Mode mode) {
  146. String testUrl = System.getProperty("iciql.url", DEFAULT_TEST_DB.url);
  147. String testUser = System.getProperty("iciql.user", DEFAULT_TEST_DB.username);
  148. String testPassword = System.getProperty("iciql.password", DEFAULT_TEST_DB.password);
  149. Db db = null;
  150. PoolingDataSource dataSource = dataSources.get(testUrl);
  151. if (dataSource == null) {
  152. ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(testUrl, testUser,
  153. testPassword);
  154. GenericObjectPool pool = new GenericObjectPool();
  155. pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
  156. PoolableConnectionFactory factory = new PoolableConnectionFactory(connectionFactory, pool, null,
  157. null, false, true);
  158. dataSource = new PoolingDataSource(pool);
  159. dataSources.put(testUrl, dataSource);
  160. connectionFactories.put(testUrl, factory);
  161. }
  162. db = Db.open(dataSource, mode);
  163. // drop views
  164. db.dropView(ProductView.class);
  165. db.dropView(ProductViewInherited.class);
  166. db.dropView(ProductViewFromQuery.class);
  167. db.dropView(ProductViewInheritedComplex.class);
  168. // drop tables
  169. db.dropTable(BooleanModel.class);
  170. db.dropTable(ComplexObject.class);
  171. db.dropTable(Customer.class);
  172. db.dropTable(DefaultValuesModel.class);
  173. db.dropTable(EnumIdModel.class);
  174. db.dropTable(EnumOrdinalModel.class);
  175. db.dropTable(EnumStringModel.class);
  176. db.dropTable(Order.class);
  177. db.dropTable(PrimitivesModel.class);
  178. db.dropTable(Product.class);
  179. db.dropTable(ProductAnnotationOnly.class);
  180. db.dropTable(ProductInheritedAnnotation.class);
  181. db.dropTable(ProductMixedAnnotation.class);
  182. db.dropTable(SupportedTypes.class);
  183. db.dropTable(JoinTest.UserId.class);
  184. db.dropTable(JoinTest.UserNote.class);
  185. db.dropTable(EnumsTest.BadEnums.class);
  186. db.dropTable(MultipleBoolsModel.class);
  187. db.dropTable(ProductAnnotationOnlyWithForeignKey.class);
  188. db.dropTable(CategoryAnnotationOnly.class);
  189. db.dropTable(SerializedObjectTypeAdapterTest.class);
  190. return db;
  191. }
  192. /**
  193. * Open the current database.
  194. *
  195. * @return the current database
  196. */
  197. public static Db openCurrentDb() {
  198. String testUrl = System.getProperty("iciql.url", DEFAULT_TEST_DB.url);
  199. String testUser = System.getProperty("iciql.user", DEFAULT_TEST_DB.username);
  200. String testPassword = System.getProperty("iciql.password", DEFAULT_TEST_DB.password);
  201. return Db.open(testUrl, testUser, testPassword);
  202. }
  203. /**
  204. * Returns the name of the underlying database engine for the Db object.
  205. *
  206. * @param db
  207. * @return the database engine name
  208. */
  209. public static String getDatabaseEngineName(Db db) {
  210. String database = "";
  211. try {
  212. database = db.getConnection().getMetaData().getDatabaseProductName();
  213. } catch (SQLException s) {
  214. }
  215. return database;
  216. }
  217. /**
  218. * Returns true if the underlying database engine is Derby.
  219. *
  220. * @param db
  221. * @return true if underlying database engine is Derby
  222. */
  223. public static boolean isDerby(Db db) {
  224. return IciqlSuite.getDatabaseEngineName(db).equals("Apache Derby");
  225. }
  226. /**
  227. * Returns true if the underlying database engine is H2.
  228. *
  229. * @param db
  230. * @return true if underlying database engine is H2
  231. */
  232. public static boolean isH2(Db db) {
  233. return IciqlSuite.getDatabaseEngineName(db).equals("H2");
  234. }
  235. /**
  236. * Returns true if the underlying database engine is MySQL.
  237. *
  238. * @param db
  239. * @return true if underlying database engine is MySQL
  240. */
  241. public static boolean isMySQL(Db db) {
  242. return IciqlSuite.getDatabaseEngineName(db).equals("MySQL");
  243. }
  244. /**
  245. * Returns true if the underlying database engine is SQLite.
  246. *
  247. * @param db
  248. * @return true if underlying database engine is SQLite
  249. */
  250. public static boolean isSQLite(Db db) {
  251. return IciqlSuite.getDatabaseEngineName(db).equals("SQLite");
  252. }
  253. /**
  254. * Gets the default schema of the underlying database engine.
  255. *
  256. * @param db
  257. * @return the default schema
  258. */
  259. public static String getDefaultSchema(Db db) {
  260. if (isDerby(db)) {
  261. // Derby sets default schema name to username
  262. return "SA";
  263. } else if (isMySQL(db)) {
  264. // MySQL does not have schemas
  265. return null;
  266. }
  267. return "PUBLIC";
  268. }
  269. /**
  270. * Main entry point for the test suite. Executing this method will run the
  271. * test suite on all registered databases.
  272. *
  273. * @param args
  274. * @throws Exception
  275. */
  276. public static void main(String... args) throws Exception {
  277. Params params = new Params();
  278. JCommander jc = new JCommander(params);
  279. try {
  280. jc.parse(args);
  281. } catch (ParameterException t) {
  282. usage(jc, t);
  283. }
  284. // Replace System.out with a file
  285. if (!StringUtils.isNullOrEmpty(params.dbPerformanceFile)) {
  286. out = new PrintStream(params.dbPerformanceFile);
  287. System.setErr(out);
  288. }
  289. deleteRecursively(baseFolder);
  290. new File(baseFolder, "/sqlite").mkdirs();
  291. // Start the HSQL and H2 servers in-process
  292. org.hsqldb.Server hsql = startHSQL();
  293. org.h2.tools.Server h2 = startH2();
  294. // Statement logging
  295. final FileWriter statementWriter;
  296. if (StringUtils.isNullOrEmpty(params.sqlStatementsFile)) {
  297. statementWriter = null;
  298. } else {
  299. statementWriter = new FileWriter(params.sqlStatementsFile);
  300. }
  301. IciqlListener statementListener = new IciqlListener() {
  302. @Override
  303. public void logIciql(StatementType type, String statement) {
  304. if (statementWriter == null) {
  305. return;
  306. }
  307. try {
  308. statementWriter.append(statement);
  309. statementWriter.append('\n');
  310. } catch (IOException e) {
  311. e.printStackTrace();
  312. }
  313. }
  314. };
  315. IciqlLogger.registerListener(statementListener);
  316. SuiteClasses suiteClasses = IciqlSuite.class.getAnnotation(SuiteClasses.class);
  317. long quickestDatabase = Long.MAX_VALUE;
  318. String dividerMajor = buildDivider('*', 79);
  319. String dividerMinor = buildDivider('-', 79);
  320. // Header
  321. out.println(dividerMajor);
  322. out.println(MessageFormat.format("{0} {1} ({2}) testing {3} database configurations", Constants.NAME,
  323. Constants.getVersion(), Constants.getBuildDate(), TEST_DBS.length));
  324. out.println(dividerMajor);
  325. out.println();
  326. showProperty("java.vendor");
  327. showProperty("java.runtime.version");
  328. showProperty("java.vm.name");
  329. showProperty("os.name");
  330. showProperty("os.version");
  331. showProperty("os.arch");
  332. showProperty("available processors", "" + Runtime.getRuntime().availableProcessors());
  333. showProperty(
  334. "available memory",
  335. MessageFormat.format("{0,number,0.0} GB", ((double) Runtime.getRuntime().maxMemory())
  336. / (1024 * 1024)));
  337. out.println();
  338. // Test a database
  339. long lastCount = 0;
  340. for (TestDb testDb : TEST_DBS) {
  341. out.println(dividerMinor);
  342. out.println("Testing " + testDb.describeDatabase());
  343. out.println(" " + testDb.url);
  344. out.println(dividerMinor);
  345. // inject a database section delimiter in the statement log
  346. if (statementWriter != null) {
  347. statementWriter.append("\n\n");
  348. statementWriter.append("# ").append(dividerMinor).append('\n');
  349. statementWriter.append("# ").append("Testing " + testDb.describeDatabase()).append('\n');
  350. statementWriter.append("# ").append(dividerMinor).append('\n');
  351. statementWriter.append("\n\n");
  352. }
  353. if (testDb.getVersion().equals("OFFLINE")) {
  354. // Database not available
  355. out.println("Skipping. Could not find " + testDb.url);
  356. out.println();
  357. } else {
  358. // Setup system properties
  359. System.setProperty("iciql.url", testDb.url);
  360. System.setProperty("iciql.user", testDb.username);
  361. System.setProperty("iciql.password", testDb.password);
  362. // Test database
  363. Result result = JUnitCore.runClasses(suiteClasses.value());
  364. // Report results
  365. testDb.runtime = result.getRunTime();
  366. if (testDb.runtime < quickestDatabase) {
  367. quickestDatabase = testDb.runtime;
  368. }
  369. testDb.statements = IciqlLogger.getTotalCount() - lastCount;
  370. // reset total count for next database
  371. lastCount = IciqlLogger.getTotalCount();
  372. out.println(MessageFormat.format(
  373. "{0} tests ({1} failures, {2} ignores) {3} statements in {4,number,0.000} secs",
  374. result.getRunCount(), result.getFailureCount(), result.getIgnoreCount(),
  375. testDb.statements, result.getRunTime() / 1000f));
  376. if (result.getFailureCount() == 0) {
  377. out.println();
  378. out.println(" 100% successful test suite run.");
  379. out.println();
  380. } else {
  381. for (Failure failure : result.getFailures()) {
  382. out.println(MessageFormat.format("\n + {0}\n {1}", failure.getTestHeader(),
  383. failure.getMessage()));
  384. }
  385. out.println();
  386. }
  387. }
  388. }
  389. // Display runtime results sorted by performance leader
  390. out.println();
  391. out.println(dividerMajor);
  392. out.println(MessageFormat.format("{0} {1} ({2}) test suite performance results", Constants.NAME,
  393. Constants.getVersion(), Constants.getBuildDate()));
  394. StringBuilder compressedSystem = new StringBuilder();
  395. compressedSystem.append(" on ");
  396. compressedSystem.append(System.getProperty("java.vendor"));
  397. compressedSystem.append(' ');
  398. compressedSystem.append(System.getProperty("java.runtime.version"));
  399. compressedSystem.append(", ");
  400. compressedSystem.append(System.getProperty("os.name"));
  401. compressedSystem.append(' ');
  402. compressedSystem.append(System.getProperty("os.version"));
  403. compressedSystem.append(", ");
  404. compressedSystem.append(System.getProperty("os.arch"));
  405. out.println(compressedSystem.toString());
  406. out.println(dividerMajor);
  407. List<TestDb> dbs = Arrays.asList(TEST_DBS);
  408. Collections.sort(dbs);
  409. out.println(MessageFormat.format("{0} {1} {2} {3} {4}",
  410. StringUtils.pad("Name", 11, " ", true),
  411. StringUtils.pad("Config", 16, " ", true),
  412. StringUtils.pad("Version", 25, " ", true),
  413. StringUtils.pad("Stats/sec", 10, " ", true),
  414. "Runtime"));
  415. out.println(dividerMinor);
  416. for (TestDb testDb : dbs) {
  417. DecimalFormat df = new DecimalFormat("0.0");
  418. out.println(MessageFormat.format("{0} {1} {2} {3} {4}s ({5,number,0.0}x)",
  419. StringUtils.pad(testDb.name, 11, " ", true),
  420. StringUtils.pad(testDb.config, 16, " ", true),
  421. StringUtils.pad(testDb.getVersion(), 23, " ", true),
  422. StringUtils.pad("" + testDb.getStatementRate(), 7, " ", false),
  423. StringUtils.pad(df.format(testDb.getRuntime()), 8, " ", false),
  424. ((double) testDb.runtime) / quickestDatabase));
  425. }
  426. out.println(dividerMinor);
  427. // cleanup
  428. for (PoolableConnectionFactory factory : connectionFactories.values()) {
  429. factory.getPool().close();
  430. }
  431. IciqlLogger.unregisterListener(statementListener);
  432. out.close();
  433. System.setErr(ERR);
  434. if (statementWriter != null) {
  435. statementWriter.close();
  436. }
  437. hsql.stop();
  438. h2.stop();
  439. System.exit(0);
  440. }
  441. private static void showProperty(String name) {
  442. showProperty(name, System.getProperty(name));
  443. }
  444. private static void showProperty(String name, String value) {
  445. out.print(' ');
  446. out.print(StringUtils.pad(name, 25, " ", true));
  447. out.println(value);
  448. }
  449. private static void usage(JCommander jc, ParameterException t) {
  450. System.out.println(Constants.NAME + " test suite v" + Constants.getVersion());
  451. System.out.println();
  452. if (t != null) {
  453. System.out.println(t.getMessage());
  454. System.out.println();
  455. }
  456. if (jc != null) {
  457. jc.usage();
  458. }
  459. System.exit(0);
  460. }
  461. private static String buildDivider(char c, int length) {
  462. StringBuilder sb = new StringBuilder();
  463. for (int i = 0; i < length; i++) {
  464. sb.append(c);
  465. }
  466. return sb.toString();
  467. }
  468. private static void deleteRecursively(File f) {
  469. if (f.isDirectory()) {
  470. for (File file : f.listFiles()) {
  471. if (file.isDirectory()) {
  472. deleteRecursively(file);
  473. }
  474. file.delete();
  475. }
  476. }
  477. f.delete();
  478. }
  479. /**
  480. * Start an HSQL tcp server.
  481. *
  482. * @return an HSQL server instance
  483. * @throws Exception
  484. */
  485. private static org.hsqldb.Server startHSQL() throws Exception {
  486. HsqlProperties p = new HsqlProperties();
  487. String db = new File(System.getProperty("user.dir")).getAbsolutePath() + "/testdbs/hsqltcp/iciql";
  488. p.setProperty("server.database.0", "file:" + db);
  489. p.setProperty("server.dbname.0", "iciql");
  490. // set up the rest of properties
  491. // alternative to the above is
  492. org.hsqldb.Server server = new org.hsqldb.Server();
  493. server.setProperties(p);
  494. server.setLogWriter(null);
  495. server.setErrWriter(null);
  496. server.start();
  497. return server;
  498. }
  499. /**
  500. * Start the H2 tcp server.
  501. *
  502. * @return an H2 server instance
  503. * @throws Exception
  504. */
  505. private static org.h2.tools.Server startH2() throws Exception {
  506. org.h2.tools.Server server = org.h2.tools.Server.createTcpServer();
  507. server.start();
  508. return server;
  509. }
  510. /**
  511. * Represents a test database url.
  512. */
  513. private static class TestDb implements Comparable<TestDb> {
  514. final String name;
  515. final String config;
  516. final String url;
  517. final String username;
  518. final String password;
  519. String version;
  520. long runtime;
  521. long statements;
  522. TestDb(String name, String config, String url) {
  523. this(name, config, url, "sa", "");
  524. }
  525. TestDb(String name, String config, String url, String username, String password) {
  526. this.name = name;
  527. this.config = config;
  528. this.url = url;
  529. this.username = username;
  530. this.password = password;
  531. }
  532. double getRuntime() {
  533. return runtime / 1000d;
  534. }
  535. int getStatementRate() {
  536. return Double.valueOf((statements) / (runtime / 1000d)).intValue();
  537. }
  538. String describeDatabase() {
  539. StringBuilder sb = new StringBuilder(name);
  540. sb.append(" ");
  541. sb.append(getVersion());
  542. return sb.toString();
  543. }
  544. String getVersion() {
  545. if (version == null) {
  546. try {
  547. Db db = Db.open(url, username, password);
  548. version = db.getConnection().getMetaData().getDatabaseProductVersion();
  549. db.close();
  550. return version;
  551. } catch (Throwable t) {
  552. version = "OFFLINE";
  553. }
  554. }
  555. return version;
  556. }
  557. @Override
  558. public int compareTo(TestDb o) {
  559. if (runtime == 0) {
  560. return 1;
  561. }
  562. if (o.runtime == 0) {
  563. return -1;
  564. }
  565. int r1 = getStatementRate();
  566. int r2 = o.getStatementRate();
  567. if (r1 == r2) {
  568. return 0;
  569. }
  570. if (r1 < r2) {
  571. return 1;
  572. }
  573. return -1;
  574. }
  575. }
  576. /**
  577. * Command-line parameters for TestSuite.
  578. */
  579. @Parameters(separators = " ")
  580. private static class Params {
  581. @Parameter(names = { "--dbFile" }, description = "Database performance results text file", required = false)
  582. public String dbPerformanceFile;
  583. @Parameter(names = { "--sqlFile" }, description = "SQL statements log file", required = false)
  584. public String sqlStatementsFile;
  585. }
  586. }