[jbosscache-commits] JBoss Cache SVN: r7272 - in core/branches/flat/src/test/java/org/jboss/starobrno/profiling: testinternals and 1 other directory.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Dec 9 13:17:00 EST 2008


Author: manik.surtani at jboss.com
Date: 2008-12-09 13:17:00 -0500 (Tue, 09 Dec 2008)
New Revision: 7272

Added:
   core/branches/flat/src/test/java/org/jboss/starobrno/profiling/testinternals/
   core/branches/flat/src/test/java/org/jboss/starobrno/profiling/testinternals/Generator.java
   core/branches/flat/src/test/java/org/jboss/starobrno/profiling/testinternals/TaskRunner.java
Modified:
   core/branches/flat/src/test/java/org/jboss/starobrno/profiling/TreeProfileTest.java
Log:
Improved test harness

Modified: core/branches/flat/src/test/java/org/jboss/starobrno/profiling/TreeProfileTest.java
===================================================================
--- core/branches/flat/src/test/java/org/jboss/starobrno/profiling/TreeProfileTest.java	2008-12-09 18:06:59 UTC (rev 7271)
+++ core/branches/flat/src/test/java/org/jboss/starobrno/profiling/TreeProfileTest.java	2008-12-09 18:17:00 UTC (rev 7272)
@@ -7,6 +7,8 @@
 import org.jboss.starobrno.Cache;
 import org.jboss.starobrno.UnitTestCacheFactory;
 import org.jboss.starobrno.config.Configuration;
+import org.jboss.starobrno.profiling.testinternals.Generator;
+import org.jboss.starobrno.profiling.testinternals.TaskRunner;
 import org.jboss.starobrno.tree.Fqn;
 import org.jboss.starobrno.tree.TreeCache;
 import org.jboss.starobrno.tree.TreeCacheImpl;
