1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.bench.java.lang.reflect;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.CompilerControl;
  28 import org.openjdk.jmh.annotations.Mode;
  29 import org.openjdk.jmh.annotations.OutputTimeUnit;
  30 import org.openjdk.jmh.annotations.Scope;
  31 import org.openjdk.jmh.annotations.Setup;
  32 import org.openjdk.jmh.annotations.State;
  33 
  34 import java.lang.reflect.InvocationTargetException;
  35 import java.lang.reflect.Method;
  36 import java.util.concurrent.TimeUnit;
  37 
  38 /**
  39  * Benchmark measuring java.lang.reflect.Method.invoke speed.
  40  * <p/>
  41  * TODO: Add tests for virtual and interface methods.
  42  */
  43 @BenchmarkMode(Mode.AverageTime)
  44 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  45 @State(Scope.Thread)
  46 public class MethodInvoke {
  47 
  48     private Method staticMeth_0;
  49     private Method staticMeth_6ref;
  50     private Method staticMeth_6prim;
  51 
  52     private Object[] args_0;
  53     private Object[] args_6ref;
  54     private Object[] args_6prim;
  55 
  56     @Setup
  57     public void setup() {
  58         args_0 = new Object[]{};
  59         args_6ref = new Object[]{ new Object(), new Object(),
  60                 new Object(), new Object(), new Object(), new Object()};
  61         args_6prim = new Object[]{
  62                 new Integer(1), new Long(5L),
  63                 new Double(5.6d), new Float(23.11f),
  64                 Boolean.TRUE, new Character('d')
  65         };
  66 
  67         staticMeth_0 = getMethodWithName("staticMethodWithoutParams");
  68         staticMeth_6ref = getMethodWithName("staticMethodWithSixObjectParams");
  69         staticMeth_6prim = getMethodWithName("staticMethodWithSixPrimitiveParams");
  70     }
  71 
  72     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
  73     public static void staticMethodWithoutParams() {
  74         // intentionally left blank
  75     }
  76 
  77     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
  78     public static void staticMethodWithSixObjectParams(Object o1, Object o2, Object o3, Object o4, Object o5, Object o6) {
  79         // intentionally left blank
  80     }
  81 
  82     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
  83     public static void staticMethodWithSixPrimitiveParams(int i, long j, double d, float f, boolean z, char c) {
  84         // intentionally left blank
  85     }
  86 
  87     /* inner method to get the method to invoke. */
  88 
  89     private static Method getMethodWithName(String methodName) {
  90         Method[] methodArray = MethodInvoke.class.getMethods();
  91         for (Method m : methodArray) {
  92             if (m.getName().equals(methodName)) {
  93                 return m;
  94             }
  95         }
  96         return null;
  97     }
  98 
  99     @Benchmark
 100     public void invokeWithoutParams() throws InvocationTargetException, IllegalAccessException {
 101         staticMeth_0.invoke(null, args_0);
 102     }
 103 
 104     @Benchmark
 105     public void invokeWithSixObjectParams() throws Exception {
 106         staticMeth_6ref.invoke(null, args_6ref);
 107     }
 108 
 109     @Benchmark
 110     public void invokeWithSixPrimitiveParams() throws Exception {
 111         staticMeth_6prim.invoke(null, args_6prim);
 112     }
 113 
 114 }