Author: burton
Date: Fri Mar  4 00:01:11 2005
New Revision: 156140

URL: http://svn.apache.org/viewcvs?view=rev&rev=156140
Log:
Support for proxy benchmarks....

Added:
    
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/proxy/
    
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/proxy/BenchmarkProxyFactory.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=156139&r2=156140
==============================================================================
--- 
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 00:01:11 2005
@@ -16,6 +16,8 @@
 
 package org.apache.commons.benchmark;
 
+import org.apache.commons.benchmark.proxy.*;
+
 import java.util.*;
 
 /**
@@ -252,9 +254,15 @@
         return getBenchmark( this.name + "." + name );
         
     }
-    
+
     // **** static code 
*********************************************************
 
+    public static Object getBenchmarkAsProxy( Object _instance, Class 
_interface ) {
+
+        return BenchmarkProxyFactory.newBenchmarkFactory( _instance, 
_interface );
+        
+    }
+
     /**
      * Factory method for obtaining a benchmark.  This method uses the callers
      * classname by performing a quick stack analysis.  Note that this is slow
@@ -300,6 +308,33 @@
 
     }  
 
+    /**
+     * Return a map of all known benchmarks.
+     *
+     * @author <a href="mailto:[EMAIL PROTECTED]">Kevin A. Burton</a>
+     */
+    public static Map getBenchmarks() {
+        return Collections.unmodifiableMap( benchmarks );
+    }
+
+    // **** Object methods 
******************************************************
+
+    public String toString() {
+        return "1: " +
+            "current=("  +
+            getTracker1().getStarted() +
+            "," +
+            getTracker1().getCompleted() +
+            ")" + 
+            " " +
+            "last=("  +
+            getTracker1().getLastStarted() +
+            "," +
+            getTracker1().getLastCompleted() +
+            ")";
+            
+    }
+    
     // **** test code 
***********************************************************
 
     /**

Added: 
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/proxy/BenchmarkProxyFactory.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/proxy/BenchmarkProxyFactory.java?view=auto&rev=156140
==============================================================================
--- 
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/proxy/BenchmarkProxyFactory.java
 (added)
+++ 
jakarta/commons/sandbox/benchmark/trunk/src/java/org/apache/commons/benchmark/proxy/BenchmarkProxyFactory.java
 Fri Mar  4 00:01:11 2005
@@ -0,0 +1,66 @@
+/*
+ * 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.proxy;
+
+import org.apache.commons.benchmark.*;
+
+import java.lang.reflect.*;
+
+public class BenchmarkProxyFactory {
+
+    public static Object newBenchmarkFactory( Object _instance,
+                                              Class _interface ) {
+
+        Object proxy = Proxy.newProxyInstance( _interface.getClassLoader(),
+                                               new Class[] { _interface },
+                                               new BenchmarkInvocationHandler( 
_instance ) );
+
+        return proxy;
+        
+    }
+    
+}
+
+class BenchmarkInvocationHandler implements InvocationHandler {
+
+    private Object target = null;
+    
+    public BenchmarkInvocationHandler( Object target ) {
+        this.target = target;
+    }
+
+    public Object invoke( Object proxy, Method method, Object[] args ) throws 
Throwable {
+
+        String name = method.getDeclaringClass().getName() + "." + 
method.getName();
+        
+        Benchmark benchmark = Benchmark.getBenchmark( name );
+
+        //before our method
+        benchmark.start();
+
+        //need to set this as accessible or we can't call it when its in a 
diff package.
+        method.setAccessible( true );
+        Object result = method.invoke( target, args );
+
+        //after our method
+        benchmark.complete();
+        
+        return result;
+
+    }
+
+}
\ No newline at end of file

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=156139&r2=156140
==============================================================================
--- 
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 00:01:11 2005
@@ -28,6 +28,27 @@
 
     //FIXME: test with using really short intervals but with diff values.
 
+    public void testProxy() throws Exception {
+
+        IFoo foo = new Foo();
+
+        foo = (IFoo)Benchmark.getBenchmarkAsProxy( foo, IFoo.class );
+
+        foo.doSomething();
+
+        Benchmark benchmark = Benchmark.getBenchmark( 
"org.apache.commons.benchmark.IFoo.doSomething" );
+
+        assertNotNull( benchmark );
+
+        System.out.println( "Not null! " );
+
+        assertEquals( 1, benchmark.getTracker1().getStarted() );
+        assertEquals( 1, benchmark.getTracker1().getCompleted() );
+
+        System.out.println( Benchmark.getBenchmarks() );
+        
+    }
+
     public void testIntervalReset() throws Exception {
 
         //this is cheating a bit.  We set the intervals smaller so that we can
@@ -42,18 +63,18 @@
         benchmark.start();
         benchmark.complete();
 
-        assertEquals( 1, benchmark.getTracker( Benchmark.INTERVAL_1 
).getStarted() );
-        assertEquals( 1, benchmark.getTracker( Benchmark.INTERVAL_1 
).getCompleted() );
+        assertEquals( 1, benchmark.getTracker1().getStarted() );
+        assertEquals( 1, benchmark.getTracker1().getCompleted() );
 
-        assertEquals( 1, benchmark.getTracker( Benchmark.INTERVAL_5 
).getStarted() );
-        assertEquals( 1, benchmark.getTracker( Benchmark.INTERVAL_15 
).getCompleted() );
+        assertEquals( 1, benchmark.getTracker5().getStarted() );
+        assertEquals( 1, benchmark.getTracker15().getCompleted() );
 
         Thread.sleep( 1000 );
 
-        assertEquals( 0, benchmark.getTracker( Benchmark.INTERVAL_1 
).getStarted() );
+        assertEquals( 0, benchmark.getTracker1().getStarted() );
 
         Thread.sleep( 5000 );
-        assertEquals( 0, benchmark.getTracker( Benchmark.INTERVAL_5 
).getStarted() );
+        assertEquals( 0, benchmark.getTracker5().getStarted() );
 
         //reset all the trackers
         benchmark.reset();
@@ -98,4 +119,18 @@
 
     }
 
+}
+
+//test for working with a proxyied benchmark
+
+interface IFoo {
+
+    public void doSomething();
+    
+}
+
+class Foo implements IFoo {
+
+    public void doSomething() { }
+    
 }



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

Reply via email to