1 17 18 package org.apache.geronimo.transaction.log; 19 20 import java.io.File ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.io.Writer ; 24 import java.util.Collections ; 25 import java.util.List ; 26 27 import javax.transaction.xa.Xid ; 28 29 import junit.framework.TestCase; 30 import org.apache.geronimo.transaction.manager.TransactionLog; 31 32 38 public abstract class AbstractLogTest extends TestCase { 39 private Object startBarrier = new Object (); 40 private Object stopBarrier = new Object (); 41 private int startedThreads = 0; 42 private int stoppedThreads = 0; 43 long totalDuration = 0; 44 private Xid xid; 45 private List names; 46 final Object mutex = new Object (); 47 long totalXidCount = 0; 48 private Writer resultsXML; 49 private Writer resultsCSV; 50 51 public void testDummy() throws Exception {} 52 53 public void testTransactionLog() throws Exception { 54 File resultFileXML = new File (getResultFileName() + ".xml"); 55 resultsXML = new FileWriter (resultFileXML); 56 resultsXML.write("<log-test>\n"); 57 File resultFileCSV = new File (getResultFileName() + ".csv"); 58 resultsCSV = new FileWriter (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 getResultFileName(); 90 91 public long testTransactionLog(int workers, int xidCount) throws Exception { 92 TransactionLog transactionLog = createTransactionLog(); 93 94 xid = new XidImpl2(new byte[Xid.MAXGTRIDSIZE]); 95 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 ; 108 109 protected abstract TransactionLog createTransactionLog() throws Exception ; 110 111 private long journalTest(final TransactionLog logger, final int workers, final int xidCount) 112 throws Exception { 113 totalXidCount = 0; 114 startedThreads = 0; 115 stoppedThreads = 0; 116 totalDuration = 0; 117 for (int i = 0; i < workers; i++) { 118 new Thread () { 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 Object logMark = logger.prepare(xid, names); 134 136 logger.commit(xid, logMark); 138 localXidCount++; 139 } 140 localDuration = System.currentTimeMillis() - localStartTime; 141 } catch (Exception e) { 142 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 long startTime = 0; 167 synchronized (startBarrier) { 168 while (startedThreads < workers) startBarrier.wait(); 169 ++startedThreads; 170 startBarrier.notifyAll(); 171 startTime = System.currentTimeMillis(); 172 } 173 174 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 { 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 |