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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.openjdk.bench.java.lang.invoke;
  26 
  27 
  28 import org.openjdk.jmh.annotations.Benchmark;
  29 import org.openjdk.jmh.annotations.BenchmarkMode;
  30 import org.openjdk.jmh.annotations.Mode;
  31 import org.openjdk.jmh.annotations.OutputTimeUnit;
  32 import org.openjdk.jmh.annotations.Scope;
  33 import org.openjdk.jmh.annotations.State;
  34 
  35 import java.lang.invoke.MethodHandle;
  36 import java.lang.invoke.MethodHandles;
  37 import java.lang.invoke.MethodType;
  38 import java.util.concurrent.TimeUnit;
  39 
  40 /**
  41  * Benchmark assesses MethodHandle.lookup() performance
  42  */
  43 @BenchmarkMode(Mode.AverageTime)
  44 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  45 @State(Scope.Thread)
  46 public class LookupDefaultFind {
  47 
  48     /*
  49         Implementation notes:
  50             - does not test findSpecial, since the privileges of this Bench is not enough to access private members
  51      */
  52 
  53     @Benchmark
  54     public MethodHandle testConstructor() throws Exception {
  55         return MethodHandles.lookup().findConstructor(Victim.class, MethodType.methodType(void.class));
  56     }
  57 
  58     @Benchmark
  59     public MethodHandle testGetter() throws Exception {
  60         return MethodHandles.lookup().findGetter(Victim.class, "field", int.class);
  61     }
  62 
  63     @Benchmark
  64     public MethodHandle testSetter() throws Exception {
  65         return MethodHandles.lookup().findSetter(Victim.class, "field", int.class);
  66     }
  67 
  68     @Benchmark
  69     public MethodHandle testStatic() throws Exception {
  70         return MethodHandles.lookup().findStatic(Victim.class, "staticWork", MethodType.methodType(int.class));
  71     }
  72 
  73     @Benchmark
  74     public MethodHandle testStaticGetter() throws Exception {
  75         return MethodHandles.lookup().findStaticGetter(Victim.class, "staticField", int.class);
  76     }
  77 
  78     @Benchmark
  79     public MethodHandle testStaticSetter() throws Exception {
  80         return MethodHandles.lookup().findStaticSetter(Victim.class, "staticField", int.class);
  81     }
  82 
  83     @Benchmark
  84     public MethodHandle testVirtual() throws Exception {
  85         return MethodHandles.lookup().findVirtual(Victim.class, "virtualWork", MethodType.methodType(int.class));
  86     }
  87 
  88     public static class Victim {
  89 
  90         public static int staticField;
  91         public int field;
  92 
  93         public Victim() {
  94             // do nothing
  95         }
  96 
  97         public int virtualWork() {
  98             return 1;
  99         }
 100 
 101         public static int staticWork() {
 102             return 1;
 103         }
 104 
 105         private int specialWork() {
 106             return 1;
 107         }
 108 
 109     }
 110 
 111 }