KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > regression > StressRegressionTest


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7
8  There are special exceptions to the terms and conditions of the GPL
9  as it is applied to this software. View the full text of the
10  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11  software distribution.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
23
24  */

25 package testsuite.regression;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.sql.Statement JavaDoc;
32 import java.sql.Timestamp JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Properties JavaDoc;
38
39 import testsuite.BaseTestCase;
40
41 /**
42  * Tests for multi-thread stress regressions.
43  *
44  * @author Mark Matthews
45  * @version $Id: StressRegressionTest.java,v 1.1.2.1 2005/05/13 18:58:38
46  * mmatthews Exp $
47  */

48 public class StressRegressionTest extends BaseTestCase {
49     private int numThreadsStarted;
50
51     /**
52      * Creates a new StressRegressionTest
53      *
54      * @param name
55      * the name of the test.
56      */

57     public StressRegressionTest(String JavaDoc name) {
58         super(name);
59
60         // TODO Auto-generated constructor stub
61
}
62
63     /**
64      * Runs all test cases in this test suite
65      *
66      * @param args
67      */

68     public static void main(String JavaDoc[] args) {
69         junit.textui.TestRunner.run(StressRegressionTest.class);
70     }
71
72     /**
73      *
74      *
75      * @throws Exception
76      * ...
77      */

78     public synchronized void testContention() throws Exception JavaDoc {
79         if (false) {
80             System.out.println("Calculating baseline elapsed time...");
81
82             long start = System.currentTimeMillis();
83
84             contentiousWork(this.conn, this.stmt, 0);
85
86             long singleThreadElapsedTimeMillis = System.currentTimeMillis()
87                     - start;
88
89             System.out.println("Single threaded execution took "
90                     + singleThreadElapsedTimeMillis + " ms.");
91
92             int numThreadsToStart = 95;
93
94             System.out.println("\nStarting " + numThreadsToStart + " threads.");
95
96             this.numThreadsStarted = numThreadsToStart;
97
98             ContentionThread[] threads = new ContentionThread[this.numThreadsStarted];
99
100             for (int i = 0; i < numThreadsToStart; i++) {
101                 threads[i] = new ContentionThread(i);
102                 threads[i].start();
103             }
104
105             for (;;) {
106                 try {
107                     wait();
108
109                     if (this.numThreadsStarted == 0) {
110                         break;
111                     }
112                 } catch (InterruptedException JavaDoc ie) {
113                     // ignore
114
}
115             }
116
117             // Collect statistics...
118
System.out.println("Done!");
119
120             double avgElapsedTimeMillis = 0;
121
122             List JavaDoc elapsedTimes = new ArrayList JavaDoc();
123
124             for (int i = 0; i < numThreadsToStart; i++) {
125                 elapsedTimes.add(new Long JavaDoc(threads[i].elapsedTimeMillis));
126
127                 avgElapsedTimeMillis += ((double) threads[i].elapsedTimeMillis / numThreadsToStart);
128             }
129
130             Collections.sort(elapsedTimes);
131
132             System.out.println("Average elapsed time per-thread was "
133                     + avgElapsedTimeMillis + " ms.");
134             System.out.println("Median elapsed time per-thread was "
135                     + elapsedTimes.get(elapsedTimes.size() / 2) + " ms.");
136             System.out.println("Minimum elapsed time per-thread was "
137                     + elapsedTimes.get(0) + " ms.");
138             System.out.println("Maximum elapsed time per-thread was "
139                     + elapsedTimes.get(elapsedTimes.size() - 1) + " ms.");
140         }
141     }
142
143     /**
144      *
145      *
146      * @throws Exception
147      * ...
148      */

149     public void testCreateConnections() throws Exception JavaDoc {
150         new CreateThread().run();
151     }
152
153     /**
154      *
155      *
156      * @throws Exception
157      * ...
158      */

159     public void testCreateConnectionsUnderLoad() throws Exception JavaDoc {
160         new CreateThread(new BusyThread()).run();
161     }
162
163     void contentiousWork(Connection JavaDoc threadConn, Statement JavaDoc threadStmt,
164             int threadNumber) {
165         Date JavaDoc now = new Date JavaDoc();
166
167         try {
168             for (int i = 0; i < 1000; i++) {
169                 ResultSet JavaDoc threadRs = threadStmt.executeQuery("SELECT 1, 2");
170
171                 while (threadRs.next()) {
172                     threadRs.getString(1);
173                     threadRs.getString(2);
174                 }
175
176                 threadRs.close();
177
178                 PreparedStatement JavaDoc pStmt = threadConn
179                         .prepareStatement("SELECT ?");
180                 pStmt.setTimestamp(1, new Timestamp JavaDoc(now.getTime()));
181
182                 threadRs = pStmt.executeQuery();
183
184                 while (threadRs.next()) {
185                     threadRs.getTimestamp(1);
186                 }
187
188                 threadRs.close();
189                 pStmt.close();
190             }
191         } catch (Exception JavaDoc ex) {
192             throw new RuntimeException JavaDoc(ex.toString());
193         }
194     }
195
196     synchronized void reportDone() {
197         this.numThreadsStarted--;
198         notify();
199     }
200
201     public class BusyThread extends Thread JavaDoc {
202         boolean stop = false;
203
204         public void run() {
205             while (!this.stop) {
206             }
207         }
208     }
209
210     class ContentionThread extends Thread JavaDoc {
211         Connection JavaDoc threadConn;
212
213         Statement JavaDoc threadStmt;
214
215         int threadNumber;
216
217         long elapsedTimeMillis;
218
219         public ContentionThread(int num) throws SQLException JavaDoc {
220             this.threadNumber = num;
221             this.threadConn = getConnectionWithProps(new Properties JavaDoc());
222             this.threadStmt = this.threadConn.createStatement();
223
224             System.out.println(this.threadConn);
225         }
226
227         public void run() {
228             long start = System.currentTimeMillis();
229
230             try {
231                 contentiousWork(this.threadConn, this.threadStmt,
232                         this.threadNumber);
233                 this.elapsedTimeMillis = System.currentTimeMillis() - start;
234
235                 System.out
236                         .println("Thread " + this.threadNumber + " finished.");
237             } finally {
238                 if (this.elapsedTimeMillis == 0) {
239                     this.elapsedTimeMillis = System.currentTimeMillis() - start;
240                 }
241
242                 reportDone();
243
244                 try {
245                     this.threadStmt.close();
246                     this.threadConn.close();
247                 } catch (SQLException JavaDoc ex) {
248                     // ignore
249
}
250             }
251         }
252     }
253
254     class CreateThread extends Thread JavaDoc {
255         BusyThread busyThread;
256
257         int numConnections = 15;
258
259         public CreateThread() {
260         }
261
262         public CreateThread(BusyThread toStop) {
263             this.busyThread = toStop;
264         }
265
266         public CreateThread(int numConns) {
267             this.numConnections = numConns;
268         }
269
270         public void run() {
271             try {
272                 Connection JavaDoc[] connList = new Connection JavaDoc[this.numConnections];
273
274                 long maxConnTime = Long.MIN_VALUE;
275                 long minConnTime = Long.MAX_VALUE;
276                 double averageTime = 0;
277
278                 Properties JavaDoc nullProps = new Properties JavaDoc();
279
280                 for (int i = 0; i < this.numConnections; i++) {
281                     long startTime = System.currentTimeMillis();
282                     connList[i] = getConnectionWithProps(nullProps);
283
284                     long endTime = System.currentTimeMillis();
285                     long ellapsedTime = endTime - startTime;
286
287                     if (ellapsedTime < minConnTime) {
288                         minConnTime = ellapsedTime;
289                     }
290
291                     if (ellapsedTime > maxConnTime) {
292                         maxConnTime = ellapsedTime;
293                     }
294
295                     averageTime += ((double) ellapsedTime / this.numConnections);
296                 }
297
298                 if (this.busyThread != null) {
299                     this.busyThread.stop = true;
300                 }
301
302                 for (int i = 0; i < this.numConnections; i++) {
303                     connList[i].close();
304                 }
305
306                 System.out.println(minConnTime + "/" + maxConnTime + "/"
307                         + averageTime);
308             } catch (Exception JavaDoc ex) {
309                 throw new RuntimeException JavaDoc(ex);
310             }
311         }
312     }
313 }
314
Popular Tags