[jbosscache-commits] JBoss Cache SVN: r7014 - benchmarks/benchmark-fwk/trunk/src/org/cachebench/tests.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Sat Oct 25 19:50:03 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-10-25 19:50:03 -0400 (Sat, 25 Oct 2008)
New Revision: 7014

Modified:
   benchmarks/benchmark-fwk/trunk/src/org/cachebench/tests/SessionSimulatorTest.java
   benchmarks/benchmark-fwk/trunk/src/org/cachebench/tests/SessionSimulatorTestResult.java
Log:
multithreaded

Modified: benchmarks/benchmark-fwk/trunk/src/org/cachebench/tests/SessionSimulatorTest.java
===================================================================
--- benchmarks/benchmark-fwk/trunk/src/org/cachebench/tests/SessionSimulatorTest.java	2008-10-25 23:25:14 UTC (rev 7013)
+++ benchmarks/benchmark-fwk/trunk/src/org/cachebench/tests/SessionSimulatorTest.java	2008-10-25 23:50:03 UTC (rev 7014)
@@ -8,7 +8,14 @@
 import org.cachebench.config.TestConfig;
 import org.cachebench.tests.results.TestResult;
 
-import java.util.*;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
 
 /**
  * Simulates work with a web session.
@@ -52,6 +59,7 @@
     * Out of the total number of request, this define the frequency of writes (percentage)
     */
    private int writePercentage;
