1 22 23 24 package com.mchange.v2.c3p0.test; 25 26 import java.util.*; 27 import java.sql.*; 28 import javax.sql.*; 29 import com.mchange.v2.c3p0.*; 30 import com.mchange.v1.db.sql.*; 31 import com.mchange.v2.c3p0.DriverManagerDataSource; 32 33 public final class C3P0BenchmarkApp 34 { 35 final static String EMPTY_TABLE_CREATE = "CREATE TABLE emptyyukyuk (a varchar(8), b varchar(8))"; 36 final static String EMPTY_TABLE_SELECT = "SELECT * FROM emptyyukyuk"; 37 final static String EMPTY_TABLE_DROP = "DROP TABLE emptyyukyuk"; 38 39 final static String EMPTY_TABLE_CONDITIONAL_SELECT = "SELECT * FROM emptyyukyuk where a = ?"; 40 41 final static String N_ENTRY_TABLE_CREATE = "CREATE TABLE n_entryyukyuk (a INTEGER)"; 42 final static String N_ENTRY_TABLE_INSERT = "INSERT INTO n_entryyukyuk VALUES ( ? )"; 43 final static String N_ENTRY_TABLE_SELECT = "SELECT * FROM n_entryyukyuk"; 44 final static String N_ENTRY_TABLE_DROP = "DROP TABLE n_entryyukyuk"; 45 46 final static int NUM_ITERATIONS = 2000; 48 52 public static void main(String [] argv) 53 { 54 if (argv.length > 0) 55 { 56 System.err.println( C3P0BenchmarkApp.class.getName() + 57 " now requires no args. Please set everything in standard c3p0 config files."); 58 return; 59 } 60 61 65 68 69 DataSource ds_unpooled = null; 70 DataSource ds_pooled = null; 71 try 72 { 73 95 96 98 109 ds_unpooled = new DriverManagerDataSource(); 112 113 116 122 124 ComboPooledDataSource cpds = new ComboPooledDataSource(); 126 ds_pooled = cpds; 130 131 create(ds_pooled); 132 133 System.out.println("Please wait. Tests can be very slow."); 134 List l = new ArrayList(); 135 l.add( new ConnectionAcquisitionTest() ); 136 l.add( new StatementCreateTest() ); 137 l.add( new StatementEmptyTableSelectTest() ); 138 l.add( new PreparedStatementEmptyTableSelectTest() ); 140 l.add( new PreparedStatementAcquireTest() ); 141 l.add( new ResultSetReadTest() ); 142 l.add( new FiveThreadPSQueryTestTest() ); 143 for (int i = 0, len = l.size(); i < len; ++i) 144 ((Test) l.get(i)).perform( ds_unpooled, ds_pooled, NUM_ITERATIONS ); 145 } 146 catch( Throwable t ) 147 { 148 System.err.print("Aborting tests on Throwable -- "); 149 t.printStackTrace(); 150 if (t instanceof Error ) 151 throw (Error ) t; 152 } 153 finally 154 { 155 157 try { drop(ds_pooled); } 158 catch (Exception e) 159 { e.printStackTrace(); } 160 161 try { DataSources.destroy(ds_pooled); } 162 catch (Exception e) 163 { e.printStackTrace(); } 164 165 try { DataSources.destroy(ds_unpooled); } 166 catch (Exception e) 167 { e.printStackTrace(); } 168 } 169 } 170 171 181 static void create(DataSource ds) 182 throws SQLException 183 { 184 System.err.println("Creating test schema."); 185 Connection con = null; 186 PreparedStatement ps1 = null; 187 PreparedStatement ps2 = null; 188 PreparedStatement ps3 = null; 189 try 190 { 191 con = ds.getConnection(); 192 ps1 = con.prepareStatement(EMPTY_TABLE_CREATE); 193 ps2 = con.prepareStatement(N_ENTRY_TABLE_CREATE); 194 ps3 = con.prepareStatement(N_ENTRY_TABLE_INSERT); 195 196 ps1.executeUpdate(); 197 ps2.executeUpdate(); 198 199 for (int i = 0; i < NUM_ITERATIONS; ++i) 200 { 201 ps3.setInt(1, i ); 202 ps3.executeUpdate(); 203 System.err.print('.'); 204 } 205 System.err.println(); 206 System.err.println("Test schema created."); 207 } 208 finally 209 { 210 StatementUtils.attemptClose( ps1 ); 211 StatementUtils.attemptClose( ps2 ); 212 StatementUtils.attemptClose( ps3 ); 213 ConnectionUtils.attemptClose( con ); 214 } 215 } 216 217 static void drop(DataSource ds) 218 throws SQLException 219 { 220 Connection con = null; 221 PreparedStatement ps1 = null; 222 PreparedStatement ps2 = null; 223 try 224 { 225 con = ds.getConnection(); 226 ps1 = con.prepareStatement(EMPTY_TABLE_DROP); 227 ps2 = con.prepareStatement(N_ENTRY_TABLE_DROP); 228 229 ps1.executeUpdate(); 230 ps2.executeUpdate(); 231 232 235 System.err.println("Test schema dropped."); 236 } 237 finally 238 { 239 StatementUtils.attemptClose( ps1 ); 240 StatementUtils.attemptClose( ps2 ); 241 ConnectionUtils.attemptClose( con ); 242 } 243 } 244 245 static abstract class Test 246 { 247 String name; 248 249 Test(String name) 250 { this.name = name; } 251 252 public void perform(DataSource unpooled, DataSource pooled, int iterations) throws Exception 253 { 254 double msecs_unpooled = test(unpooled, iterations) / ((double) iterations); 255 double msecs_pooled = test(pooled, iterations) / ((double) iterations); 256 System.out.println(name + " [ " + iterations + " iterations ]:"); 257 System.out.println('\t' + "unpooled: " + msecs_unpooled + " msecs"); 258 System.out.println('\t' + " pooled: " + msecs_pooled + " msecs"); 259 System.out.println('\t' + "speed-up factor: " + msecs_unpooled / msecs_pooled + " times"); 260 System.out.println('\t' + "speed-up absolute: " + (msecs_unpooled - msecs_pooled) + 261 " msecs"); 262 System.out.println(); 263 264 } 270 271 protected abstract long test(DataSource ds, int n) throws Exception ; 272 } 273 274 static class ConnectionAcquisitionTest extends Test 275 { 276 ConnectionAcquisitionTest() 277 { super("Connection Acquisition and Cleanup"); } 278 279 protected long test(DataSource ds, int n) throws Exception 280 { 281 long start; 282 long end; 283 284 start = System.currentTimeMillis(); 285 for (int i = 0; i < n; ++i) 286 { 287 Connection con = null; 288 try 289 { con = ds.getConnection(); } 290 finally 291 { ConnectionUtils.attemptClose( con ); } 292 } 294 end = System.currentTimeMillis(); 295 return end - start; 296 } 297 } 298 299 static class StatementCreateTest extends Test 300 { 301 StatementCreateTest() 302 { super("Statement Creation and Cleanup"); } 303 304 protected long test(DataSource ds, int n) throws SQLException 305 { 306 Connection con = null; 307 try 308 { 309 con = ds.getConnection(); 310 return test( con , n ); 311 } 312 finally 313 { ConnectionUtils.attemptClose( con ); } 314 } 316 317 long test(Connection con, int n) throws SQLException 318 { 319 long start; 320 long end; 321 322 Statement stmt = null; 323 start = System.currentTimeMillis(); 324 for (int i = 0; i < n; ++i) 325 { 326 try 327 { stmt = con.createStatement(); } 328 finally 329 { StatementUtils.attemptClose( stmt ); } 330 } 331 end = System.currentTimeMillis(); 332 return end - start; 333 } 334 } 335 336 337 static class StatementEmptyTableSelectTest extends Test 338 { 339 StatementEmptyTableSelectTest() 340 { super("Empty Table Statement Select (on a single Statement)"); } 341 342 protected long test(DataSource ds, int n) throws SQLException 343 { 344 Connection con = null; 345 Statement stmt = null; 346 try 347 { 348 con = ds.getConnection(); 349 stmt = con.createStatement(); 350 return test( stmt , n ); 352 } 353 finally 354 { 355 StatementUtils.attemptClose( stmt ); 356 ConnectionUtils.attemptClose( con ); 357 } 358 } 359 360 long test(Statement stmt, int n) throws SQLException 361 { 362 long start; 363 long end; 364 365 start = System.currentTimeMillis(); 366 for (int i = 0; i < n; ++i) 367 stmt.executeQuery(EMPTY_TABLE_SELECT).close(); 368 end = System.currentTimeMillis(); 369 return end - start; 370 } 371 } 372 373 static class DataBaseMetaDataListNonexistentTablesTest extends Test 374 { 375 DataBaseMetaDataListNonexistentTablesTest() 376 { super("DataBaseMetaDataListNonexistentTablesTest"); } 377 378 protected long test(DataSource ds, int n) throws SQLException 379 { 380 Connection con = null; 381 Statement stmt = null; 382 try 383 { 384 con = ds.getConnection(); 385 return test( con , n ); 386 } 387 finally 388 { 389 StatementUtils.attemptClose( stmt ); 390 ConnectionUtils.attemptClose( con ); 391 } 392 } 393 394 long test(Connection con, int n) throws SQLException 395 { 396 ResultSet rs = null; 397 398 try 399 { 400 long start; 401 long end; 402 403 start = System.currentTimeMillis(); 404 for (int i = 0; i < n; ++i) 405 rs = con.getMetaData().getTables( null, 406 null, 407 "PROBABLYNOT", 408 new String [] {"TABLE"} ); 409 end = System.currentTimeMillis(); 410 return end - start; 411 } 412 finally 413 { ResultSetUtils.attemptClose( rs ); } 414 } 415 } 416 417 static class PreparedStatementAcquireTest extends Test 418 { 419 PreparedStatementAcquireTest() 420 { super("Acquire and Cleanup a PreparedStatement (same statement, many times)"); } 421 422 protected long test(DataSource ds, int n) throws SQLException 423 { 424 long start; 425 long end; 426 427 Connection con = null; 428 PreparedStatement pstmt = null; 429 try 430 { 431 con = ds.getConnection(); 432 start = System.currentTimeMillis(); 433 for (int i = 0; i < n; ++i) 434 { 435 try 436 { pstmt = con.prepareStatement(EMPTY_TABLE_CONDITIONAL_SELECT); } 437 438 451 finally 452 { StatementUtils.attemptClose( pstmt ); } 453 } 454 end = System.currentTimeMillis(); 455 return end - start; 456 } 457 finally 458 { ConnectionUtils.attemptClose( con ); } 459 } 460 } 461 462 static class PreparedStatementEmptyTableSelectTest extends Test 463 { 464 PreparedStatementEmptyTableSelectTest() 465 { super("Empty Table PreparedStatement Select (on a single PreparedStatement)"); } 466 467 protected long test(DataSource ds, int n) throws SQLException 468 { 469 Connection con = null; 470 PreparedStatement pstmt = null; 471 try 472 { 473 con = ds.getConnection(); 474 pstmt = con.prepareStatement(EMPTY_TABLE_SELECT); 475 476 return test( pstmt , n ); 483 } 484 finally 485 { 486 StatementUtils.attemptClose( pstmt ); 487 ConnectionUtils.attemptClose( con ); 488 } 489 } 490 491 long test(PreparedStatement pstmt, int n) throws SQLException 492 { 493 long start; 494 long end; 495 496 start = System.currentTimeMillis(); 497 for (int i = 0; i < n; ++i) 498 pstmt.executeQuery().close(); 499 end = System.currentTimeMillis(); 500 return end - start; 501 } 502 } 503 504 static class ResultSetReadTest extends Test 505 { 506 ResultSetReadTest() 507 { super("Reading one row / one entry from a result set"); } 508 509 protected long test(DataSource ds, int n) throws SQLException 510 { 511 if (n > 10000) 512 throw new IllegalArgumentException ("10K max."); 513 514 long start; 515 long end; 516 517 Connection con = null; 518 PreparedStatement pstmt = null; 519 ResultSet rs = null; 520 521 try 522 { 523 con = ds.getConnection(); 524 pstmt = con.prepareStatement(N_ENTRY_TABLE_SELECT); 525 rs = pstmt.executeQuery(); 526 527 start = System.currentTimeMillis(); 528 for (int i = 0; i < n; ++i) 529 { 530 if (! rs.next() ) 531 System.err.println("huh?"); 532 rs.getInt(1); 533 } 534 end = System.currentTimeMillis(); 535 return end - start; 536 } 537 finally 538 { 539 ResultSetUtils.attemptClose( rs ); 540 StatementUtils.attemptClose( pstmt ); 541 ConnectionUtils.attemptClose( con ); 542 } 543 } 544 } 545 546 static class FiveThreadPSQueryTestTest extends Test 547 { 548 551 FiveThreadPSQueryTestTest() 552 { 553 super( "Five threads getting a connection, executing a query, " + 554 System.getProperty( "line.separator" ) + 555 "and retrieving results concurrently via a prepared statement (in a transaction)." ); 556 } 557 558 protected long test(final DataSource ds, final int n) throws Exception 559 { 560 class QueryThread extends Thread 561 { 562 QueryThread(int num) 563 { super("QueryThread-" + num);} 564 565 public void run() 566 { 567 Connection con = null; 568 PreparedStatement pstmt = null; 569 ResultSet rs = null; 570 571 for (int i = 0; i < (n / 5); ++i) 572 { 573 try 574 { 575 con = ds.getConnection(); 576 577 582 con.setAutoCommit( false ); 583 584 pstmt = con.prepareStatement( EMPTY_TABLE_CONDITIONAL_SELECT ); 585 586 591 594 pstmt.setString(1, "boo"); 595 rs = pstmt.executeQuery(); 596 while( rs.next() ) 597 System.err.println("Huh?? Empty table has values?"); 598 600 609 con.commit(); 610 } 611 catch (Exception e) 612 { 613 System.err.print("FiveThreadPSQueryTestTest exception -- "); 614 e.printStackTrace(); 615 try { if (con != null) con.rollback(); } 616 catch (SQLException e2) 617 { 618 System.err.print("Rollback on exception failed! -- "); 619 e2.printStackTrace(); 620 } 621 } 622 finally 623 { 624 ResultSetUtils.attemptClose( rs ); 625 StatementUtils.attemptClose( pstmt ); 626 ConnectionUtils.attemptClose( con ); 627 con = null; 628 629 } 634 } 635 } 637 } 638 639 long start = System.currentTimeMillis(); 640 641 Thread [] ts = new Thread [5]; 642 for (int i = 0; i < 5; ++i) 643 { 644 ts[i] = new QueryThread(i); 645 ts[i].start(); 646 } 647 for (int i = 0; i < 5; ++i) 648 ts[i].join(); 649 650 return System.currentTimeMillis() - start; 651 } 652 653 } 654 655 656 661 666 669 673 696 697 715 } 719 720 721 722 723 724 | Popular Tags |