Author: hadrian
Date: Fri Dec 14 21:09:12 2012
New Revision: 1422087

URL: http://svn.apache.org/viewvc?rev=1422087&view=rev
Log:
Add ProxyConstructor for faster proxy creation

Added:
    
ode/trunk/jacob/src/main/java/org/apache/ode/jacob/vpu/ProxyConstructor.java   
(with props)
    ode/trunk/jacob/src/test/java/org/apache/ode/jacob/vpu/
    
ode/trunk/jacob/src/test/java/org/apache/ode/jacob/vpu/ProxyConstructorTimingTest.java
   (with props)
Modified:
    
ode/trunk/jacob/src/main/java/org/apache/ode/jacob/vpu/ExecutionQueueImpl.java

Modified: 
ode/trunk/jacob/src/main/java/org/apache/ode/jacob/vpu/ExecutionQueueImpl.java
URL: 
http://svn.apache.org/viewvc/ode/trunk/jacob/src/main/java/org/apache/ode/jacob/vpu/ExecutionQueueImpl.java?rev=1422087&r1=1422086&r2=1422087&view=diff
==============================================================================
--- 
ode/trunk/jacob/src/main/java/org/apache/ode/jacob/vpu/ExecutionQueueImpl.java 
(original)
+++ 
ode/trunk/jacob/src/main/java/org/apache/ode/jacob/vpu/ExecutionQueueImpl.java 
Fri Dec 14 21:09:12 2012
@@ -96,8 +96,6 @@ public class ExecutionQueueImpl implemen
 
     private int _objIdCounter;
 
-    private ExecutionQueueStatistics _statistics = new 
ExecutionQueueStatistics();
-
     private ReplacementMap _replacementMap;
 
     private Serializable _gdata;
@@ -380,7 +378,6 @@ public class ExecutionQueueImpl implemen
         ps.println("-- GENERAL INFO");
         ps.println("   Current Cycle          : " + _currentCycle);
         ps.println("   Num. Reactions  : " + _reactions.size());
