KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > regression > MicroPerformanceRegressionTest


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7
8  There are special exceptions to the terms and conditions of the GPL
9  as it is applied to this software. View the full text of the
10  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11  software distribution.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
23
24  */

25 package testsuite.regression;
26
27 import java.sql.Date JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.Time JavaDoc;
30 import java.sql.Timestamp JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import testsuite.BaseTestCase;
35
36 /**
37  * Microperformance benchmarks to track increase/decrease in performance of core
38  * methods in the driver over time.
39  *
40  * @author Mark Matthews
41  *
42  * @version $Id: MicroPerformanceRegressionTest.java,v 1.1.2.1 2005/05/13
43  * 18:58:38 mmatthews Exp $
44  */

45 public class MicroPerformanceRegressionTest extends BaseTestCase {
46
47     private double scaleFactor = 1.0;
48
49     private final static int ORIGINAL_LOOP_TIME_MS = 2300;
50
51     private final static double LEEWAY = 3.0;
52
53     private final static Map JavaDoc BASELINE_TIMES = new HashMap JavaDoc();
54
55     static {
56         BASELINE_TIMES.put("ResultSet.getInt()", new Double JavaDoc(0.00661));
57         BASELINE_TIMES.put("ResultSet.getDouble()", new Double JavaDoc(0.00671));
58         BASELINE_TIMES.put("ResultSet.getTime()", new Double JavaDoc(0.02033));
59         BASELINE_TIMES.put("ResultSet.getTimestamp()", new Double JavaDoc(0.02363));
60         BASELINE_TIMES.put("ResultSet.getDate()", new Double JavaDoc(0.02223));
61         BASELINE_TIMES.put("ResultSet.getString()", new Double JavaDoc(0.00982));
62         BASELINE_TIMES.put("ResultSet.getObject() on a string", new Double JavaDoc(
63                 0.00861));
64         BASELINE_TIMES
65                 .put("Connection.prepareStatement()", new Double JavaDoc(0.18547));
66         BASELINE_TIMES.put("PreparedStatement.setInt()", new Double JavaDoc(0.0011));
67         BASELINE_TIMES
68                 .put("PreparedStatement.setDouble()", new Double JavaDoc(0.00671));
69         BASELINE_TIMES.put("PreparedStatement.setTime()", new Double JavaDoc(0.0642));
70         BASELINE_TIMES.put("PreparedStatement.setTimestamp()", new Double JavaDoc(
71                 0.03184));
72         BASELINE_TIMES.put("PreparedStatement.setDate()", new Double JavaDoc(0.12248));
73         BASELINE_TIMES
74                 .put("PreparedStatement.setString()", new Double JavaDoc(0.01512));
75         BASELINE_TIMES.put("PreparedStatement.setObject() on a string",
76                 new Double JavaDoc(0.01923));
77         BASELINE_TIMES.put("single selects", new Double JavaDoc(46));
78         BASELINE_TIMES.put("5 standalone queries", new Double JavaDoc(146));
79         BASELINE_TIMES.put("total time all queries", new Double JavaDoc(190));
80     }
81
82     public MicroPerformanceRegressionTest(String JavaDoc name) {
83         super(name);
84     }
85
86     /**
87      * Runs all test cases in this test suite
88      *
89      * @param args
90      */

91     public static void main(String JavaDoc[] args) {
92         junit.textui.TestRunner.run(MicroPerformanceRegressionTest.class);
93     }
94
95     /**
96      * Tests result set accessors performance.
97      *
98      * @throws Exception
99      * if the performance of these methods does not meet
100      * expectations.
101      */

102     public void testResultSetAccessors() throws Exception JavaDoc {
103         try {
104             this.stmt.executeUpdate("DROP TABLE IF EXISTS marktest");
105             this.stmt
106                     .executeUpdate("CREATE TABLE marktest(intField INT, floatField DOUBLE, timeField TIME, datetimeField DATETIME, stringField VARCHAR(64))");
107             this.stmt
108                     .executeUpdate("INSERT INTO marktest VALUES (123456789, 12345.6789, NOW(), NOW(), 'abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@')");
109
110             this.rs = this.stmt
111                     .executeQuery("SELECT intField, floatField, timeField, datetimeField, stringField FROM marktest");
112
113             this.rs.next();
114
115             int numLoops = 100000;
116
117             long start = System.currentTimeMillis();
118
119             for (int i = 0; i < numLoops; i++) {
120                 this.rs.getInt(1);
121             }
122
123             double getIntAvgMs = (double) (System.currentTimeMillis() - start)
124                     / numLoops;
125
126             checkTime("ResultSet.getInt()", getIntAvgMs);
127
128             start = System.currentTimeMillis();
129
130             for (int i = 0; i < numLoops; i++) {
131                 this.rs.getDouble(2);
132             }
133
134             double getDoubleAvgMs = (double) (System.currentTimeMillis() - start)
135                     / numLoops;
136
137             checkTime("ResultSet.getDouble()", getDoubleAvgMs);
138
139             start = System.currentTimeMillis();
140
141             for (int i = 0; i < numLoops; i++) {
142                 this.rs.getTime(3);
143             }
144
145             double getTimeAvgMs = (double) (System.currentTimeMillis() - start)
146                     / numLoops;
147
148             checkTime("ResultSet.getTime()", getTimeAvgMs);
149
150             start = System.currentTimeMillis();
151
152             for (int i = 0; i < numLoops; i++) {
153                 this.rs.getTimestamp(4);
154             }
155
156             double getTimestampAvgMs = (double) (System.currentTimeMillis() - start)
157                     / numLoops;
158
159             checkTime("ResultSet.getTimestamp()", getTimestampAvgMs);
160
161             start = System.currentTimeMillis();
162
163             for (int i = 0; i < numLoops; i++) {
164                 this.rs.getDate(4);
165             }
166
167             double getDateAvgMs = (double) (System.currentTimeMillis() - start)
168                     / numLoops;
169
170             checkTime("ResultSet.getDate()", getDateAvgMs);
171
172             start = System.currentTimeMillis();
173
174             for (int i = 0; i < numLoops; i++) {
175                 this.rs.getString(5);
176             }
177
178             double getStringAvgMs = (double) (System.currentTimeMillis() - start)
179                     / numLoops;
180
181             checkTime("ResultSet.getString()", getStringAvgMs);
182
183             start = System.currentTimeMillis();
184
185             for (int i = 0; i < numLoops; i++) {
186                 this.rs.getObject(5);
187             }
188
189             double getStringObjAvgMs = (double) (System.currentTimeMillis() - start)
190                     / numLoops;
191
192             checkTime("ResultSet.getObject() on a string", getStringObjAvgMs);
193
194             start = System.currentTimeMillis();
195
196             long blockStart = System.currentTimeMillis();
197             long lastBlock = 0;
198
199             int numPrepares = 100000;
200
201             if (versionMeetsMinimum(4, 1)) {
202                 numPrepares = 100000; // we don't need to do so many for
203
// server-side prep statements...
204
}
205
206             for (int i = 0; i < numPrepares; i++) {
207                 if (i % 1000 == 0) {
208
209                     long blockEnd = System.currentTimeMillis();
210
211                     long totalTime = blockEnd - blockStart;
212
213                     blockStart = blockEnd;
214
215                     StringBuffer JavaDoc messageBuf = new StringBuffer JavaDoc();
216
217                     messageBuf.append(i
218                             + " prepares, the last 1000 prepares took "
219                             + totalTime + " ms");
220
221                     if (lastBlock == 0) {
222                         lastBlock = totalTime;
223                         messageBuf.append(".");
224                     } else {
225                         double diff = (double) totalTime / (double) lastBlock;
226
227                         messageBuf.append(", difference is " + diff + " x");
228
229                         lastBlock = totalTime;
230                     }
231
232                     System.out.println(messageBuf.toString());
233
234                 }
235
236                 PreparedStatement JavaDoc pStmt = this.conn
237                         .prepareStatement("INSERT INTO test.marktest VALUES (?, ?, ?, ?, ?)");
238                 pStmt.close();
239             }
240
241             double getPrepareStmtAvgMs = (double) (System.currentTimeMillis() - start)
242                     / numPrepares;
243
244             // checkTime("Connection.prepareStatement()", getPrepareStmtAvgMs);
245

246             PreparedStatement JavaDoc pStmt = this.conn
247                     .prepareStatement("INSERT INTO marktest VALUES (?, ?, ?, ?, ?)");
248
249             System.out.println(pStmt.toString());
250
251             start = System.currentTimeMillis();
252
253             for (int i = 0; i < numLoops; i++) {
254                 pStmt.setInt(1, 1);
255             }
256
257             System.out.println(pStmt.toString());
258
259             double setIntAvgMs = (double) (System.currentTimeMillis() - start)
260                     / numLoops;
261
262             checkTime("PreparedStatement.setInt()", setIntAvgMs);
263
264             start = System.currentTimeMillis();
265
266             for (int i = 0; i < numLoops; i++) {
267                 pStmt.setDouble(2, 1234567890.1234);
268             }
269
270             double setDoubleAvgMs = (double) (System.currentTimeMillis() - start)
271                     / numLoops;
272
273             checkTime("PreparedStatement.setDouble()", getDoubleAvgMs);
274
275             start = System.currentTimeMillis();
276
277             Time JavaDoc tm = new Time JavaDoc(start);
278
279             for (int i = 0; i < numLoops; i++) {
280                 pStmt.setTime(3, tm);
281             }
282
283             double setTimeAvgMs = (double) (System.currentTimeMillis() - start)
284                     / numLoops;
285
286             checkTime("PreparedStatement.setTime()", setTimeAvgMs);
287
288             start = System.currentTimeMillis();
289
290             Timestamp JavaDoc ts = new Timestamp JavaDoc(start);
291
292             for (int i = 0; i < numLoops; i++) {
293                 pStmt.setTimestamp(4, ts);
294             }
295
296             double setTimestampAvgMs = (double) (System.currentTimeMillis() - start)
297                     / numLoops;
298
299             checkTime("PreparedStatement.setTimestamp()", setTimestampAvgMs);
300
301             start = System.currentTimeMillis();
302
303             Date JavaDoc dt = new Date JavaDoc(start);
304
305             for (int i = 0; i < numLoops; i++) {
306                 pStmt.setDate(4, dt);
307             }
308
309             double setDateAvgMs = (double) (System.currentTimeMillis() - start)
310                     / numLoops;
311
312             checkTime("PreparedStatement.setDate()", setDateAvgMs);
313
314             start = System.currentTimeMillis();
315
316             for (int i = 0; i < numLoops; i++) {
317                 pStmt
318                         .setString(5,
319                                 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@");
320             }
321
322             double setStringAvgMs = (double) (System.currentTimeMillis() - start)
323                     / numLoops;
324
325             checkTime("PreparedStatement.setString()", setStringAvgMs);
326
327             start = System.currentTimeMillis();
328
329             for (int i = 0; i < numLoops; i++) {
330                 pStmt
331                         .setObject(5,
332                                 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@");
333             }
334
335             double setStringObjAvgMs = (double) (System.currentTimeMillis() - start)
336                     / numLoops;
337
338             checkTime("PreparedStatement.setObject() on a string",
339                     setStringObjAvgMs);
340
341             start = System.currentTimeMillis();
342
343         } finally {
344             this.stmt.executeUpdate("DROP TABLE IF EXISTS marktest");
345         }
346     }
347
348     /*
349      * (non-Javadoc)
350      *
351      * @see junit.framework.TestCase#setUp()
352      */

353     public void setUp() throws Exception JavaDoc {
354         super.setUp();
355
356         System.out.println("Calculating performance scaling factor...");
357         // Run this simple test to get some sort of performance scaling factor,
358
// compared to
359
// the development environment. This should help reduce false-positives
360
// on this test.
361
int numLoops = 10000;
362
363         long start = System.currentTimeMillis();
364
365         for (int j = 0; j < 2000; j++) {
366             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(numLoops);
367
368             for (int i = 0; i < numLoops; i++) {
369                 buf.append('a');
370             }
371         }
372
373         long elapsedTime = System.currentTimeMillis() - start;
374
375         System.out.println("Elapsed time for factor: " + elapsedTime);
376
377         this.scaleFactor = (double) elapsedTime
378                 / (double) ORIGINAL_LOOP_TIME_MS;
379
380         System.out
381                 .println("Performance scaling factor is: " + this.scaleFactor);
382     }
383
384     private void checkTime(String JavaDoc testType, double avgExecTimeMs)
385             throws Exception JavaDoc {
386         System.out.println("Execution time for " + testType + ": "
387                 + avgExecTimeMs);
388
389         Double JavaDoc baselineExecTimeMs = (Double JavaDoc) BASELINE_TIMES.get(testType);
390
391         if (baselineExecTimeMs == null) {
392             throw new Exception JavaDoc("No baseline time recorded for test '"
393                     + testType + "'");
394         }
395
396         double acceptableTime = LEEWAY * baselineExecTimeMs.doubleValue()
397                 * this.scaleFactor;
398
399         assertTrue("Average execution time of " + avgExecTimeMs
400                 + " ms. exceeded baseline * leeway of " + acceptableTime
401                 + " ms.", (avgExecTimeMs <= acceptableTime));
402     }
403
404     public void testBug6359() throws Exception JavaDoc {
405         if (runLongTests()) {
406             int numRows = 550000;
407             int numSelects = 100000;
408
409             try {
410                 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug6359");
411                 this.stmt
412                         .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))");
413
414                 PreparedStatement JavaDoc pStmt = this.conn
415                         .prepareStatement("INSERT INTO testBug6359 (field1, field2, field3, field4, field5, field6, field7, field8, field9) VALUES (?, 1, 2, 3, 4, 5, 6, 7, 8)");
416
417                 logDebug("Loading " + numRows + " rows...");
418
419                 for (int i = 0; i < numRows; i++) {
420                     pStmt.setInt(1, i);
421                     pStmt.executeUpdate();
422
423                     if ((i % 10000) == 0) {
424                         logDebug(i + " rows loaded so far");
425                     }
426                 }
427
428                 logDebug("Finished loading rows");
429
430                 long begin = System.currentTimeMillis();
431
432                 long beginSingleQuery = System.currentTimeMillis();
433
434                 for (int i = 0; i < numSelects; i++) {
435                     this.rs = this.stmt
436                             .executeQuery("SELECT pk_field FROM testBug6359 WHERE field1 BETWEEN 1 AND 5");
437                 }
438
439                 long endSingleQuery = System.currentTimeMillis();
440
441                 double secondsSingleQuery = ((double) endSingleQuery - (double) beginSingleQuery) / 1000;
442
443                 logDebug("time to execute " + numSelects + " single queries: "
444                         + secondsSingleQuery + " seconds");
445
446                 checkTime("single selects", secondsSingleQuery);
447
448                 PreparedStatement JavaDoc pStmt2 = this.conn
449                         .prepareStatement("SELECT field2, field3, field4, field5 FROM testBug6359 WHERE pk_field=?");
450
451                 long beginFiveQueries = System.currentTimeMillis();
452
453                 for (int i = 0; i < numSelects; i++) {
454
455                     for (int j = 0; j < 5; j++) {
456                         pStmt2.setInt(1, j);
457                         pStmt2.executeQuery();
458                     }
459                 }
460
461                 long endFiveQueries = System.currentTimeMillis();
462
463                 double secondsFiveQueries = ((double) endFiveQueries - (double) beginFiveQueries) / 1000;
464
465                 logDebug("time to execute " + numSelects
466                         + " 5 standalone queries: " + secondsFiveQueries
467                         + " seconds");
468
469                 checkTime("5 standalone queries", secondsFiveQueries);
470
471                 long end = System.currentTimeMillis();
472
473                 double seconds = ((double) end - (double) begin) / 1000;
474
475                 logDebug("time to execute " + numSelects + " selects: "
476                         + seconds + " seconds");
477
478                 checkTime("total time all queries", seconds);
479             } finally {
480                 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug6359");
481             }
482         }
483     }
484
485 }
486
Popular Tags