+   private ExecutorService ex;
 
    public void setConfiguration(Configuration configuration)
    {
@@ -60,6 +68,7 @@
 
    public TestResult doTest(String testName, CacheWrapper cache, String testCaseName, int sampleSize, int numThreads) throws Exception
    {
+      ex = Executors.newFixedThreadPool(configuration.getNumThreads());
       this.cacheWrapper = cache;
       TestCase testCase = configuration.getTestCase(testCaseName);
       thisTestConfig = testCase.getTest(testName);
@@ -79,7 +88,7 @@
       Random r = new Random();
       int randomAction;
       int randomAttribute;
-      long durationNanos = 0;
+      AtomicLong durationNanos = new AtomicLong(0);
       for (int i = 0; i < numberOfRequests; i++)
       {
          logRunCount(i);
@@ -91,49 +100,35 @@
          List<String> path = generatePath(sessionId, randomAttribute);
 
          if (randomAction < readPercentage)
-         { // read
-            try
-            {
-               long start = System.nanoTime();
-               buf = (byte[]) cacheWrapper.get(path, sessionEntry);
-               durationNanos += System.nanoTime() - start;
-               totalBytesRead += buf == null ? 0 : buf.length;
-               reads++;
-            }
-            catch (Throwable e)
-            {
-               log.warn("Error appeared whilst reading from cache:" + e.getMessage(), e);
-            }
+         {
+            // read
+            ex.execute(new ReadTask(path, durationNanos, sessionEntry));
+            reads++;
          }
          else
-         {             // write
-            buf = new byte[this.sizeOfAnAttribute];
-            try
-            {
-               long start = System.nanoTime();
-               cacheWrapper.put(path, sessionEntry, buf);
-               durationNanos += System.nanoTime() - start;
-               totalBytesWritten += buf.length;
-               writes++;
-            }
-            catch (Throwable e)
-            {
-               log.warn("Error appeared whilst writing to cache:" + e.getMessage(), e);
-            }
+         {
+            // write
+            ex.execute(new WriteTask(path, durationNanos, sessionEntry));
+            writes++;
          }
       }
-      long replicationDelay = System.currentTimeMillis();
-      replicationDelay = verifyReplicationDelay(replicationDelay);
-      long duration = this.reportNanos ? durationNanos : durationNanos / 1000000;
+
+      ex.shutdown();
+      ex.awaitTermination(60 * 10, TimeUnit.SECONDS);
+
+//      long replicationDelay = System.currentTimeMillis();
+//      replicationDelay = verifyReplicationDelay(replicationDelay);
+
+      long duration = this.reportNanos ? durationNanos.get() : durationNanos.get() / 1000000;
       SessionSimulatorTestResult result = new SessionSimulatorTestResult(reads, writes, duration, totalBytesRead, totalBytesWritten);
       result.setTestPassed(true);
       result.setTestName(testCaseName + getNodeIndex());
       result.setTestTime(new Date());
       result.setTestType(testName);
-      if (registerReplicationDelay)
-      {
-         result.setReplicationDelayMillis(replicationDelay);
-      }
+//      if (registerReplicationDelay)
+//      {
+//         result.setReplicationDelayMillis(replicationDelay);
+//      }
       log.trace("Returning result:" + result);
       return result;
    }
@@ -181,7 +176,7 @@
          {
             if (cacheWrapper.get(path, buildAckKey(i, j)) == null)
             {
-               if (log.isTraceEnabled()) log.trace("Missing replication message: " + buildAckKey(i,j));
+               if (log.isTraceEnabled()) log.trace("Missing replication message: " + buildAckKey(i, j));
                return true;
             }
          }
@@ -242,4 +237,74 @@
       return configuration.isLocalOnly() ? 0 : configuration.getClusterConfig().getCurrentNodeIndex();
    }
 
+   private abstract static class Task implements Runnable
+   {
+      List<String> path;
+      AtomicLong durationNanos;
+      Object sessionEntry;
+
+      protected Task(List<String> path, AtomicLong durationNanos, Object sessionEntry)
+      {
+         this.path = path;
+         this.durationNanos = durationNanos;
+         this.sessionEntry = sessionEntry;
+      }
+   }
+
+   private class WriteTask extends Task
+   {
+      protected WriteTask(List<String> path, AtomicLong durationNanos, Object sessionEntry)
+      {
+         super(path, durationNanos, sessionEntry);
+      }
+
+      public void run()
+      {
+         try
+         {
+            String payload = generateRandomString(sizeOfAnAttribute);
+            long start = System.nanoTime();
+            cacheWrapper.put(path, sessionEntry, payload);
+            durationNanos.getAndAdd(System.nanoTime() - start);
+//            totalBytesWritten += buf.length;
+         }
+         catch (Throwable e)
+         {
+            log.warn("Error appeared whilst writing to cache:" + e.getMessage(), e);
+         }
+      }
+   }
+
+   private class ReadTask extends Task
+   {
+      protected ReadTask(List<String> path, AtomicLong durationNanos, Object sessionEntry)
+      {
+         super(path, durationNanos, sessionEntry);
+      }
+
+      public void run()
+      {
+         try
+         {
+            long start = System.nanoTime();
+            cacheWrapper.get(path, sessionEntry);
+            durationNanos.getAndAdd(System.nanoTime() - start);
+//            totalBytesRead += buf == null ? 0 : buf.length;
+         }
+         catch (Throwable e)
+         {
+            log.warn("Error appeared whilst reading from cache:" + e.getMessage(), e);
+         }
+      }
+   }
+
+   private static Random r = new Random();
+
+   private static String generateRandomString(int size)
+   {
+      // each char is 2 bytes
+      StringBuilder sb = new StringBuilder();
+      for (int i = 0; i < size / 2; i++) sb.append((char) (64 + r.nextInt(26)));
+      return sb.toString();
+   }
 }

Modified: benchmarks/benchmark-fwk/trunk/src/org/cachebench/tests/SessionSimulatorTestResult.java
===================================================================
--- benchmarks/benchmark-fwk/trunk/src/org/cachebench/tests/SessionSimulatorTestResult.java	2008-10-25 23:25:14 UTC (rev 7013)
+++ benchmarks/benchmark-fwk/trunk/src/org/cachebench/tests/SessionSimulatorTestResult.java	2008-10-25 23:50:03 UTC (rev 7014)
@@ -1,7 +1,7 @@
 package org.cachebench.tests;
 
+import org.cachebench.reportgenerators.CsvSessionSimlatorReportGenerator;
 import org.cachebench.tests.results.BaseTestResult;
-import org.cachebench.reportgenerators.CsvSessionSimlatorReportGenerator;
 
 /**
  * @author Mircea.Markus at jboss.com
@@ -82,4 +82,29 @@
    {
       return replicationDelayMillis != -1;
    }
+
+   public void setReadCount(long readCount)
+   {
+      this.readCount = readCount;
+   }
+
+   public void setWriteCount(long writeCount)
+   {
+      this.writeCount = writeCount;
+   }
+
+   public void setDurration(long durration)
+   {
+      this.durration = durration;
+   }
+
+   public void setBytesRead(long bytesRead)
+   {
+      this.bytesRead = bytesRead;
+   }
+
+   public void setBytesWritten(long bytesWritten)
+   {
+      this.bytesWritten = bytesWritten;
+   }
 }




More information about the jbosscache-commits mailing list