[jboss-cvs] JBossAS SVN: r103967 - in projects/jboss-reflect/trunk/src/test/java/org/jboss/test: benchmark and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 14 09:08:43 EDT 2010


Author: kabir.khan at jboss.com
Date: 2010-04-14 09:08:42 -0400 (Wed, 14 Apr 2010)
New Revision: 103967

Added:
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/benchmark/
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/benchmark/AccessorBenchmark.java
Log:
Add benchmark

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/benchmark/AccessorBenchmark.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/benchmark/AccessorBenchmark.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/benchmark/AccessorBenchmark.java	2010-04-14 13:08:42 UTC (rev 103967)
@@ -0,0 +1,187 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors. 
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/ 
+package org.jboss.test.benchmark;
+
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtField;
+import javassist.CtMethod;
+import javassist.CtPrimitiveType;
+import javassist.Modifier;
+import junit.framework.TestCase;
+
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.ConstructorInfo;
+import org.jboss.reflect.spi.FieldInfo;
+import org.jboss.reflect.spi.MethodInfo;
+import org.jboss.reflect.spi.PrimitiveInfo;
+import org.jboss.reflect.spi.TypeInfo;
+import org.jboss.reflect.spi.TypeInfoFactory;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class AccessorBenchmark extends TestCase
+{
+   TypeInfoFactory factory = new org.jboss.reflect.plugins.javassist.JavassistTypeInfoFactory();
+//   TypeInfoFactory factory = new org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory();
+   
+   int numClasses = 100;
+   int numFields = 100;
+   int numGetMembers = 50;
+   int numJoinpoint = 50;
+   
+   
+   public void testAccessors() throws Throwable
+   {
+      System.out.println("========== " + factory.getClass().getSimpleName());
+      
+      Class<?>[] classes = setupClasses();
+      
+      long start = System.currentTimeMillis();
+      
+      ClassInfo[] classInfos = setupClassInfos(classes);
+      start = outputTime(start, "A - Created " + numClasses + " Class Infos");
+      
+      getFieldsAndMethods(classInfos);
+      start = outputTime(start, "B - Getting " + numFields + " fields and methods for " + numClasses + " classes " + numGetMembers + " times");
+    
+      useJoinpoint(classInfos, 1);
+      start = outputTime(start, "C - First accessing " + numFields + " fields and methods for " + numClasses + " classes " + numJoinpoint + " times");
+
+      useJoinpoint(classInfos, numJoinpoint);
+      start = outputTime(start, "D - Accessing " + numFields + " fields and methods for " + numClasses + " classes " + numJoinpoint + " times");
+
+      System.out.println("Done!");
+   }
+   
+   private void useJoinpoint(ClassInfo[] classInfos, int numJoinpoint) throws Throwable
+   {
+      TypeInfo[] params = new TypeInfo[] {PrimitiveInfo.INT};
+      Object[] values = new Object[1];
+      
+      for (int c = 0 ; c < classInfos.length ; c++)
+      {
+         ConstructorInfo ctor = classInfos[c].getDeclaredConstructor(null);
+         Object o = ctor.newInstance(null);
+         
+         for (int f = 0 ; f < numFields ; f++)
+         {
+            FieldInfo field = classInfos[c].getDeclaredField(fieldName(f));
+            Integer val = Integer.valueOf(f);
+            
+            for (int i = 0 ; i < numJoinpoint ; i ++)
+            {
+               field.set(o, val);
+               assertEquals(val, field.get(o));
+            }
+         }
+         
+         for (int m = 0 ; m < numFields ; m++)
+         {
+            MethodInfo method = classInfos[c].getDeclaredMethod(methodName(m), params);
+            values[0] = Integer.valueOf(m);
+
+            for (int i = 0 ; i < numJoinpoint ; i ++)
+            {
+               assertEquals(values[0], method.invoke(o, values));
+            }
+         }
+      }
+   }
+
+   private void getFieldsAndMethods(ClassInfo[] classInfos)
+   {
+      TypeInfo[] params = new TypeInfo[] {PrimitiveInfo.INT};
+      for (int i = 0 ; i < numGetMembers ; i++)
+      {
+         for (int c = 0 ; c < classInfos.length ; c++)
+         {
+            for (int f = 0 ; f < numFields ; f++)
+               assertNotNull(classInfos[c].getDeclaredField(fieldName(f)));
+            
+            for (int m = 0 ; m < numFields ; m++)
+               assertNotNull(classInfos[c].getDeclaredMethod(methodName(m), params));
+         }
+      }
+   }
+   
+   private ClassInfo[] setupClassInfos(Class<?>[] classes) throws Throwable
+   {
+      ClassInfo[] classInfos = new ClassInfo[classes.length];
+      
+      for (int i = 0 ; i < classInfos.length ; i++)
+         classInfos[i] = (ClassInfo)factory.getTypeInfo(classes[i]);
+      return classInfos;
+   }
+   
+   private Class<?>[] setupClasses() throws Exception
+   {
+      Class<?>[] createdClasses = new Class[numClasses];
+      for (int i = 0 ; i < numClasses ; i++)
+      {
+         createdClasses[i] = createClass(i);
+      }
+      return createdClasses;
+   }
+   
+   private Class<?> createClass(int num) throws Exception
+   {
+      CtClass clazz = ClassPool.getDefault().makeClass("org.jboss.test.benchmark.Test" + num);
+      
+      for (int i = 0 ; i < numFields ; i++)
+      {
+         CtField field = new CtField(CtPrimitiveType.intType, fieldName(i), clazz);
+         field.setModifiers(Modifier.PUBLIC);
+         clazz.addField(field);
+      }
+      
+      for (int i = 0 ; i < numFields ; i++)
+      {
+         CtMethod method = new CtMethod(CtPrimitiveType.intType, methodName(i), new CtClass[] {CtPrimitiveType.intType}, clazz);
+         method.setModifiers(Modifier.PUBLIC);
+         method.setBody("{return $1;}");
+         clazz.addMethod(method);
+      }
+      
+      return clazz.toClass();
+   }
+   
+   private String fieldName(int num)
+   {
+      return "field" + num;
+   }
+   
+   private String methodName(int num)
+   {
+      return "method" + num;
+   }
+   
+   private long outputTime(long start, String msg)
+   {
+      long end = System.currentTimeMillis();
+      System.out.println(msg + " " + (end - start) + "ms");
+      return end;
+   }
+}




More information about the jboss-cvs-commits mailing list