001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.c3p0.test;
024:
025: import java.util.*;
026: import java.sql.*;
027: import javax.sql.*;
028: import com.mchange.v2.c3p0.*;
029: import com.mchange.v1.db.sql.*;
030:
031: public final class ConnectionDispersionTest {
032: private final static int DELAY_TIME = 120000;
033: //private final static int DELAY_TIME = 300000;
034:
035: private final static int NUM_THREADS = 600;
036: //private final static int NUM_THREADS = 300;
037: //private final static int NUM_THREADS = 50;
038:
039: private final static Integer ZERO = new Integer(0);
040:
041: private static boolean should_go = false;
042:
043: private static DataSource cpds;
044:
045: private static int ready_count = 0;
046:
047: private static synchronized void setDataSource(DataSource ds) {
048: cpds = ds;
049: }
050:
051: private static synchronized DataSource getDataSource() {
052: return cpds;
053: }
054:
055: private static synchronized int ready() {
056: return ++ready_count;
057: }
058:
059: private static synchronized boolean isReady() {
060: return ready_count == NUM_THREADS;
061: }
062:
063: private static synchronized void start() {
064: should_go = true;
065: ConnectionDispersionTest.class.notifyAll();
066: }
067:
068: private static synchronized void stop() {
069: should_go = false;
070: ConnectionDispersionTest.class.notifyAll();
071: }
072:
073: private static synchronized boolean shouldGo() {
074: return should_go;
075: }
076:
077: public static void main(String[] argv) {
078: String jdbc_url = null;
079: String username = null;
080: String password = null;
081: if (argv.length == 3) {
082: jdbc_url = argv[0];
083: username = argv[1];
084: password = argv[2];
085: } else if (argv.length == 1) {
086: jdbc_url = argv[0];
087: username = null;
088: password = null;
089: } else
090: usage();
091:
092: if (!jdbc_url.startsWith("jdbc:"))
093: usage();
094:
095: try {
096: ComboPooledDataSource ds = new ComboPooledDataSource();
097: ds.setJdbcUrl(jdbc_url);
098: ds.setUser(username);
099: ds.setPassword(password);
100: setDataSource(ds);
101:
102: List threads = new ArrayList(NUM_THREADS);
103:
104: for (int i = 0; i < NUM_THREADS; ++i) {
105: Thread t = new CompeteThread();
106: t.start();
107: threads.add(t);
108: Thread.currentThread().yield();
109: }
110:
111: synchronized (ConnectionDispersionTest.class) {
112: while (!isReady())
113: ConnectionDispersionTest.class.wait();
114: }
115:
116: System.err.println("Starting the race.");
117: start();
118:
119: System.err.println("Sleeping "
120: + ((float) DELAY_TIME / 1000)
121: + " seconds to let the race run");
122: Thread.sleep(DELAY_TIME);
123: System.err.println("Stopping the race.");
124: stop();
125: for (int i = 0; i < NUM_THREADS; ++i)
126: ((Thread) threads.get(i)).join();
127:
128: Map outcomeMap = new TreeMap();
129: for (int i = 0; i < NUM_THREADS; ++i) {
130: Integer outcome = new Integer(((CompeteThread) threads
131: .get(i)).getCount());
132: Integer old = (Integer) outcomeMap.get(outcome);
133: if (old == null)
134: old = ZERO;
135: outcomeMap
136: .put(outcome, new Integer(old.intValue() + 1));
137: }
138:
139: int last = 0;
140: for (Iterator ii = outcomeMap.keySet().iterator(); ii
141: .hasNext();) {
142: Integer outcome = (Integer) ii.next();
143: Integer count = (Integer) outcomeMap.get(outcome);
144: int oc = outcome.intValue();
145: int c = count.intValue();
146: for (; last < oc; ++last)
147: System.out.println(String.valueOf(10000 + last)
148: .substring(1)
149: + ": ");
150: ++last;
151: System.out.print(String.valueOf(10000 + oc)
152: .substring(1)
153: + ": ");
154: // if (oc < 10)
155: // System.out.print(' ');
156: for (int i = 0; i < c; ++i)
157: System.out.print('*');
158: System.out.println();
159: }
160:
161: // List outcomes = new ArrayList(NUM_THREADS);
162: // for (int i = 0; i < NUM_THREADS; ++i)
163: // outcomes.add( new Integer( ((CompeteThread) threads.get(i)).getCount() ) );
164: // Collections.sort( outcomes );
165:
166: // System.out.println("Connection counts:");
167: // for (int i = 0; i < NUM_THREADS; ++i)
168: // System.out.println( outcomes.get(i) + " (" + i + ")");
169: } catch (Exception e) {
170: e.printStackTrace();
171: }
172: }
173:
174: static class CompeteThread extends Thread {
175: DataSource ds;
176: int count;
177:
178: synchronized void increment() {
179: ++count;
180: }
181:
182: synchronized int getCount() {
183: return count;
184: }
185:
186: public void run() {
187: try {
188: this .ds = getDataSource();
189: synchronized (ConnectionDispersionTest.class) {
190: ready();
191: ConnectionDispersionTest.class.wait();
192: }
193: while (shouldGo()) {
194: Connection c = null;
195: ResultSet rs = null;
196: try {
197: c = ds.getConnection();
198: increment();
199: rs = c.getMetaData()
200: .getTables(null, null, "PROBABLYNOT",
201: new String[] { "TABLE" });
202: } catch (SQLException e) {
203: e.printStackTrace();
204: } finally {
205: try {
206: if (rs != null)
207: rs.close();
208: } catch (Exception e) {
209: e.printStackTrace();
210: }
211:
212: try {
213: if (c != null)
214: c.close();
215: } catch (Exception e) {
216: e.printStackTrace();
217: }
218: }
219: }
220: } catch (Exception e) {
221: e.printStackTrace();
222: }
223: }
224: }
225:
226: private static void usage() {
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: }
|