KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > test > ConnectionDispersionTest


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

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     //private final static int DELAY_TIME = 300000;
36

37     private final static int NUM_THREADS = 600;
38     //private final static int NUM_THREADS = 300;
39
//private final static int NUM_THREADS = 50;
40

41     private final static Integer JavaDoc ZERO = new Integer JavaDoc(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 JavaDoc[] argv)
77     {
78     String JavaDoc jdbc_url = null;
79     String JavaDoc username = null;
80     String JavaDoc 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 JavaDoc 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 JavaDoc) threads.get(i)).join();
130
131         Map outcomeMap = new TreeMap();
132         for (int i = 0; i < NUM_THREADS; ++i)
133             {
134             Integer JavaDoc outcome = new Integer JavaDoc( ((CompeteThread) threads.get(i)).getCount() );
135             Integer JavaDoc old = (Integer JavaDoc) outcomeMap.get( outcome );
136             if (old == null)
137                 old = ZERO;
138             outcomeMap.put( outcome, new Integer JavaDoc(old.intValue() + 1) );
139             }
140
141         int last = 0;
142         for (Iterator ii = outcomeMap.keySet().iterator(); ii.hasNext(); )
143             {
144             Integer JavaDoc outcome = (Integer JavaDoc) ii.next();
145             Integer JavaDoc count = (Integer JavaDoc) 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 // if (oc < 10)
153
// System.out.print(' ');
154
for(int i = 0; i < c; ++i)
155                 System.out.print('*');
156             System.out.println();
157             }
158         
159 // List outcomes = new ArrayList(NUM_THREADS);
160
// for (int i = 0; i < NUM_THREADS; ++i)
161
// outcomes.add( new Integer( ((CompeteThread) threads.get(i)).getCount() ) );
162
// Collections.sort( outcomes );
163

164 // System.out.println("Connection counts:");
165
// for (int i = 0; i < NUM_THREADS; ++i)
166
// System.out.println( outcomes.get(i) + " (" + i + ")");
167
}
168     catch (Exception JavaDoc e)
169         { e.printStackTrace(); }
170     }
171
172     static class CompeteThread extends Thread JavaDoc
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 JavaDoc[] {"TABLE"} );
205                 }
206                 catch (SQLException e)
207                 { e.printStackTrace(); }
208                 finally
209                 {
210                     try {if (rs != null) rs.close(); }
211                     catch (Exception JavaDoc e)
212                     { e.printStackTrace(); }
213                     
214                     try {if (c != null) c.close(); }
215                     catch (Exception JavaDoc e)
216                     { e.printStackTrace(); }
217                 }
218             }
219         }
220         catch (Exception JavaDoc 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