1 25 package testsuite.perf; 26 27 import java.sql.PreparedStatement ; 28 import java.sql.SQLException ; 29 30 import java.text.NumberFormat ; 31 32 import testsuite.BaseTestCase; 33 34 39 public class LoadStorePerfTest extends BasePerfTest { 40 41 private String tableType = "HEAP"; 42 43 private boolean takeMeasurements = false; 44 45 private boolean useColumnNames = false; 46 47 private boolean largeResults = false; 48 49 55 public LoadStorePerfTest(String name) { 56 super(name); 57 58 String newTableType = System 59 .getProperty("com.mysql.jdbc.test.tabletype"); 60 61 this.largeResults = "TRUE" 62 .equalsIgnoreCase(System 63 .getProperty("com.mysql.jdbc.testsuite.loadstoreperf.useBigResults")); 64 65 if ((newTableType != null) && (newTableType.length() > 0)) { 66 this.tableType = newTableType; 67 68 System.out.println("Using specified table type of '" 69 + this.tableType + "'"); 70 } 71 } 72 73 82 public static void main(String [] args) throws Exception { 83 new LoadStorePerfTest("test1000Transactions").run(); 84 } 85 86 89 public void setUp() throws Exception { 90 super.setUp(); 91 92 try { 93 this.stmt.executeUpdate("DROP TABLE perfLoadStore"); 94 } catch (SQLException sqlEx) { 95 } 97 98 String dateTimeType = "DATETIME"; 99 100 if (BaseTestCase.dbUrl.indexOf("oracle") != -1) { 101 dateTimeType = "TIMESTAMP"; 102 } 103 104 String query = "CREATE TABLE perfLoadStore (priKey INT NOT NULL, " 108 + "fk1 INT NOT NULL, " + "fk2 INT NOT NULL, " + "dtField " 109 + dateTimeType + ", " + "charField1 CHAR(32), " 110 + "charField2 CHAR(32), " + "charField3 CHAR(32), " 111 + "charField4 CHAR(32), " + "intField1 INT, " 112 + "intField2 INT, " + "intField3 INT, " + "intField4 INT, " 113 + "doubleField1 DECIMAL," + "doubleField2 DOUBLE," 114 + "doubleField3 DOUBLE," + "doubleField4 DOUBLE," 115 + "PRIMARY KEY (priKey))"; 116 117 if (BaseTestCase.dbUrl.indexOf("mysql") != -1) { 118 query += (" TYPE=" + this.tableType); 119 } 120 121 this.stmt.executeUpdate(query); 122 123 String currentDateValue = "NOW()"; 124 125 if (BaseTestCase.dbUrl.indexOf("sqlserver") != -1) { 126 currentDateValue = "GETDATE()"; 127 } 128 129 if (BaseTestCase.dbUrl.indexOf("oracle") != -1) { 130 currentDateValue = "CURRENT_TIMESTAMP"; 131 } 132 133 int numLoops = 1; 134 135 if (this.largeResults) { 136 numLoops = 32; 137 } 138 139 System.out.println("Inserting " + numLoops + " rows to retrieve..."); 140 141 for (int i = 0; i < numLoops; i++) { 142 this.stmt.executeUpdate("INSERT INTO perfLoadStore (" + "priKey, " 143 + "fk1, " + "fk2, " + "dtField, " + "charField1, " 144 + "charField2, " + "charField3, " + "charField4, " 145 + "intField1, " + "intField2, " + "intField3, " 146 + "intField4, " + "doubleField1," + "doubleField2," 147 + "doubleField3," + "doubleField4" + ") VALUES (" + i + "," + "2," + "3," + currentDateValue + "," + "'0123456789ABCDEF0123456789ABCDEF'," + "'0123456789ABCDEF0123456789ABCDEF'," + "'0123456789ABCDEF0123456789ABCDEF'," + "'0123456789ABCDEF0123456789ABCDEF'," + "7," + "8," + "9," + "10," + "1.20," + "2.30," + "3.40," + "4.50" + ")"); 164 } 165 } 166 167 170 public void tearDown() throws Exception { 171 try { 172 this.stmt.executeUpdate("DROP TABLE perfLoadStore"); 173 } catch (SQLException sqlEx) { 174 } 176 177 super.tearDown(); 178 } 179 180 186 public void test1000Transactions() throws Exception { 187 this.takeMeasurements = false; 188 warmUp(); 189 this.takeMeasurements = true; 190 doIterations(29); 191 192 reportResults("\n\nResults for instance # 1: "); 193 } 194 195 200 protected void doOneIteration() throws Exception { 201 PreparedStatement pStmtStore = this.conn 202 .prepareStatement("UPDATE perfLoadStore SET " + "priKey = ?, " 203 + "fk1 = ?, " + "fk2 = ?, " + "dtField = ?, " 204 + "charField1 = ?, " + "charField2 = ?, " 205 + "charField3 = ?, " + "charField4 = ?, " 206 + "intField1 = ?, " + "intField2 = ?, " 207 + "intField3 = ?, " + "intField4 = ?, " 208 + "doubleField1 = ?," + "doubleField2 = ?," 209 + "doubleField3 = ?," + "doubleField4 = ?" 210 + " WHERE priKey=?"); 211 PreparedStatement pStmtCheck = this.conn 212 .prepareStatement("SELECT COUNT(*) FROM perfLoadStore WHERE priKey=?"); 213 PreparedStatement pStmtLoad = null; 214 215 if (this.largeResults) { 216 pStmtLoad = this.conn.prepareStatement("SELECT " + "priKey, " 217 + "fk1, " + "fk2, " + "dtField, " + "charField1, " 218 + "charField2, " + "charField3, " + "charField4, " 219 + "intField1, " + "intField2, " + "intField3, " 220 + "intField4, " + "doubleField1," + "doubleField2, " 221 + "doubleField3," + "doubleField4" + " FROM perfLoadStore"); 222 } else { 223 pStmtLoad = this.conn.prepareStatement("SELECT " + "priKey, " 224 + "fk1, " + "fk2, " + "dtField, " + "charField1, " 225 + "charField2, " + "charField3, " + "charField4, " 226 + "intField1, " + "intField2, " + "intField3, " 227 + "intField4, " + "doubleField1," + "doubleField2, " 228 + "doubleField3," + "doubleField4" 229 + " FROM perfLoadStore WHERE priKey=?"); 230 } 231 232 NumberFormat numFormatter = NumberFormat.getInstance(); 233 numFormatter.setMaximumFractionDigits(4); 234 numFormatter.setMinimumFractionDigits(4); 235 236 int transactionCount = 5000; 237 238 if (this.largeResults) { 239 transactionCount = 50; 240 } 241 242 long begin = System.currentTimeMillis(); 243 244 for (int i = 0; i < transactionCount; i++) { 245 this.conn.setAutoCommit(false); 246 pStmtCheck.setInt(1, 1); 247 this.rs = pStmtCheck.executeQuery(); 248 249 while (this.rs.next()) { 250 this.rs.getInt(1); 251 } 252 253 this.rs.close(); 254 255 if (!this.largeResults) { 256 pStmtLoad.setInt(1, 1); 257 } 258 259 this.rs = pStmtLoad.executeQuery(); 260 261 if (this.rs.next()) { 262 int key = this.rs.getInt(1); 263 264 if (!this.useColumnNames) { 265 pStmtStore.setInt(1, key); pStmtStore.setInt(2, this.rs.getInt(2)); pStmtStore.setInt(3, this.rs.getInt(3)); pStmtStore.setTimestamp(4, this.rs.getTimestamp(4)); pStmtStore.setString(5, this.rs.getString(5)); pStmtStore.setString(6, this.rs.getString(7)); pStmtStore.setString(7, this.rs.getString(7)); pStmtStore.setString(8, this.rs.getString(8)); pStmtStore.setInt(9, this.rs.getInt(9)); pStmtStore.setInt(10, this.rs.getInt(10)); pStmtStore.setInt(11, this.rs.getInt(11)); pStmtStore.setInt(12, this.rs.getInt(12)); pStmtStore.setDouble(13, this.rs.getDouble(13)); pStmtStore.setDouble(14, this.rs.getDouble(14)); pStmtStore.setDouble(15, this.rs.getDouble(15)); pStmtStore.setDouble(16, this.rs.getDouble(16)); 282 pStmtStore.setInt(17, key); 283 } else { 284 293 pStmtStore.setInt(1, key); pStmtStore.setInt(2, this.rs.getInt("fk1")); pStmtStore.setInt(3, this.rs.getInt("fk2")); pStmtStore.setTimestamp(4, this.rs.getTimestamp("dtField")); pStmtStore.setString(5, this.rs.getString("charField1")); pStmtStore.setString(6, this.rs.getString("charField2")); pStmtStore.setString(7, this.rs.getString("charField3")); pStmtStore.setString(8, this.rs.getString("charField4")); pStmtStore.setInt(9, this.rs.getInt("intField1")); pStmtStore.setInt(10, this.rs.getInt("intField2")); pStmtStore.setInt(11, this.rs.getInt("intField3")); pStmtStore.setInt(12, this.rs.getInt("intField4")); pStmtStore.setDouble(13, this.rs.getDouble("doubleField1")); pStmtStore.setDouble(14, this.rs.getDouble("doubleField2")); pStmtStore.setDouble(15, this.rs.getDouble("doubleField3")); pStmtStore.setDouble(16, this.rs.getDouble("doubleField4")); 310 pStmtStore.setInt(17, key); 311 } 312 313 pStmtStore.executeUpdate(); 314 } 315 316 this.rs.close(); 317 318 this.conn.commit(); 319 this.conn.setAutoCommit(true); 320 } 321 322 pStmtStore.close(); 323 pStmtCheck.close(); 324 pStmtLoad.close(); 325 326 long end = System.currentTimeMillis(); 327 328 long timeElapsed = (end - begin); 329 330 double timeElapsedSeconds = (double) timeElapsed / 1000; 331 double tps = transactionCount / timeElapsedSeconds; 332 333 if (this.takeMeasurements) { 334 addResult(tps); 335 System.out.print("1 [ " + numFormatter.format(getMeanValue()) 336 + " ] "); 337 } else { 338 System.out.println("Warm-up: " + tps + " trans/sec"); 339 } 340 } 341 342 348 protected void warmUp() throws Exception { 349 try { 350 System.out.print("Warm-up period (10 iterations)"); 351 352 for (int i = 0; i < 10; i++) { 353 doOneIteration(); 354 System.out.print("."); 355 } 356 357 System.out.println(); 358 System.out.println("Warm-up period ends"); 359 System.out.println("\nUnits for this test are transactions/sec."); 360 } catch (Exception ex) { 361 ex.printStackTrace(); 362 363 throw ex; 364 } 365 } 366 } 367 | Popular Tags |