commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bur...@apache.org
Subject svn commit: r157235 - in jakarta/commons/sandbox/benchmark/trunk/src: java/org/apache/commons/benchmark/Benchmark.java java/org/apache/commons/benchmark/BenchmarkUtils.java test/org/apache/commons/benchmark/Test1.java
Date Sat, 12 Mar 2005 06:29:01 GMT
Author: burton
Date: Fri Mar 11 22:28:59 2005
New Revision: 157235

URL: http://svn.apache.org/viewcvs?view=rev&rev=157235
Log:
Ability to specify just a constructor now

Added:
    jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkUtils.java
Modified:
    jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/Benchmark.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=157234&r2=157235
==============================================================================
--- 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 11 22:28:59 2005
@@ -108,19 +108,34 @@
     /**
      * The current name of this benchmark.
      */
-    private String name = null;
+    String name = null;
 
     /**
      * The current benchmark.
      */
-    private BenchmarkTracker tracker1 = null;
-    private BenchmarkTracker tracker5 = null;
-    private BenchmarkTracker tracker15 = null;
+    BenchmarkTracker tracker1 = null;
+    BenchmarkTracker tracker5 = null;
+    BenchmarkTracker tracker15 = null;
+
+    /**
+     * When benchmarks are created with a constructor we only store them as a
+     * valid benchmark when start/complete is called without a given operation.
+     */
+    boolean registered = false;
+    
+    /**
+     * Create a benchmark for the current class as caller.
+     */
+    public Benchmark() {
+
+        this( getCallerClassname() );
+
+    }
 
     /**
      * Create a new benchmark with a given name.
      */
-    private Benchmark( String name ) {
+    public Benchmark( String name ) {
 
         this.name = name;
 
@@ -205,6 +220,8 @@
         if ( DISABLED  )
             return;
 
+        doRegisterWhenNecessary();
+
         tracker1.start();
         tracker5.start();
         tracker15.start();
@@ -220,12 +237,21 @@
         if ( DISABLED  )
             return;
 
+        doRegisterWhenNecessary();
+        
         tracker1.complete();
         tracker5.complete();
         tracker15.complete();
         
     }
 
+    private void doRegisterWhenNecessary() {
+
+        if ( registered == false )
+            benchmarks.put( name, this );
+        
+    }
+    
     /**
      * Return a child benchmark of the current method.  This can be used to
      * return a benchmark for a specific method based on a benchmark for a
@@ -249,9 +275,19 @@
     static String getCallerClassname() {
 
         Exception e = new Exception();
-        String name = e.getStackTrace()[2].getClassName();
 
-        return name;
+        StackTraceElement trace[] = e.getStackTrace();
+
+        for ( int i = 2; i < trace.length; ++i  ) {
+            
+            String name = trace[i].getClassName();
+
+            if ( name.startsWith( "org.apache.commons.benchmark" ) == false )
+                return name;
+            
+        }
+
+        return null;
         
     }
 
@@ -322,6 +358,7 @@
             if ( benchmark == null ) {
                 benchmark= new Benchmark( name );
                 benchmarks.put( name, benchmark );
+                benchmark.registered = true;
             }
 
             return benchmark;

Added: 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=auto&rev=157235
==============================================================================
--- jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkUtils.java
(added)
+++ jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/BenchmarkUtils.java
Fri Mar 11 22:28:59 2005
@@ -0,0 +1,87 @@
+/*
+ * 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.*;
+import java.lang.reflect.*;
+
+/**
+ * 
+ * @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 BenchmarkUtils {
+
+    public static void benchmarkMethod( String name ) throws Exception {
+        benchmarkMethod( name, 10 );
+    }
+        
+    public static void benchmarkMethod( String name,
+                                        int max ) throws Exception {
+
+        String caller = Benchmark.getCallerClassname();
+
+        Class clazz = Class.forName( caller );
+        
+        System.gc();
+
+        Method method = clazz.getMethod( name, null );
+
+        System.out.println( "Testing method: " + name );
+
+        long memoryBefore = getUsedMemory();
+        long duration = 0;
+
+        for ( int i = 0; i <= max; ++i ) {
+
+            long before = System.currentTimeMillis();
+            
+            method.invoke( null, null );
+
+            if ( i == 0 )
+                continue; //don't measure the first call
+
+            long after = System.currentTimeMillis();
+            duration += after-before;
+            
+        }
+
+        System.out.println( "----------------" );
+        System.out.println( "Total parse count: " + max );
+
+        System.out.println( "Total duration: " + duration + "  milliseconds" );
+
+        System.gc();
+
+        long memoryAfter = getUsedMemory();
+        
+        float totalAvgDuration = (float)duration / (float)max;
+
+        System.out.println( "Total avg duration: " + totalAvgDuration + "  milliseconds"
);
+
+        float totalPerSecond = 1000 / totalAvgDuration;
+
+        System.out.println( "Total per second: " + totalPerSecond );
+        System.out.println( "Used memory: "  + (memoryAfter-memoryBefore) );
+        
+    }
+
+    private static long getUsedMemory() {
+        return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
+    }
+
+}

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=157234&r2=157235
==============================================================================
--- 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 11 22:28:59 2005
@@ -33,6 +33,25 @@
         super(testName);
     }
 
+    public void testBasicConstructor() throws Exception {
+
+        resetForTests();
+        
+        Benchmark b = new Benchmark();
+
+        assertEquals( b.name, "org.apache.commons.benchmark.Test1" );
+        
+        assertNull( Benchmark.benchmarks.get( b.name ) );
+
+        b.start();
+        b.complete();
+
+        assertNotNull( Benchmark.benchmarks.get( b.name ) );
+
+        resetForTests();
+        
+    }
+        
     public void testThreads() throws Exception {
 
         int count = 100;
@@ -140,7 +159,7 @@
         System.out.println( "Total bytes used by benchmark: " + usedMemory );
         assertTrue( usedMemory < 500 * count );
 
-        Benchmark.benchmarks = new HashMap();
+        resetForTests();
         //Benchmark.benchmarks.clear();
     }
     
@@ -396,6 +415,11 @@
         
     }
 
+    private void resetForTests() {
+        Benchmark.benchmarks = new HashMap();
+
+    }
+    
 }
 
 //test for working with a proxyied 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