1 /*
   2  * Copyright (c) 2014, 2019, 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.proxy;
  24 
  25 import org.openjdk.jmh.annotations.*;
  26 
  27 import java.lang.reflect.InvocationHandler;
  28 import java.lang.reflect.Proxy;
  29 import java.util.concurrent.Callable;
  30 import java.util.concurrent.TimeUnit;
  31 
  32 @Warmup(iterations = 5)
  33 @Measurement(iterations = 10)
  34 @Fork(value = 1)
  35 @BenchmarkMode(Mode.AverageTime)
  36 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  37 @State(Scope.Thread)
  38 public class ProxyBench {
  39 
  40     /**
  41      * On Dell T7610:
  42      *
  43      * Benchmark w/ -Djdk.proxy.ProxyGenerator.v49=true
  44      * Benchmark                      Mode  Cnt   Score   Error  Units
  45      * ProxyBench.getProxyClass1i     avgt   10  20.472 +/- 0.209  ns/op
  46      * ProxyBench.getProxyClass4i     avgt   10  57.353 +/- 0.461  ns/op
  47      * ProxyBench.newProxyInstance1i  avgt   10  31.459 +/- 0.516  ns/op
  48      * ProxyBench.newProxyInstance4i  avgt   10  66.580 +/- 0.983  ns/op
  49      *
  50      * Benchmark                      Mode  Cnt   Score   Error  Units
  51      * ProxyBench.getProxyClass1i     avgt   10  21.291 +/- 0.475  ns/op
  52      * ProxyBench.getProxyClass4i     avgt   10  61.481 +/- 4.709  ns/op
  53      * ProxyBench.newProxyInstance1i  avgt   10  30.177 +/- 0.761  ns/op
  54      * ProxyBench.newProxyInstance4i  avgt   10  68.302 +/- 1.344  ns/op
  55      */
  56 
  57     interface PkgPrivate1 {
  58         void m1();
  59     }
  60 
  61     interface PkgPrivate2 {
  62         void m2();
  63     }
  64 
  65     static final InvocationHandler handler = (proxy, method, args) -> null;
  66 
  67     static final ClassLoader loader1 = null;
  68     static final Class<?>[] interfaces1 = {Runnable.class};
  69 
  70     static final ClassLoader loader4 = PkgPrivate1.class.getClassLoader();
  71     static final Class<?>[] interfaces4 = {Runnable.class, Callable.class,
  72                                            PkgPrivate1.class, PkgPrivate2.class};
  73 
  74     @Benchmark
  75     public Class<?> getProxyClass1i() {
  76         return Proxy.getProxyClass(loader1, interfaces1);
  77     }
  78 
  79     @Benchmark
  80     public Class<?> getProxyClass4i() {
  81         return Proxy.getProxyClass(loader4, interfaces4);
  82     }
  83 
  84     @Benchmark
  85     public Object newProxyInstance1i() {
  86         return Proxy.newProxyInstance(loader1, interfaces1, handler);
  87     }
  88 
  89     @Benchmark
  90     public Object newProxyInstance4i() {
  91         return Proxy.newProxyInstance(loader4, interfaces4, handler);
  92     }
  93 }