-        _statistics.printStatistics(ps);
         if (!_reactions.isEmpty()) {
             ps.println("-- REACTIONS");
             int cnt = 0;

Added: 
ode/trunk/jacob/src/main/java/org/apache/ode/jacob/vpu/ProxyConstructor.java
URL: 
http://svn.apache.org/viewvc/ode/trunk/jacob/src/main/java/org/apache/ode/jacob/vpu/ProxyConstructor.java?rev=1422087&view=auto
==============================================================================
--- 
ode/trunk/jacob/src/main/java/org/apache/ode/jacob/vpu/ProxyConstructor.java 
(added)
+++ 
ode/trunk/jacob/src/main/java/org/apache/ode/jacob/vpu/ProxyConstructor.java 
Fri Dec 14 21:09:12 2012
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.ode.jacob.vpu;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+
+
+/**
+ * Interface implemented by channel proxies.
+ */
+public class ProxyConstructor<T> {
+       private Constructor<T> constructor;
+       @SuppressWarnings("unchecked")
+       public ProxyConstructor(Class<T> clazz) {
+           try {
+                   Class<?> proxyClass = 
Proxy.getProxyClass(clazz.getClassLoader(), new Class[] {clazz});
+                       constructor = (Constructor<T>) 
proxyClass.getConstructor(new Class[] { InvocationHandler.class });
+               } catch (Exception e) {
+                       // TODO: ignore? LOG?
+               }
+       }
+       
+       public T newInstance(InvocationHandler handler) {
+               try {
+                       return constructor != null ? 
(T)constructor.newInstance(handler) : null;
+               } catch (Exception e) {
+                       // TODO: really ignore?
+               }
+               return null;
+       }
+}

Propchange: 
ode/trunk/jacob/src/main/java/org/apache/ode/jacob/vpu/ProxyConstructor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
ode/trunk/jacob/src/test/java/org/apache/ode/jacob/vpu/ProxyConstructorTimingTest.java
URL: 
http://svn.apache.org/viewvc/ode/trunk/jacob/src/test/java/org/apache/ode/jacob/vpu/ProxyConstructorTimingTest.java?rev=1422087&view=auto
==============================================================================
--- 
ode/trunk/jacob/src/test/java/org/apache/ode/jacob/vpu/ProxyConstructorTimingTest.java
 (added)
+++ 
ode/trunk/jacob/src/test/java/org/apache/ode/jacob/vpu/ProxyConstructorTimingTest.java
 Fri Dec 14 21:09:12 2012
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.ode.jacob.vpu;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
+import junit.framework.TestCase;
+
+
+public class ProxyConstructorTimingTest extends TestCase {
+       private static final long COUNT = 1000000L;
+    public ProxyConstructorTimingTest(String testName) {
+        super(testName);
+    }
+
+    public void testDoNothing() throws Exception {
+    }
+
+    public interface TestExecution {
+        public void execute() throws Exception;        
+    }
+
+    public class RepeatExecution implements TestExecution {
+       private final long count;
+       private final TestExecution test;
+       public RepeatExecution(long count, TestExecution test) {
+               this.count = count;
+               this.test = test;
+       }
+        public void execute() throws Exception {
+               for (long i = 0; i < count; i++) {
+                       test.execute();
+               }
+        }
+    }
+
+    public class TimedExecution implements TestExecution {
+       private final String name;
+       private final TestExecution test;
+       public TimedExecution(String name, TestExecution test) {
+               this.name = name;
+               this.test = test;
+       }
+        public void execute() throws Exception {
+               NanoTimer timer = new NanoTimer().start();
+               test.execute();
+               System.out.println("TimedExecution(" + name + "): " + 
timer.stop() + "[ns]");
+        }
+    }
+
+    public void timedRepeatedExecution(String name, TestExecution test) throws 
Exception {
+       new TimedExecution(name, new RepeatExecution(COUNT, test)).execute();
+    }
+
+    public void manualTestProxyTiming() throws Exception {
+       timedRepeatedExecution("direct invocation", new TestExecution() {
+                       @Override
+                       public void execute() throws Exception {
+                               // Create new instance every time
+                   new GreeterImpl2().hello("World");
+                       }
+       });
+
+       timedRepeatedExecution("newProxyInstance", new TestExecution() {
+                       @Override
+                       public void execute() throws Exception {
+                   Greeter gp = (Greeter) 
Proxy.newProxyInstance(Greeter.class.getClassLoader(),
+                   new Class<?>[] {Greeter.class}, new 
GreeterInvocationHandler(new GreeterImpl2()));
+                       gp.hello("World");
+                       }
+       });
+
+           final ProxyConstructor<Greeter> helper = new 
ProxyConstructor<Greeter>(Greeter.class);
+       timedRepeatedExecution("ProxyConstructor", new TestExecution() {
+                       @Override
+                       public void execute() throws Exception {
+                   Greeter gp = (Greeter) helper.newInstance(new 
GreeterInvocationHandler(new GreeterImpl2()));
+                   gp.hello("World");
+                       }
+       });
+    }
+    
+    public interface Greeter {
+       String hello(String name);
+    }
+    
+    public class GreeterImpl implements Greeter {
+               public String hello(String name) {
+                       return "Hello " + name;
+               }
+    }
+
+    public class GreeterImpl2 implements Greeter {
+               public String hello(String name) {
+                       return "";
+               }
+    }
+
+    public class GreeterInvocationHandler implements InvocationHandler {
+       private Object greeter;
+       GreeterInvocationHandler(Object o) {
+               greeter = o;
+       }
+
+       public Object invoke(Object proxy, Method method, Object[] args) throws 
Throwable {
+                  if(Object.class  == method.getDeclaringClass()) {
+                      String name = method.getName();
+                      if("equals".equals(name)) {
+                          return proxy == args[0];
+                      } else if("hashCode".equals(name)) {
+                          return System.identityHashCode(proxy);
+                      } else if("toString".equals(name)) {
+                          return proxy.getClass().getName() + "@" +
+                              
Integer.toHexString(System.identityHashCode(proxy)) +
+                              ", with InvocationHandler " + this;
+                      } else {
+                          throw new 
IllegalStateException(String.valueOf(method));
+                      }
+                  }
+                  return method.invoke(greeter, args);
+               }       
+    }
+
+    // TODO: may be useful for other things? move it somewhere else?
+    public class NanoTimer {
+       private long start;
+       private long lap;
+       // TODO: we could also count laps...
+       public NanoTimer() {
+               // don't start by default, easy to just call .start();
+       }
+       public NanoTimer start() {
+               start = System.nanoTime();
+               lap = start;
+               return this;
+       }
+       public long stop() {
+               long span = System.nanoTime() - start;
+               start = 0;
+               lap = 0;
+               return span;
+       }
+       public long lap() {
+               long prev = lap;
+               lap = (start != 0) ? System.nanoTime() : 0;
+               return lap - prev;
+       }
+    }
+}

Propchange: 
ode/trunk/jacob/src/test/java/org/apache/ode/jacob/vpu/ProxyConstructorTimingTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to