Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.transaction.log » [javadoc | source]
    1   /**
    2    *
    3    * Copyright 2004 The Apache Software Foundation
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  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;
   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   /**
   33    *
   34    *
   35    * @version $Rev: 54973 $ $Date: 2004-10-17 13:06:11 -0700 (Sun, 17 Oct 2004) $
   36    *
   37    * */
   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           //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 ;
  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                               // journalize COMMITTING record
  133                               Object 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 e) {
  142                           System.err.println(Thread.currentThread().getName());
  143                           e.printStackTrace(System.err);
  144                           exception = true;
  145                       } finally {
  146                           synchronized (mutex) {
  147                               totalXidCount += localXidCount;
  148                               totalDuration += localDuration;
  149                           }
  150                           synchronized (stopBarrier) {
  151                               ++stoppedThreads;
  152                               stopBarrier.notifyAll();
  153                           }
  154                       }
  155   
  156                   }
  157               }
  158                       .start();
  159           }
  160   
  161           // Wait for all the workers to be ready..
  162           long startTime = 0;
  163           synchronized (startBarrier) {
  164               while (startedThreads < workers) startBarrier.wait();
  165               ++startedThreads;
  166               startBarrier.notifyAll();
  167               startTime = System.currentTimeMillis();
  168           }
  169   
  170           // Wait for all the workers to finish.
  171           synchronized (stopBarrier) {
  172               while (stoppedThreads < workers) stopBarrier.wait();
  173           }
  174   
  175           return startTime;
  176   
  177       }
  178   
  179       void printSpeedReport(TransactionLog logger, long startTime, long stopTime, int workers, int xidCount) throws IOException {
  180           long mc = ((long) xidCount) * workers;
  181           long duration = (stopTime - startTime);
  182           long xidsPerSecond = (totalXidCount * 1000 / (duration));
  183           int averageForceTime = logger.getAverageForceTime();
  184           int averageBytesPerForce = logger.getAverageBytesPerForce();
  185           long averageLatency = totalDuration/totalXidCount;
  186           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");
  187           resultsXML.write(logger.getXMLStats() + "\n");
  188           resultsCSV.write("" + workers + "," + xidCount + "," + mc + "," + (mc - totalXidCount) + "," + duration + "," + xidsPerSecond + "," + averageForceTime + "," + averageBytesPerForce  + "," + averageLatency + "\n");
  189   
  190       }
  191   }

Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.transaction.log » [javadoc | source]