Author: burton
Date: Sat Mar 12 14:18:52 2005
New Revision: 157287

URL: http://svn.apache.org/viewcvs?view=rev&rev=157287
Log:
refactored to not use stdout but to return as metadata

Modified:
    
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkMeta.java
    
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkUtils.java
    
jakarta/commons/sandbox/benchmark/trunk/src/test/org/apache/commons/benchmark/Test1.java
    jakarta/commons/sandbox/benchmark/trunk/xdocs/index.xml

Modified: 
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkMeta.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkMeta.java?view=diff&r1=157286&r2=157287
==============================================================================
--- 
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkMeta.java
 (original)
+++ 
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkMeta.java
 Sat Mar 12 14:18:52 2005
@@ -17,6 +17,8 @@
 package org.apache.commons.benchmark;
 
 /**
+ * Used to represent metadata for a benchmark including name, timestamp,
+ * started, completed, etc.
  * 
  * @author <a href="mailto:[EMAIL PROTECTED]">Kevin Burton</a>
  * @version $Id: Benchmark.java,v 1.3 2005/02/16 02:28:09 burton Exp $
@@ -32,6 +34,18 @@
     public long duration = 0;
 
     /**
+     * The name of this metadata result (optional)
+     */
+    public String name = null;
+
+    /**
+     * Track the total amount of memory available BEFORE this benchmark.  -1 if
+     * not available.  
+     */
+    public long memoryBefore = -1;
+    public long memoryAfter = -1;
+    
+    /**
      * The time the current benchmark was started.  -1 for never started.
      */
     public long getTimestamp() {
@@ -70,10 +84,46 @@
         return duration > 0 ? duration / completed : 0;
     }
 
+    public float getCompletedPerSecond() {
+
+        long meanDuration = getMeanDuration();
+
+        if ( meanDuration == 0 )
+            return 0;
+        
+        return  1000 / getMeanDuration();
+    }
+    
     public void reset() {
         started = 0;
         completed = 0;
         duration = 0;
+    }
+
+    /**
+     * Build a human readable report from the benchmark.  This should include
+     * information such as mean duration, memory used, etc.
+     *
+     * @author <a href="mailto:[EMAIL PROTECTED]">Kevin A. Burton</a>
+     */
+    public String getReport() {
+
+        StringBuffer buff = new StringBuffer();
+
+        buff.append( "----------------------\n" );;
+        buff.append( "Results from method test: " + name + "\n" );
+        buff.append( "Total duration: " + duration + "milliseconds \n" );
+        buff.append( "Total completed: " + completed + "\n" );
+        buff.append( "Total completed per second: " + getCompletedPerSecond() 
+ "\n" );
+
+        if ( memoryBefore > -1 ) {
+
+            buff.append( "Used memory: "  + (memoryAfter-memoryBefore) + "\n" 
);
+
+        }
+        
+        return buff.toString();
+
     }
     
 }

Modified: 
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkUtils.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkUtils.java?view=diff&r1=157286&r2=157287
==============================================================================
--- 
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkUtils.java
 (original)
+++ 
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkUtils.java
 Sat Mar 12 14:18:52 2005
@@ -26,12 +26,18 @@
  */
 public class BenchmarkUtils {
 
-    public static void benchmarkMethod( String name ) throws Exception {
-        benchmarkMethod( name, 10 );
+    public static BenchmarkMeta benchmarkMethod( String name ) throws 
Exception {
+        return benchmarkMethod( name, 10 );
     }
         
-    public static void benchmarkMethod( String name,
-                                        int max ) throws Exception {
+    /**
+     * Benchmark the performance of a given method.
+     * 
+     * @param name The name of the method from the caller (found via 
reflection).
+     * @author <a href="mailto:[EMAIL PROTECTED]">Kevin A. Burton</a>
+     */
+    public static BenchmarkMeta benchmarkMethod( String name,
+                                                 int numIterations ) throws 
Exception {
 
         String caller = Benchmark.getCallerClassname();
 
@@ -41,12 +47,13 @@
 
         Method method = clazz.getMethod( name, null );
 
-        System.out.println( "Testing method: " + name );
+        BenchmarkMeta bmeta = new BenchmarkMeta();
+        bmeta.name = name;
 
-        long memoryBefore = getUsedMemory();
+        bmeta.memoryBefore = getUsedMemory();
         long duration = 0;
 
-        for ( int i = 0; i <= max; ++i ) {
+        for ( int i = 0; i <= numIterations; ++i ) {
 
             long before = System.currentTimeMillis();
             
@@ -60,23 +67,20 @@
             
         }
 
-        System.out.println( "----------------" );
-        System.out.println( "Total parse count: " + max );
+        bmeta.duration = duration;
 
-        System.out.println( "Total duration: " + duration + "  milliseconds" );
+        bmeta.started = numIterations;
+        bmeta.completed = numIterations;
 
         System.gc();
 
-        long memoryAfter = getUsedMemory();
+        bmeta.memoryAfter = getUsedMemory();
         
-        float totalAvgDuration = (float)duration / (float)max;
-
-        System.out.println( "Total avg duration: " + totalAvgDuration + "  
milliseconds" );
+        float totalAvgDuration = (float)duration / (float)numIterations;
 
         float totalPerSecond = 1000 / totalAvgDuration;
 
-        System.out.println( "Total per second: " + totalPerSecond );
-        System.out.println( "Used memory: "  + (memoryAfter-memoryBefore) );
+        return bmeta;
         
     }
 
@@ -85,3 +89,4 @@
     }
 
 }
+

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=157286&r2=157287
==============================================================================
--- 
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
 Sat Mar 12 14:18:52 2005
@@ -33,6 +33,9 @@
         super(testName);
     }
 
+    //FIXME: write unit test for PERFORMANCE.  With it enabled/disabled we
+    //should be able to call it FREQUENTLY without killing the CPU.
+    
     public void testBasicConstructor() throws Exception {
 
         resetForTests();

Modified: jakarta/commons/sandbox/benchmark/trunk/xdocs/index.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/benchmark/trunk/xdocs/index.xml?view=diff&r1=157286&r2=157287
==============================================================================
--- jakarta/commons/sandbox/benchmark/trunk/xdocs/index.xml (original)
+++ jakarta/commons/sandbox/benchmark/trunk/xdocs/index.xml Sat Mar 12 14:18:52 
2005
@@ -96,11 +96,10 @@
             </p>
   
             <p>
-                This class is lightweight (only requires a hashmap entry, and
-                3x32 bytes per benchmark of storage with no external storage
-                requirements.  This class is also threadsafe so if you need to
-                call this from multithreaded code to benchmark then you'll be
-                ok.
+                This class is lightweight and only requires 500 bytes per
+                benchmark of memory with no external storage requirements.  
This
+                class is also threadsafe so if you need to call this from
+                multithreaded code to benchmark then you'll be ok.
             </p>
  
             <p>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to