commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bur...@apache.org
Subject svn commit: r156202 - in jakarta/commons/sandbox/benchmark/trunk/src: java/org/apache/commons/benchmark/Benchmark.java java/org/apache/commons/benchmark/BenchmarkTracker.java test/org/apache/commons/benchmark/Test1.java
Date Fri, 04 Mar 2005 22:19:36 GMT
Author: burton
Date: Fri Mar  4 14:19:35 2005
New Revision: 156202

URL: http://svn.apache.org/viewcvs?view=rev&rev=156202
Log:
support for benchmarking duration... disabled for right now until I can benchmark the benchmark
code...  I want to make sure it doesn't slow down the system much.

Modified:
    jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/Benchmark.java
    jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkTracker.java
    jakarta/commons/sandbox/benchmark/trunk/src/test/org/apache/commons/benchmark/Test1.java

Modified: jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/Benchmark.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/Benchmark.java?view=diff&r1=156201&r2=156202
==============================================================================
--- jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/Benchmark.java
(original)
+++ jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/Benchmark.java
Fri Mar  4 14:19:35 2005
@@ -91,6 +91,14 @@
      * performance overhead zero.
      */
     public static boolean DISABLED = false;
+
+    /**
+     * We support keeping track of local values between start/complete such as
+     * duration. We can disable this for additional throughput.  Enabling this
+     * feature uses threadlocal variables so there's a constant overhead per
+     * thread. 
+     */
+    public static boolean DISABLE_LOCAL = true;
     
     /**
      * Maintain a metadata map between the name and BMeta classes.

Modified: jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkTracker.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkTracker.java?view=diff&r1=156201&r2=156202
==============================================================================
--- jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkTracker.java
(original)
+++ jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkTracker.java
Fri Mar  4 14:19:35 2005
@@ -63,6 +63,15 @@
     public long completed = 0;
 
     /**
+     * The total amount of time (in milliseconds) that threads have spent
+     * between start() and complete() methods.  Note that mean duration can be
+     * computed by duration / completed.
+     */
+    public long duration = 0;
+    
+    // **** Last variables after the rollover ***********************************
+
+    /**
      * Value getTotalStarted
      */
     public long lastStarted = 0;
@@ -73,6 +82,11 @@
     public long lastCompleted = 0;
 
     /**
+     * Keep track of duration.
+     */
+    private static BenchmarkThreadLocal threadlocal = new BenchmarkThreadLocal();
+    
+    /**
      * 
      * Create a new <code>BenchmarkTracker</code> instance.
      */
@@ -134,6 +148,8 @@
             resetWhenNecessary();
             ++started;
 
+            doLocalStart();
+
         }
         
     }
@@ -148,10 +164,43 @@
             resetWhenNecessary();
             ++completed;
 
+            doLocalCompleted();
+            
         }
         
     }
 
+    void doLocalStart() {
+
+        if ( Benchmark.DISABLE_LOCAL )
+            return;
+
+        BenchmarkThreadLocalClosure closure = getClosure();
+        
+        closure.startedTimeMillis = System.currentTimeMillis();
+        
+    }
+
+    void doLocalCompleted() {
+
+        if ( Benchmark.DISABLE_LOCAL )
+            return;
+
+        BenchmarkThreadLocalClosure closure = getClosure();
+        
+        closure.completedTimeMillis = System.currentTimeMillis();
+
+        duration += closure.completedTimeMillis - closure.startedTimeMillis;
+        
+    }
+
+    /**
+     * Get the lexical closure for this benchmark.
+     */
+    BenchmarkThreadLocalClosure getClosure() {
+        return (BenchmarkThreadLocalClosure)threadlocal.get();
+    }
+    
     // **** metadata ************************************************************
 
     /**
@@ -190,4 +239,38 @@
         return completed;
     }
 
-}
\ No newline at end of file
+    public long getDuration() {
+        return duration;
+    }
+
+    public long getMeanDuration() {
+
+        return duration > 0 ? duration / completed : 0;
+
+    }
+    
+}
+
+class BenchmarkThreadLocal extends ThreadLocal {
+
+    public Object initialValue() {
+        return new BenchmarkThreadLocalClosure();
+    }
+
+}
+
+/**
+ * We keep one threadlocal variable per tracker but we keep the data in a
+ * lexical closure so that we don't have to have multiple thread locals each
+ * with dedicated objects.
+ */
+class BenchmarkThreadLocalClosure {
+
+    /**
+     * The time (in millis) that this benchmark was started.
+     */
+    long startedTimeMillis = 0;
+    long completedTimeMillis = 0;
+    
+}
+

Modified: jakarta/commons/sandbox/benchmark/trunk/src/test/org/apache/commons/benchmark/Test1.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/benchmark/trunk/src/test/org/apache/commons/benchmark/Test1.java?view=diff&r1=156201&r2=156202
==============================================================================
--- jakarta/commons/sandbox/benchmark/trunk/src/test/org/apache/commons/benchmark/Test1.java
(original)
+++ jakarta/commons/sandbox/benchmark/trunk/src/test/org/apache/commons/benchmark/Test1.java
Fri Mar  4 14:19:35 2005
@@ -30,6 +30,39 @@
 
     //FIXME: test with using really short intervals but with diff values.
 
+    public void testDuration() throws Exception {
+
+        Benchmark.DISABLE_LOCAL = false;
+        
+        Benchmark benchmark = Benchmark.getBenchmark( Test1.class );
+        benchmark = benchmark.child( "testDuration" );
+
+        benchmark.start();
+        Thread.sleep( 100 );
+        benchmark.complete();
+
+        benchmark.start();
+        Thread.sleep( 100 );
+        benchmark.complete();
+
+        long duration = benchmark.getTracker1().getDuration();
+        long meanDuration = benchmark.getTracker1().getMeanDuration();
+
+        System.out.println( "duration: " + duration );
+        System.out.println( "meanDuration: " + meanDuration );
+
+        assertTrue( 0 < duration );
+        assertTrue( 100 < duration );
+        assertTrue( 150 < duration );
+
+        assertTrue( 0 < meanDuration );
+        assertTrue( 100 < meanDuration );
+        assertTrue( 200 > meanDuration );
+
+        benchmark.reset();
+
+    }
+
     public void testLongSleep() throws Exception {
 
         //   - Potential bug.  If we don't log anything > 5 minutes the LAST benchmark



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message