01: package com.jat.integration.db.connectionpool;
02:
03: import java.sql.Connection;
04: import java.sql.SQLException;
05: import java.util.Enumeration;
06: import java.util.Hashtable;
07:
08: import com.jat.integration.DataSourceFactory;
09: import com.jat.integration.IntegrationException;
10:
11: /**
12: * <p>Title: JAT</p>
13: * <p>Description: </p>
14: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
15: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
16: * @author stf
17: * @version 1.0
18: * @since 1.2
19: */
20:
21: public class ConnectionPoolStressTester extends Thread {
22:
23: protected static Hashtable errors = new Hashtable();
24: protected static int errorNum = 0;
25: protected static long[] times;
26: private static int cursor = 0;
27:
28: public static void startStressTester(String dataSourceName,
29: int threads, long sqlTime) throws IntegrationException {
30: times = new long[threads];
31: cursor = 0;
32: long time = System.currentTimeMillis();
33: System.out.println("Starting " + threads
34: + " processes (sql time " + sqlTime + " milliseconds)");
35: errors = new Hashtable();
36: errorNum = 0;
37: ThreadGroup group = new ThreadGroup("group");
38: for (int i = 0; i < threads; i++) {
39: Thread thread = new ConnectionPoolStressTester(group,
40: dataSourceName, sqlTime);
41: thread.start();
42: }
43: while (group.activeCount() > 0) {
44: //System.out.println(group.activeCount());
45: }
46: System.out
47: .println("Total time: "
48: + (System.currentTimeMillis() - time)
49: + " milliseconds");
50: System.out.println("Errors: " + errorNum + " of " + threads);
51: int i = 1;
52: for (Enumeration e = errors.keys(); e.hasMoreElements();)
53: System.out.println("Errors msg " + (i++) + ": "
54: + e.nextElement());
55: for (i = 0; i < threads; i++) {
56: if (i < threads - 1)
57: System.out.print(times[i] + ";");
58: else
59: System.out.println(times[i]);
60: }
61: System.out.println((ConnectionPoolDataSource) DataSourceFactory
62: .getFactory().getDataSource(dataSourceName));
63: }
64:
65: private synchronized static void addTime(long time) {
66: times[cursor++] = time;
67: }
68:
69: public ConnectionPoolStressTester(ThreadGroup group,
70: String dataSourceName, long waitTime)
71: throws IntegrationException {
72: super (group, "" + System.currentTimeMillis());
73: ds = (ConnectionPoolDataSource) DataSourceFactory.getFactory()
74: .getDataSource(dataSourceName);
75: this .waitTime = waitTime;
76: }
77:
78: public void run() {
79: long time = System.currentTimeMillis();
80: try {
81: Connection con = ds.getConnection();
82: try {
83: this .sleep(waitTime);
84: } catch (InterruptedException ex1) {
85: }
86: con.close();
87: } catch (SQLException ex) {
88: errors.put(ex.getMessage(), ex);
89: errorNum++;
90: } finally {
91: addTime(System.currentTimeMillis() - time);
92: }
93: }
94:
95: private long waitTime;
96: private ConnectionPoolDataSource ds;
97: }
|