1 22 23 24 package com.mchange.v2.c3p0.stmt; 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 32 public final class StatementCacheBenchmark 33 { 34 final static String EMPTY_TABLE_CREATE = "CREATE TABLE emptyyukyuk (a varchar(8), b varchar(8))"; 35 final static String EMPTY_TABLE_SELECT = "SELECT * FROM emptyyukyuk"; 36 final static String EMPTY_TABLE_DROP = "DROP TABLE emptyyukyuk"; 37 38 final static String EMPTY_TABLE_CONDITIONAL_SELECT = "SELECT * FROM emptyyukyuk where a = ?"; 39 40 final static int NUM_ITERATIONS = 2000; 41 42 public static void main(String [] argv) 43 { 44 DataSource ds_unpooled = null; 45 DataSource ds_pooled = null; 46 try 47 { 48 49 String jdbc_url = null; 50 String username = null; 51 String password = null; 52 if (argv.length == 3) 53 { 54 jdbc_url = argv[0]; 55 username = argv[1]; 56 password = argv[2]; 57 } 58 else if (argv.length == 1) 59 { 60 jdbc_url = argv[0]; 61 username = null; 62 password = null; 63 } 64 else 65 usage(); 66 67 if (! jdbc_url.startsWith("jdbc:") ) 68 usage(); 69 70 ds_unpooled = DriverManagerDataSourceFactory.create(jdbc_url, username, password); 71 ds_pooled 72 = PoolBackedDataSourceFactory.create(jdbc_url, 73 username, 74 password, 75 5, 76 20, 77 5, 78 0, 79 100 ); 80 81 create(ds_pooled); 82 83 perform( ds_pooled, "pooled" ); 84 perform( ds_unpooled, "unpooled" ); 85 } 86 catch( Exception e ) 87 { e.printStackTrace(); } 88 finally 89 { 90 try { drop(ds_pooled); } 91 catch (Exception e) 92 { e.printStackTrace(); } 93 } 94 } 95 96 private static void perform( DataSource ds, String name ) 97 throws SQLException 98 { 99 Connection c = null; 100 PreparedStatement ps = null; 101 try 102 { 103 c = ds.getConnection(); 104 long start = System.currentTimeMillis(); 105 for (int i = 0; i < NUM_ITERATIONS; ++i) 106 { 107 PreparedStatement test = 108 c.prepareStatement( EMPTY_TABLE_CONDITIONAL_SELECT ); 109 test.close(); 110 } 111 long end = System.currentTimeMillis(); 112 System.err.println(name + " --> " + 113 (end - start) / (float) NUM_ITERATIONS + 114 " [" + NUM_ITERATIONS + " iterations]"); 115 } 116 finally 117 { 118 StatementUtils.attemptClose( ps ); 119 ConnectionUtils.attemptClose( c ); 120 } 121 } 122 123 private static void usage() 124 { 125 System.err.println("java " + 126 "-Djdbc.drivers=<comma_sep_list_of_drivers> " + 127 StatementCacheBenchmark.class.getName() + 128 " <jdbc_url> [<username> <password>]" ); 129 System.exit(-1); 130 } 131 132 static void create(DataSource ds) 133 throws SQLException 134 { 135 System.err.println("Creating test schema."); 136 Connection con = null; 137 PreparedStatement ps1 = null; 138 try 139 { 140 con = ds.getConnection(); 141 ps1 = con.prepareStatement(EMPTY_TABLE_CREATE); 142 ps1.executeUpdate(); 143 System.err.println("Test schema created."); 144 } 145 finally 146 { 147 StatementUtils.attemptClose( ps1 ); 148 ConnectionUtils.attemptClose( con ); 149 } 150 } 151 152 static void drop(DataSource ds) 153 throws SQLException 154 { 155 Connection con = null; 156 PreparedStatement ps1 = null; 157 try 158 { 159 con = ds.getConnection(); 160 ps1 = con.prepareStatement(EMPTY_TABLE_DROP); 161 ps1.executeUpdate(); 162 } 163 finally 164 { 165 StatementUtils.attemptClose( ps1 ); 166 ConnectionUtils.attemptClose( con ); 167 } 168 System.err.println("Test schema dropped."); 169 } 170 } 171 172 173 174 175 176 | Popular Tags |