@@ -18,10 +20,6 @@
 import java.util.ArrayList;
 import java.util.Collections;
 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;
 
 /**
@@ -75,7 +73,6 @@
    }
 
    private List<Fqn> fqns = new ArrayList<Fqn>(MAX_OVERALL_FQNS);
-   private Random r = new Random();
 
    Log log = LogFactory.getLog(TreeProfileTest.class);
 
@@ -107,12 +104,14 @@
       fqns.clear();
       for (int i = 0; i < MAX_OVERALL_FQNS; i++)
       {
-         Fqn fqn = createRandomFqn(r);
-         while (fqns.contains(fqn)) fqn = createRandomFqn(r);
-         if (i % 100 == 0)
+         Fqn fqn;
+         do
          {
-            log.warn("Generated " + i + " fqns");
+            fqn = Generator.createRandomFqn(TREE_DEPTH);
          }
+         while (fqns.contains(fqn));
+
+         if (i % 100 == 0) log.warn("Generated " + i + " fqns");
          fqns.add(fqn);
       }
       System.gc();
@@ -120,14 +119,6 @@
       log.warn("Finished init() phase.  " + printDuration(duration));
    }
 
-   private Fqn createRandomFqn(Random r)
-   {
-      List<String> fqnElements = new ArrayList<String>(TREE_DEPTH);
-      for (int i = 0; i < TREE_DEPTH; i++) fqnElements.add(Integer.toHexString(r.nextInt(Integer.MAX_VALUE)));
-      return Fqn.fromList(fqnElements, true);
-   }
-
-
    protected void startup()
    {
       long startTime = System.currentTimeMillis();
@@ -140,12 +131,12 @@
    private void warmup() throws InterruptedException
    {
       long startTime = System.currentTimeMillis();
-      ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
+      TaskRunner runner = new TaskRunner(NUM_THREADS);
       log.warn("Starting warmup");
       // creates all the Fqns since this can be expensive and we don't really want to measure this (for now)
       for (final Fqn fqn : fqns)
       {
-         exec.execute(new Runnable()
+         runner.execute(new Runnable()
          {
             public void run()
             {
@@ -165,13 +156,13 @@
       // loop through WARMUP_LOOPS gets and puts for JVM optimisation
       for (int i = 0; i < WARMUP_LOOPS; i++)
       {
-         exec.execute(new Runnable()
+         runner.execute(new Runnable()
          {
             public void run()
             {
                try
                {
-                  Fqn fqn = fqns.get(r.nextInt(MAX_OVERALL_FQNS));
+                  Fqn fqn = Generator.getRandomElement(fqns);
                   DummyTransactionManager.getInstance().begin();
                   cache.get(fqn, "key");
                   DummyTransactionManager.getInstance().commit();
@@ -190,8 +181,7 @@
          });
       }
 
-      exec.shutdown();
-      exec.awaitTermination(360, TimeUnit.SECONDS);
+      runner.stop();
 
       long duration = System.currentTimeMillis() - startTime;
       log.warn("Finished warmup.  " + printDuration(duration));
@@ -203,7 +193,8 @@
 
    private void doTest() throws Exception
    {
-      ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
+      TaskRunner runner = new TaskRunner(NUM_THREADS);
+
       log.warn("Starting test");
       int i;
       long print = NUM_OPERATIONS / 10;
@@ -230,17 +221,13 @@
          }
          if (i % print == 0)
             log.warn("processing iteration " + i);
-         exec.execute(r);
+         runner.execute(r);
 //         if (USE_SLEEP) TestingUtil.sleepRandom(MAX_RANDOM_SLEEP_MILLIS);
          if (USE_SLEEP) TestingUtil.sleepThread(MAX_RANDOM_SLEEP_MILLIS);
       }
       log.warn("Finished generating runnables; awaiting executor completion");
       // wait for executors to complete!
-      exec.shutdown();
-      while (!exec.awaitTermination(((long) i), TimeUnit.SECONDS))
-      {
-         Thread.sleep(1);
-      }
+      runner.stop();
 
       // wait up to 1 sec for each call?
       long elapsedTimeNanos = System.nanoTime() - stElapsed;
@@ -278,32 +265,27 @@
 
       public void run()
       {
-         Fqn fqn = fqns.get(r.nextInt(MAX_OVERALL_FQNS));
+         Fqn fqn = Generator.getRandomElement(fqns);
          long d = 0, st = 0;
          try
          {
             switch (mode)
             {
                case PUT:
-                  Object value = getRandomString();
+                  Object value = Generator.getRandomString();
+
                   st = System.nanoTime();
-                  DummyTransactionManager.getInstance().begin();
                   cache.put(fqn, "key", value);
-                  DummyTransactionManager.getInstance().commit();
                   d = System.nanoTime() - st;
                   break;
                case GET:
                   st = System.nanoTime();
-                  DummyTransactionManager.getInstance().begin();
                   cache.get(fqn, "key");
-                  DummyTransactionManager.getInstance().commit();
                   d = System.nanoTime() - st;
                   break;
                case REMOVE:
                   st = System.nanoTime();
-                  DummyTransactionManager.getInstance().begin();
                   cache.remove(fqn, "key");
-                  DummyTransactionManager.getInstance().commit();
                   d = System.nanoTime() - st;
                   break;
             }
@@ -346,18 +328,6 @@
       }
    }
 
-   private String getRandomString()
-   {
-      StringBuilder sb = new StringBuilder();
-      int len = r.nextInt(10);
-
-      for (int i = 0; i < len; i++)
-      {
-         sb.append((char) (63 + r.nextInt(26)));
-      }
-      return sb.toString();
-   }
-
    protected String printDuration(long duration)
    {
       if (duration > 2000)

Added: core/branches/flat/src/test/java/org/jboss/starobrno/profiling/testinternals/Generator.java
===================================================================
--- core/branches/flat/src/test/java/org/jboss/starobrno/profiling/testinternals/Generator.java	                        (rev 0)
+++ core/branches/flat/src/test/java/org/jboss/starobrno/profiling/testinternals/Generator.java	2008-12-09 18:17:00 UTC (rev 7272)
@@ -0,0 +1,36 @@
+package org.jboss.starobrno.profiling.testinternals;
+
+import org.jboss.starobrno.tree.Fqn;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+public class Generator
+{
+   private static final Random r = new Random();
+
+   public static String getRandomString()
+   {
+      StringBuilder sb = new StringBuilder();
+      int len = r.nextInt(10);
+
+      for (int i = 0; i < len; i++)
+      {
+         sb.append((char) (63 + r.nextInt(26)));
+      }
+      return sb.toString();
+   }
+
+   public static <T> T getRandomElement(List<T> list)
+   {
+      return list.get(r.nextInt(list.size()));
+   }
+
+   public static Fqn createRandomFqn(int depth)
+   {
+      List<String> fqnElements = new ArrayList<String>(depth);
+      for (int i = 0; i < depth; i++) fqnElements.add(Integer.toHexString(r.nextInt(Integer.MAX_VALUE)));
+      return Fqn.fromList(fqnElements, true);
+   }
+}

Added: core/branches/flat/src/test/java/org/jboss/starobrno/profiling/testinternals/TaskRunner.java
===================================================================
--- core/branches/flat/src/test/java/org/jboss/starobrno/profiling/testinternals/TaskRunner.java	                        (rev 0)
+++ core/branches/flat/src/test/java/org/jboss/starobrno/profiling/testinternals/TaskRunner.java	2008-12-09 18:17:00 UTC (rev 7272)
@@ -0,0 +1,30 @@
+package org.jboss.starobrno.profiling.testinternals;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Essentially a delegate to an ExecutorService, but a special one that is only used by perf tests so it can be ignored
+ * when profiling.
+ */
+public class TaskRunner
+{
+   ExecutorService exec;
+
+   public TaskRunner(int numThreads)
+   {
+      this.exec = Executors.newFixedThreadPool(numThreads);
+   }
+
+   public void execute(Runnable r)
+   {
+      exec.execute(r);
+   }
+
+   public void stop() throws InterruptedException
+   {
+      exec.shutdown();
+      while (!exec.awaitTermination(30, TimeUnit.SECONDS)) Thread.sleep(30);
+   }
+}




More information about the jbosscache-commits mailing list