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 32 public final class ConnectionDispersionTest 33 { 34 private final static int DELAY_TIME = 120000; 35 37 private final static int NUM_THREADS = 600; 38 41 private final static Integer ZERO = new Integer (0); 42 43 private static boolean should_go = false; 44 45 private static DataSource cpds; 46 47 private static int ready_count = 0; 48 49 private static synchronized void setDataSource(DataSource ds) 50 { cpds = ds; } 51 52 private static synchronized DataSource getDataSource() 53 { return cpds; } 54 55 private static synchronized int ready() 56 { return ++ready_count; } 57 58 private static synchronized boolean isReady() 59 { return ready_count == NUM_THREADS; } 60 61 private static synchronized void start() 62 { 63 should_go = true; 64 ConnectionDispersionTest.class.notifyAll(); 65 } 66 67 private static synchronized void stop() 68 { 69 should_go = false; 70 ConnectionDispersionTest.class.notifyAll(); 71 } 72 73 private static synchronized boolean shouldGo() 74 { return should_go; } 75 76 public static void main(String [] argv) 77 { 78 String jdbc_url = null; 79 String username = null; 80 String password = null; 81 if (argv.length == 3) 82 { 83 jdbc_url = argv[0]; 84 username = argv[1]; 85 password = argv[2]; 86 } 87 else if (argv.length == 1) 88 { 89 jdbc_url = argv[0]; 90 username = null; 91 password = null; 92 } 93 else 94 usage(); 95 96 if (! jdbc_url.startsWith("jdbc:") ) 97 usage(); 98 99 try 100 { 101 ComboPooledDataSource ds = new ComboPooledDataSource(); 102 ds.setJdbcUrl( jdbc_url ); 103 ds.setUser( username ); 104 ds.setPassword( password ); 105 setDataSource( ds ); 106 107 List threads = new ArrayList( NUM_THREADS ); 108 109 for (int i = 0; i < NUM_THREADS; ++i) 110 { 111 Thread t = new CompeteThread(); 112 t.start(); 113 threads.add( t ); 114 Thread.currentThread().yield(); 115 } 116 117 synchronized ( ConnectionDispersionTest.class ) 118 { while (! isReady()) ConnectionDispersionTest.class.wait(); } 119 120 System.err.println("Starting the race."); 121 start(); 122 123 System.err.println("Sleeping " + ((float) DELAY_TIME/1000) + 124 " seconds to let the race run"); 125 Thread.sleep(DELAY_TIME); 126 System.err.println("Stopping the race."); 127 stop(); 128 for (int i = 0; i < NUM_THREADS; ++i) 129 ((Thread ) threads.get(i)).join(); 130 131 Map outcomeMap = new TreeMap(); 132 for (int i = 0; i < NUM_THREADS; ++i) 133 { 134 Integer outcome = new Integer ( ((CompeteThread) threads.get(i)).getCount() ); 135 Integer old = (Integer ) outcomeMap.get( outcome ); 136 if (old == null) 137 old = ZERO; 138 outcomeMap.put( outcome, new Integer (old.intValue() + 1) ); 139 } 140 141 int last = 0; 142 for (Iterator ii = outcomeMap.keySet().iterator(); ii.hasNext(); ) 143 { 144 Integer outcome = (Integer ) ii.next(); 145 Integer count = (Integer ) outcomeMap.get( outcome ); 146 int oc = outcome.intValue(); 147 int c = count.intValue(); 148 for (; last < oc; ++last) 149 System.out.println(String.valueOf(10000 + last).substring(1) + ": "); 150 ++last; 151 System.out.print(String.valueOf(10000 + oc).substring(1) + ": "); 152 for(int i = 0; i < c; ++i) 155 System.out.print('*'); 156 System.out.println(); 157 } 158 159 164 } 168 catch (Exception e) 169 { e.printStackTrace(); } 170 } 171 172 static class CompeteThread extends Thread 173 { 174 DataSource ds; 175 int count; 176 177 synchronized void increment() 178 { ++count; } 179 180 synchronized int getCount() 181 { return count; } 182 183 public void run() 184 { 185 try 186 { 187 this.ds = getDataSource(); 188 synchronized ( ConnectionDispersionTest.class ) 189 { 190 ready(); 191 ConnectionDispersionTest.class.wait(); 192 } 193 while ( shouldGo() ) 194 { 195 Connection c = null; 196 ResultSet rs = null; 197 try 198 { 199 c = ds.getConnection(); 200 increment(); 201 rs = c.getMetaData().getTables( null, 202 null, 203 "PROBABLYNOT", 204 new String [] {"TABLE"} ); 205 } 206 catch (SQLException e) 207 { e.printStackTrace(); } 208 finally 209 { 210 try {if (rs != null) rs.close(); } 211 catch (Exception e) 212 { e.printStackTrace(); } 213 214 try {if (c != null) c.close(); } 215 catch (Exception e) 216 { e.printStackTrace(); } 217 } 218 } 219 } 220 catch (Exception e) 221 { e.printStackTrace(); } 222 } 223 } 224 225 private static void usage() 226 { 227 System.err.println("java " + 228 "-Djdbc.drivers=<comma_sep_list_of_drivers> " + 229 ConnectionDispersionTest.class.getName() + 230 " <jdbc_url> [<username> <password>]" ); 231 System.exit(-1); 232 } 233 } 234 | Popular Tags |