KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > ConnectionManagerStressTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.connector.outbound;
19
20 import java.util.HashSet JavaDoc;
21
22 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectorInstanceContextImpl;
23
24 /**
25  * ???
26  *
27  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
28  */

29 public class ConnectionManagerStressTest extends ConnectionManagerTestUtils {
30
31     protected int repeatCount = 200;
32     protected int threadCount = 10;
33     private Object JavaDoc startBarrier = new Object JavaDoc();
34     private Object JavaDoc stopBarrier = new Object JavaDoc();
35     private int startedThreads = 0;
36     private int stoppedThreads = 0;
37     private long totalDuration = 0;
38     private int slowCount = 0;
39     private Object JavaDoc mutex = new Object JavaDoc();
40
41     private Exception JavaDoc e = null;
42
43     public void testNoTransactionCallOneThread() throws Throwable JavaDoc {
44         for (int i = 0; i < repeatCount; i++) {
45             defaultComponentInterceptor.invoke(connectorInstanceContext);
46         }
47     }
48
49     public void testNoTransactionCallMultiThread() throws Throwable JavaDoc {
50         startedThreads = 0;
51         stoppedThreads = 0;
52         for (int t = 0; t < threadCount; t++) {
53             new Thread JavaDoc() {
54                 public void run() {
55                     long localStartTime = 0;
56                     int localSlowCount = 0;
57                     try {
58                         synchronized (startBarrier) {
59                             ++startedThreads;
60                             startBarrier.notifyAll();
61                             while (startedThreads < (threadCount + 1)) {
62                                 startBarrier.wait();
63                             }
64                         }
65                         localStartTime = System.currentTimeMillis();
66                         for (int i = 0; i < repeatCount; i++) {
67                             try {
68                                 long start = System.currentTimeMillis();
69                                 defaultComponentInterceptor.invoke(new ConnectorInstanceContextImpl(new HashSet JavaDoc(), new HashSet JavaDoc()));
70                                 long duration = System.currentTimeMillis() - start;
71                                 if (duration > 100) {
72                                     localSlowCount++;
73                                     log.debug("got a cx: " + i + ", time: " + (duration));
74                                 }
75                             } catch (Throwable JavaDoc throwable) {
76                                 log.debug(throwable.getMessage(), throwable);
77                             }
78                         }
79                     } catch (Exception JavaDoc e) {
80                         log.info(e.getMessage(), e);
81                         ConnectionManagerStressTest.this.e = e;
82                     } finally {
83                         synchronized (stopBarrier) {
84                             ++stoppedThreads;
85                             stopBarrier.notifyAll();
86                         }
87                         long localDuration = System.currentTimeMillis() - localStartTime;
88                         synchronized (mutex) {
89                             totalDuration += localDuration;
90                             slowCount += localSlowCount;
91                         }
92                     }
93                 }
94             }.start();
95         }
96         // Wait for all the workers to be ready..
97
long startTime = 0;
98         synchronized (startBarrier) {
99             while (startedThreads < threadCount) startBarrier.wait();
100             ++startedThreads;
101             startBarrier.notifyAll();
102             startTime = System.currentTimeMillis();
103         }
104
105         // Wait for all the workers to finish.
106
synchronized (stopBarrier) {
107             while (stoppedThreads < threadCount) stopBarrier.wait();
108         }
109         long duration = System.currentTimeMillis() - startTime;
110         log.debug("no tx run, thread count: " + threadCount + ", connection count: " + repeatCount + ", duration: " + duration + ", total duration: " + totalDuration + ", ms per cx request: " + (totalDuration / (threadCount * repeatCount)) + ", slow cx request count: " + slowCount);
111         //return startTime;
112
if (e != null) {
113             throw e;
114         }
115     }
116 }
117
Popular Tags