commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bur...@apache.org
Subject svn commit: r156079 - in jakarta/commons/sandbox/benchmark/trunk: project.xml src/java/org/apache/commons/benchmark/Benchmark.java src/java/org/apache/commons/benchmark/BenchmarkTracker.java
Date Thu, 03 Mar 2005 20:32:14 GMT
Author: burton
Date: Thu Mar  3 12:32:12 2005
New Revision: 156079

URL: http://svn.apache.org/viewcvs?view=rev&rev=156079
Log:
refactored benchmark tracking and rollover into dedicated class

Added:
    jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkTracker.java
Modified:
    jakarta/commons/sandbox/benchmark/trunk/project.xml
    jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/Benchmark.java

Modified: jakarta/commons/sandbox/benchmark/trunk/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/benchmark/trunk/project.xml?view=diff&r1=156078&r2=156079
==============================================================================
--- jakarta/commons/sandbox/benchmark/trunk/project.xml (original)
+++ jakarta/commons/sandbox/benchmark/trunk/project.xml Thu Mar  3 12:32:12 2005
@@ -17,7 +17,7 @@
 
     <description>Jakarta Benchmark</description>
 
-    <currentVersion>0.0.1</currentVersion>
+    <currentVersion>0.0.2</currentVersion>
 
     <package>org.apache.commons.benchmark</package>
     

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=156078&r2=156079
==============================================================================
--- 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
Thu Mar  3 12:32:12 2005
@@ -95,56 +95,26 @@
     public static HashMap benchmarks = new HashMap();
 
     /**
-     * Mutext used to prevent threads from corrupting start/complete cycles.
-     */
-    private Object MUTEX = new Object();
-
-    /**
      * The current name of this benchmark.
      */
     private String name = null;
-    
-    /**
-     * The time the current benchmark was started.  -1 for never started.
-     */
-    public long timestamp = -1;
-
-    /**
-     * The current number of "started" benchmarks.  This can be analyzed at
-     * runtime but its recommended that you use totalCompleted, totalStarted
-     */
-    public long started = 0;
-
-    /**
-     * The current number of "completed" benchmarks.  This can be analyzed at
-     * runtime but its recommended that you use totalCompleted, totalStarted
-     */
-    public long completed = 0;
-
-    /**
-     * Value getTotalStarted
-     */
-    public long totalStarted = 0;
-
-    /**
-     * Value getTotalCompleted
-     */
-    public long totalCompleted = 0;
 
+    private BenchmarkTracker tracker = null;
+    
     // **** metadata about this benchmark ***************************************
 
     /**
      * The total number of start()ed benchmarks since in the last rollover.
      */
     public long getTotalStarted() {
-        return totalStarted;
+        return tracker.totalStarted;
     }
 
     /**
      * The total number of complete()ed benchmarks since in the last rollover.
      */
     public long getTotalCompleted() {
-        return totalCompleted;
+        return tracker.totalCompleted;
     }
 
     public String getName() {
@@ -155,47 +125,16 @@
 
     public Benchmark( String name ) {
         this.name = name;
-    }
-    
-    /**
-     * Reset stats if necessary.
-     *
-     */
-    private void doReset() {
-
-        long now = System.currentTimeMillis();
-
-        if ( now - timestamp > INTERVAL ) {
-            
-            //need to perform a swap and save the current benchmark.
-            totalStarted = started;
-            totalCompleted = completed;
-                
-            //reset the benchmark
-            timestamp = now;
-            started = 0;
-            completed = 0;
-                
-        }
-
+        tracker = new BenchmarkTracker( INTERVAL, this );
+        
     }
     
     public void start() {
 
         if ( DISABLED  )
             return;
-        
-        //we need to synchronize on this individual metadata unit because if we
-        //didn't then another thread could come in, and corrupt our metadata
-        //about this benchmark.  Since benchmarks are often performed within
-        //threads this is important.
-        synchronized( MUTEX ) {
 
-            doReset();
-            ++started;
-
-        }
-        
+        tracker.start();
     }
 
     public void complete() {
@@ -203,12 +142,7 @@
         if ( DISABLED  )
             return;
 
-        synchronized( MUTEX ) {
-
-            doReset();
-            ++completed;
-
-        }
+        tracker.complete();
         
     }
 

Added: 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=auto&rev=156079
==============================================================================
--- jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkTracker.java
(added)
+++ jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkTracker.java
Thu Mar  3 12:32:12 2005
@@ -0,0 +1,138 @@
+/*
+ * Copyright 1999,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.benchmark;
+
+import java.util.*;
+
+/**
+ * <p>
+ * Handles physically tracking each benchmark and handling rollover.
+ * 
+ * @author <a href="mailto:burton@rojo.com">Kevin Burton</a>
+ * @version $Id: Benchmark.java,v 1.3 2005/02/16 02:28:09 burton Exp $
+ */
+public class BenchmarkTracker {
+
+    /**
+     * Mutext used to prevent threads from corrupting start/complete cycles.
+     */
+    private Object MUTEX = new Object();
+
+    /**
+     * The benchmark hosting this tracker.
+     */
+    private Benchmark parent = null; 
+
+    private long interval = 0;
+    
+    /**
+     * The time the current benchmark was started.  -1 for never started.
+     */
+    public long timestamp = -1;
+
+    /**
+     * The current number of "started" benchmarks.  This can be analyzed at
+     * runtime but its recommended that you use totalCompleted, totalStarted
+     */
+    public long started = 0;
+
+    /**
+     * The current number of "completed" benchmarks.  This can be analyzed at
+     * runtime but its recommended that you use totalCompleted, totalStarted
+     */
+    public long completed = 0;
+
+    /**
+     * Value getTotalStarted
+     */
+    public long totalStarted = 0;
+
+    /**
+     * Value getTotalCompleted
+     */
+    public long totalCompleted = 0;
+
+    /**
+     * 
+     * Create a new <code>BenchmarkTracker</code> instance.
+     */
+    public BenchmarkTracker( long interval, Benchmark parent ) {
+        this.interval = interval;
+        this.parent = parent;
+    }
+
+    void reset( long now ) {
+
+        //need to perform a swap and save the current benchmark.
+        totalStarted = started;
+        totalCompleted = completed;
+                
+        //reset the benchmark
+        timestamp = now;
+        started = 0;
+        completed = 0;
+
+    }
+    
+    /**
+     * Reset stats if necessary.
+     */
+    void resetWhenNecessary() {
+
+        long now = System.currentTimeMillis();
+
+        if ( now - timestamp > interval ) {
+            
+            reset( now );
+            
+        }
+
+    }
+
+    public void start() {
+
+        if ( parent.DISABLED  )
+            return;
+        
+        //we need to synchronize on this individual metadata unit because if we
+        //didn't then another thread could come in, and corrupt our metadata
+        //about this benchmark.  Since benchmarks are often performed within
+        //threads this is important.
+        synchronized( MUTEX ) {
+
+            resetWhenNecessary();
+            ++started;
+
+        }
+        
+    }
+
+    public void complete() {
+
+        if ( parent.DISABLED  )
+            return;
+
+        synchronized( MUTEX ) {
+
+            resetWhenNecessary();
+            ++completed;
+
+        }
+        
+    }
+
+}
\ No newline at end of file



---------------------------------------------------------------------
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