PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/EQEmuJSM/mysql-connector-java-5.1.13/src/testsuite/regression/MicroPerformanceRegressionTest.java

http://cubbers-eqemu-utils.googlecode.com/
Java | 520 lines | 320 code | 141 blank | 59 comment | 35 complexity | 5f3e19d0c5f6ba7859925718894fdb20 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  1. /*
  2. Copyright 2002-2004 MySQL AB, 2008 Sun Microsystems
  3. All rights reserved. Use is subject to license terms.
  4. The MySQL Connector/J is licensed under the terms of the GPL,
  5. like most MySQL Connectors. There are special exceptions to the
  6. terms and conditions of the GPL as it is applied to this software,
  7. see the FLOSS License Exception available on mysql.com.
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; version 2 of the
  11. License.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. 02110-1301 USA
  20. */
  21. package testsuite.regression;
  22. import java.sql.Date;
  23. import java.sql.PreparedStatement;
  24. import java.sql.Time;
  25. import java.sql.Timestamp;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import testsuite.BaseTestCase;
  29. /**
  30. * Microperformance benchmarks to track increase/decrease in performance of core
  31. * methods in the driver over time.
  32. *
  33. * @author Mark Matthews
  34. *
  35. * @version $Id: MicroPerformanceRegressionTest.java,v 1.1.2.1 2005/05/13
  36. * 18:58:38 mmatthews Exp $
  37. */
  38. public class MicroPerformanceRegressionTest extends BaseTestCase {
  39. private double scaleFactor = 1.0;
  40. private final static int ORIGINAL_LOOP_TIME_MS = 2300;
  41. private final static double LEEWAY = 3.0;
  42. private final static Map BASELINE_TIMES = new HashMap();
  43. static {
  44. BASELINE_TIMES.put("ResultSet.getInt()", new Double(0.00661));
  45. BASELINE_TIMES.put("ResultSet.getDouble()", new Double(0.00671));
  46. BASELINE_TIMES.put("ResultSet.getTime()", new Double(0.02033));
  47. BASELINE_TIMES.put("ResultSet.getTimestamp()", new Double(0.02363));
  48. BASELINE_TIMES.put("ResultSet.getDate()", new Double(0.02223));
  49. BASELINE_TIMES.put("ResultSet.getString()", new Double(0.00982));
  50. BASELINE_TIMES.put("ResultSet.getObject() on a string", new Double(
  51. 0.00861));
  52. BASELINE_TIMES
  53. .put("Connection.prepareStatement()", new Double(0.18547));
  54. BASELINE_TIMES.put("single selects", new Double(46));
  55. BASELINE_TIMES.put("5 standalone queries", new Double(146));
  56. BASELINE_TIMES.put("total time all queries", new Double(190));
  57. if (com.mysql.jdbc.Util.isJdbc4()) {
  58. BASELINE_TIMES.put("PreparedStatement.setInt()", new Double(0.0014));
  59. BASELINE_TIMES.put("PreparedStatement.setTime()", new Double(0.0107));
  60. BASELINE_TIMES.put("PreparedStatement.setTimestamp()", new Double(
  61. 0.0182));
  62. BASELINE_TIMES.put("PreparedStatement.setDate()", new Double(0.0819));
  63. BASELINE_TIMES
  64. .put("PreparedStatement.setString()", new Double(0.0081));
  65. BASELINE_TIMES.put("PreparedStatement.setObject() on a string",
  66. new Double(0.00793));
  67. BASELINE_TIMES
  68. .put("PreparedStatement.setDouble()", new Double(0.0246));
  69. } else {
  70. BASELINE_TIMES.put("PreparedStatement.setInt()", new Double(0.0011));
  71. BASELINE_TIMES.put("PreparedStatement.setTime()", new Double(0.0642));
  72. BASELINE_TIMES.put("PreparedStatement.setTimestamp()", new Double(
  73. 0.03184));
  74. BASELINE_TIMES.put("PreparedStatement.setDate()", new Double(0.12248));
  75. BASELINE_TIMES
  76. .put("PreparedStatement.setString()", new Double(0.01512));
  77. BASELINE_TIMES.put("PreparedStatement.setObject() on a string",
  78. new Double(0.01923));
  79. BASELINE_TIMES
  80. .put("PreparedStatement.setDouble()", new Double(0.00671));
  81. }
  82. }
  83. public MicroPerformanceRegressionTest(String name) {
  84. super(name);
  85. }
  86. /**
  87. * Runs all test cases in this test suite
  88. *
  89. * @param args
  90. */
  91. public static void main(String[] args) {
  92. junit.textui.TestRunner.run(MicroPerformanceRegressionTest.class);
  93. }
  94. /**
  95. * Tests result set accessors performance.
  96. *
  97. * @throws Exception
  98. * if the performance of these methods does not meet
  99. * expectations.
  100. */
  101. public void testResultSetAccessors() throws Exception {
  102. try {
  103. this.stmt.executeUpdate("DROP TABLE IF EXISTS marktest");
  104. this.stmt
  105. .executeUpdate("CREATE TABLE marktest(intField INT, floatField DOUBLE, timeField TIME, datetimeField DATETIME, stringField VARCHAR(64))");
  106. this.stmt
  107. .executeUpdate("INSERT INTO marktest VALUES (123456789, 12345.6789, NOW(), NOW(), 'abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@')");
  108. this.rs = this.stmt
  109. .executeQuery("SELECT intField, floatField, timeField, datetimeField, stringField FROM marktest");
  110. this.rs.next();
  111. int numLoops = 100000;
  112. long start = currentTimeMillis();
  113. for (int i = 0; i < numLoops; i++) {
  114. this.rs.getInt(1);
  115. }
  116. double getIntAvgMs = (double) (currentTimeMillis() - start)
  117. / numLoops;
  118. checkTime("ResultSet.getInt()", getIntAvgMs);
  119. start = currentTimeMillis();
  120. for (int i = 0; i < numLoops; i++) {
  121. this.rs.getDouble(2);
  122. }
  123. double getDoubleAvgMs = (double) (currentTimeMillis() - start)
  124. / numLoops;
  125. checkTime("ResultSet.getDouble()", getDoubleAvgMs);
  126. start = currentTimeMillis();
  127. for (int i = 0; i < numLoops; i++) {
  128. this.rs.getTime(3);
  129. }
  130. double getTimeAvgMs = (double) (currentTimeMillis() - start)
  131. / numLoops;
  132. checkTime("ResultSet.getTime()", getTimeAvgMs);
  133. start = currentTimeMillis();
  134. for (int i = 0; i < numLoops; i++) {
  135. this.rs.getTimestamp(4);
  136. }
  137. double getTimestampAvgMs = (double) (currentTimeMillis() - start)
  138. / numLoops;
  139. checkTime("ResultSet.getTimestamp()", getTimestampAvgMs);
  140. start = currentTimeMillis();
  141. for (int i = 0; i < numLoops; i++) {
  142. this.rs.getDate(4);
  143. }
  144. double getDateAvgMs = (double) (currentTimeMillis() - start)
  145. / numLoops;
  146. checkTime("ResultSet.getDate()", getDateAvgMs);
  147. start = currentTimeMillis();
  148. for (int i = 0; i < numLoops; i++) {
  149. this.rs.getString(5);
  150. }
  151. double getStringAvgMs = (double) (currentTimeMillis() - start)
  152. / numLoops;
  153. checkTime("ResultSet.getString()", getStringAvgMs);
  154. start = currentTimeMillis();
  155. for (int i = 0; i < numLoops; i++) {
  156. this.rs.getObject(5);
  157. }
  158. double getStringObjAvgMs = (double) (currentTimeMillis() - start)
  159. / numLoops;
  160. checkTime("ResultSet.getObject() on a string", getStringObjAvgMs);
  161. } finally {
  162. this.stmt.executeUpdate("DROP TABLE IF EXISTS marktest");
  163. }
  164. }
  165. public void testPreparedStatementTimes() throws Exception {
  166. try {
  167. this.stmt.executeUpdate("DROP TABLE IF EXISTS marktest");
  168. this.stmt
  169. .executeUpdate("CREATE TABLE marktest(intField INT, floatField DOUBLE, timeField TIME, datetimeField DATETIME, stringField VARCHAR(64))");
  170. this.stmt
  171. .executeUpdate("INSERT INTO marktest VALUES (123456789, 12345.6789, NOW(), NOW(), 'abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@')");
  172. long start = currentTimeMillis();
  173. long blockStart = currentTimeMillis();
  174. long lastBlock = 0;
  175. int numLoops = 100000;
  176. int numPrepares = 100000;
  177. if (versionMeetsMinimum(4, 1)) {
  178. numPrepares = 10000; // we don't need to do so many for
  179. // server-side prep statements...
  180. }
  181. for (int i = 0; i < numPrepares; i++) {
  182. if (i % 1000 == 0) {
  183. long blockEnd = currentTimeMillis();
  184. long totalTime = blockEnd - blockStart;
  185. blockStart = blockEnd;
  186. StringBuffer messageBuf = new StringBuffer();
  187. messageBuf.append(i
  188. + " prepares, the last 1000 prepares took "
  189. + totalTime + " ms");
  190. if (lastBlock == 0) {
  191. lastBlock = totalTime;
  192. messageBuf.append(".");
  193. } else {
  194. double diff = (double) totalTime / (double) lastBlock;
  195. messageBuf.append(", difference is " + diff + " x");
  196. lastBlock = totalTime;
  197. }
  198. System.out.println(messageBuf.toString());
  199. }
  200. PreparedStatement pStmt = this.conn
  201. .prepareStatement("INSERT INTO test.marktest VALUES (?, ?, ?, ?, ?)");
  202. pStmt.close();
  203. }
  204. double getPrepareStmtAvgMs = (double) (currentTimeMillis() - start)
  205. / numPrepares;
  206. // checkTime("Connection.prepareStatement()", getPrepareStmtAvgMs);
  207. PreparedStatement pStmt = this.conn
  208. .prepareStatement("INSERT INTO marktest VALUES (?, ?, ?, ?, ?)");
  209. System.out.println(pStmt.toString());
  210. start = currentTimeMillis();
  211. for (int i = 0; i < numLoops; i++) {
  212. pStmt.setInt(1, 1);
  213. }
  214. System.out.println(pStmt.toString());
  215. double setIntAvgMs = (double) (currentTimeMillis() - start)
  216. / numLoops;
  217. checkTime("PreparedStatement.setInt()", setIntAvgMs);
  218. start = currentTimeMillis();
  219. for (int i = 0; i < numLoops; i++) {
  220. pStmt.setDouble(2, 1234567890.1234);
  221. }
  222. double setDoubleAvgMs = (double) (currentTimeMillis() - start)
  223. / numLoops;
  224. checkTime("PreparedStatement.setDouble()", setDoubleAvgMs);
  225. start = currentTimeMillis();
  226. Time tm = new Time(start);
  227. for (int i = 0; i < numLoops; i++) {
  228. pStmt.setTime(3, tm);
  229. }
  230. double setTimeAvgMs = (double) (currentTimeMillis() - start)
  231. / numLoops;
  232. checkTime("PreparedStatement.setTime()", setTimeAvgMs);
  233. start = currentTimeMillis();
  234. Timestamp ts = new Timestamp(start);
  235. for (int i = 0; i < numLoops; i++) {
  236. pStmt.setTimestamp(4, ts);
  237. }
  238. double setTimestampAvgMs = (double) (currentTimeMillis() - start)
  239. / numLoops;
  240. checkTime("PreparedStatement.setTimestamp()", setTimestampAvgMs);
  241. start = currentTimeMillis();
  242. Date dt = new Date(start);
  243. for (int i = 0; i < numLoops; i++) {
  244. pStmt.setDate(4, dt);
  245. }
  246. double setDateAvgMs = (double) (currentTimeMillis() - start)
  247. / numLoops;
  248. checkTime("PreparedStatement.setDate()", setDateAvgMs);
  249. start = currentTimeMillis();
  250. for (int i = 0; i < numLoops; i++) {
  251. pStmt
  252. .setString(5,
  253. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@");
  254. }
  255. double setStringAvgMs = (double) (currentTimeMillis() - start)
  256. / numLoops;
  257. checkTime("PreparedStatement.setString()", setStringAvgMs);
  258. start = currentTimeMillis();
  259. for (int i = 0; i < numLoops; i++) {
  260. pStmt
  261. .setObject(5,
  262. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@");
  263. }
  264. double setStringObjAvgMs = (double) (currentTimeMillis() - start)
  265. / numLoops;
  266. checkTime("PreparedStatement.setObject() on a string",
  267. setStringObjAvgMs);
  268. start = currentTimeMillis();
  269. } finally {
  270. this.stmt.executeUpdate("DROP TABLE IF EXISTS marktest");
  271. }
  272. }
  273. /*
  274. * (non-Javadoc)
  275. *
  276. * @see junit.framework.TestCase#setUp()
  277. */
  278. public void setUp() throws Exception {
  279. super.setUp();
  280. System.out.println("Calculating performance scaling factor...");
  281. // Run this simple test to get some sort of performance scaling factor,
  282. // compared to
  283. // the development environment. This should help reduce false-positives
  284. // on this test.
  285. int numLoops = 10000;
  286. long start = currentTimeMillis();
  287. for (int j = 0; j < 2000; j++) {
  288. StringBuffer buf = new StringBuffer(numLoops);
  289. for (int i = 0; i < numLoops; i++) {
  290. buf.append('a');
  291. }
  292. }
  293. long elapsedTime = currentTimeMillis() - start;
  294. System.out.println("Elapsed time for factor: " + elapsedTime);
  295. this.scaleFactor = (double) elapsedTime
  296. / (double) ORIGINAL_LOOP_TIME_MS;
  297. System.out
  298. .println("Performance scaling factor is: " + this.scaleFactor);
  299. }
  300. private void checkTime(String testType, double avgExecTimeMs)
  301. throws Exception {
  302. double adjustForVendor = 1.0D;
  303. if (isRunningOnJRockit()) {
  304. adjustForVendor = 4.0D;
  305. }
  306. Double baselineExecTimeMs = (Double) BASELINE_TIMES.get(testType);
  307. if (baselineExecTimeMs == null) {
  308. throw new Exception("No baseline time recorded for test '"
  309. + testType + "'");
  310. }
  311. double acceptableTime = LEEWAY * baselineExecTimeMs.doubleValue()
  312. * this.scaleFactor * adjustForVendor;
  313. assertTrue("Average execution time of " + avgExecTimeMs
  314. + " ms. exceeded baseline * leeway of " + acceptableTime
  315. + " ms.", (avgExecTimeMs <= acceptableTime));
  316. }
  317. public void testBug6359() throws Exception {
  318. if (runLongTests()) {
  319. int numRows = 550000;
  320. int numSelects = 100000;
  321. try {
  322. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug6359");
  323. this.stmt
  324. .executeUpdate("CREATE TABLE testBug6359 (pk_field INT PRIMARY KEY NOT NULL AUTO_INCREMENT, field1 INT, field2 INT, field3 INT, field4 INT, field5 INT, field6 INT, field7 INT, field8 INT, field9 INT, INDEX (field1))");
  325. PreparedStatement pStmt = this.conn
  326. .prepareStatement("INSERT INTO testBug6359 (field1, field2, field3, field4, field5, field6, field7, field8, field9) VALUES (?, 1, 2, 3, 4, 5, 6, 7, 8)");
  327. logDebug("Loading " + numRows + " rows...");
  328. for (int i = 0; i < numRows; i++) {
  329. pStmt.setInt(1, i);
  330. pStmt.executeUpdate();
  331. if ((i % 10000) == 0) {
  332. logDebug(i + " rows loaded so far");
  333. }
  334. }
  335. logDebug("Finished loading rows");
  336. long begin = currentTimeMillis();
  337. long beginSingleQuery = currentTimeMillis();
  338. for (int i = 0; i < numSelects; i++) {
  339. this.rs = this.stmt
  340. .executeQuery("SELECT pk_field FROM testBug6359 WHERE field1 BETWEEN 1 AND 5");
  341. }
  342. long endSingleQuery = currentTimeMillis();
  343. double secondsSingleQuery = ((double) endSingleQuery - (double) beginSingleQuery) / 1000;
  344. logDebug("time to execute " + numSelects + " single queries: "
  345. + secondsSingleQuery + " seconds");
  346. checkTime("single selects", secondsSingleQuery);
  347. PreparedStatement pStmt2 = this.conn
  348. .prepareStatement("SELECT field2, field3, field4, field5 FROM testBug6359 WHERE pk_field=?");
  349. long beginFiveQueries = currentTimeMillis();
  350. for (int i = 0; i < numSelects; i++) {
  351. for (int j = 0; j < 5; j++) {
  352. pStmt2.setInt(1, j);
  353. pStmt2.executeQuery();
  354. }
  355. }
  356. long endFiveQueries = currentTimeMillis();
  357. double secondsFiveQueries = ((double) endFiveQueries - (double) beginFiveQueries) / 1000;
  358. logDebug("time to execute " + numSelects
  359. + " 5 standalone queries: " + secondsFiveQueries
  360. + " seconds");
  361. checkTime("5 standalone queries", secondsFiveQueries);
  362. long end = currentTimeMillis();
  363. double seconds = ((double) end - (double) begin) / 1000;
  364. logDebug("time to execute " + numSelects + " selects: "
  365. + seconds + " seconds");
  366. checkTime("total time all queries", seconds);
  367. } finally {
  368. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug6359");
  369. }
  370. }
  371. }
  372. }