KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > log > AbstractLogTest


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.transaction.log;
19
20 import java.io.File JavaDoc;
21 import java.io.FileWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.Writer JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.transaction.xa.Xid JavaDoc;
28
29 import junit.framework.TestCase;
30 import org.apache.geronimo.transaction.manager.TransactionLog;
31
32 /**
33  *
34  *
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  *
37  * */

38 public abstract class AbstractLogTest extends TestCase {
39     private Object JavaDoc startBarrier = new Object JavaDoc();
40     private Object JavaDoc stopBarrier = new Object JavaDoc();
41     private int startedThreads = 0;
42     private int stoppedThreads = 0;
43     long totalDuration = 0;
44     private Xid JavaDoc xid;
45     private List JavaDoc names;
46     final Object JavaDoc mutex = new Object JavaDoc();
47     long totalXidCount = 0;
48     private Writer JavaDoc resultsXML;
49     private Writer JavaDoc resultsCSV;
50
51     public void testDummy() throws Exception JavaDoc {}
52
53     public void testTransactionLog() throws Exception JavaDoc {
54         File JavaDoc resultFileXML = new File JavaDoc(getResultFileName() + ".xml");
55         resultsXML = new FileWriter JavaDoc(resultFileXML);
56         resultsXML.write("<log-test>\n");
57         File JavaDoc resultFileCSV = new File JavaDoc(getResultFileName() + ".csv");
58         resultsCSV = new FileWriter JavaDoc(resultFileCSV);
59         resultsCSV.write("workerCount,xidCount,TotalXids,missingXids,DurationMilliseconds,XidsPerSecond,AverageForceTime,AverageBytesPerForce,AverageLatency\n");
60         int xidCount = Integer.getInteger("xa.log.test.xid.count", 50).intValue();
61         int minWorkerCount = Integer.getInteger("xa.log.test.worker.count.min", 20).intValue();
62         int maxWorkerCount = Integer.getInteger("xa.log.test.worker.count.max", 40).intValue();
63         int workerCountStep = Integer.getInteger("xa.log.test.worker.count.step", 20).intValue();
64         int repCount = Integer.getInteger("xa.log.test.repetition.count", 1).intValue();
65         long maxTime = Long.getLong("xa.log.test.max.time.seconds", 30).longValue() * 1000;
66         int overtime = 0;
67         try {
68             for (int workers = minWorkerCount; workers <= maxWorkerCount; workers += workerCountStep) {
69                 for (int reps = 0; reps < repCount; reps++) {
70                     if (testTransactionLog(workers, xidCount) > maxTime) {
71                         overtime++;
72                         if (overtime > 1) {
73                             return;
74                         }
75                     }
76                     resultsCSV.flush();
77                     resultsXML.flush();
78                 }
79             }
80         } finally {
81             resultsXML.write("</log-test>\n");
82             resultsXML.flush();
83             resultsXML.close();
84             resultsCSV.flush();
85             resultsCSV.close();
86         }
87     }
88
89     protected abstract String JavaDoc getResultFileName();
90
91     public long testTransactionLog(int workers, int xidCount) throws Exception JavaDoc {
92         TransactionLog transactionLog = createTransactionLog();
93
94         xid = new XidImpl2(new byte[Xid.MAXGTRIDSIZE]);
95         //TODO Supply an actual list
96
names = Collections.EMPTY_LIST;
97
98         long startTime = journalTest(transactionLog, workers, xidCount);
99
100         long stopTime = System.currentTimeMillis();
101
102         printSpeedReport(transactionLog, startTime, stopTime, workers, xidCount);
103         closeTransactionLog(transactionLog);
104         return stopTime - startTime;
105     }
106
107     protected abstract void closeTransactionLog(TransactionLog transactionLog) throws Exception JavaDoc ;
108
109     protected abstract TransactionLog createTransactionLog() throws Exception JavaDoc;
110
111     private long journalTest(final TransactionLog logger, final int workers, final int xidCount)
112             throws Exception JavaDoc {
113         totalXidCount = 0;
114         startedThreads = 0;
115         stoppedThreads = 0;
116         totalDuration = 0;
117         for (int i = 0; i < workers; i++) {
118             new Thread JavaDoc() {
119                 public void run() {
120                     long localXidCount = 0;
121                     boolean exception = false;
122                     long localDuration = 0;
123                     try {
124                         synchronized (startBarrier) {
125                             ++startedThreads;
126                             startBarrier.notifyAll();
127                             while (startedThreads < (workers + 1)) startBarrier.wait();
128                         }
129                         long localStartTime = System.currentTimeMillis();
130
131                         for (int i = 0; i < xidCount; i++) {
132                             // journalize COMMITTING record
133
Object JavaDoc logMark = logger.prepare(xid, names);
134                             //localXidCount++;
135

136                             // journalize FORGET record
137
logger.commit(xid, logMark);
138                             localXidCount++;
139                         }
140                         localDuration = System.currentTimeMillis() - localStartTime;
141                     } catch (Exception JavaDoc e) {
142                         //
143
// FIXME: Remove System.err usage
144
//
145

146                         System.err.println(Thread.currentThread().getName());
147                         e.printStackTrace(System.err);
148                         exception = true;
149                     } finally {
150                         synchronized (mutex) {
151                             totalXidCount += localXidCount;
152                             totalDuration += localDuration;
153                         }
154                         synchronized (stopBarrier) {
155                             ++stoppedThreads;
156                             stopBarrier.notifyAll();
157                         }
158                     }
159
160                 }
161             }
162                     .start();
163         }
164
165         // Wait for all the workers to be ready..
166
long startTime = 0;
167         synchronized (startBarrier) {
168             while (startedThreads < workers) startBarrier.wait();
169             ++startedThreads;
170             startBarrier.notifyAll();
171             startTime = System.currentTimeMillis();
172         }
173
174         // Wait for all the workers to finish.
175
synchronized (stopBarrier) {
176             while (stoppedThreads < workers) stopBarrier.wait();
177         }
178
179         return startTime;
180
181     }
182
183     void printSpeedReport(TransactionLog logger, long startTime, long stopTime, int workers, int xidCount) throws IOException JavaDoc {
184         long mc = ((long) xidCount) * workers;
185         long duration = (stopTime - startTime);
186         long xidsPerSecond = (totalXidCount * 1000 / (duration));
187         int averageForceTime = logger.getAverageForceTime();
188         int averageBytesPerForce = logger.getAverageBytesPerForce();
189         long averageLatency = totalDuration/totalXidCount;
190         resultsXML.write("<run><workers>" + workers + "</workers><xids-per-thread>" + xidCount + "</xids-per-thread><expected-total-xids>" + mc + "</expected-total-xids><missing-xids>" + (mc - totalXidCount) + "</missing-xids><totalDuration-milliseconds>" + duration + "</totalDuration-milliseconds><xids-per-second>" + xidsPerSecond + "</xids-per-second></run>\n");
191         resultsXML.write(logger.getXMLStats() + "\n");
192         resultsCSV.write("" + workers + "," + xidCount + "," + mc + "," + (mc - totalXidCount) + "," + duration + "," + xidsPerSecond + "," + averageForceTime + "," + averageBytesPerForce + "," + averageLatency + "\n");
193
194     }
195 }
196
Popular Tags