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;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 import org.openjdk.jmh.annotations.Scope;
  30 import org.openjdk.jmh.annotations.Setup;
  31 import org.openjdk.jmh.annotations.State;
  32 import org.openjdk.jmh.infra.Blackhole;
  33 
  34 import java.util.concurrent.TimeUnit;
  35 
  36 /**
  37  * Benchmark measuring java.lang.Class.newInstance speed.
  38  */
  39 @BenchmarkMode(Mode.AverageTime)
  40 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  41 @State(Scope.Thread)
  42 public class NewInstance {
  43 
  44     public Class<?>[] samePublicClasses;
  45     public Class<?>[] differentPublicClasses;
  46     public Class<?>[] differentPublicClassesConstant;
  47     public Class<?>[] sameProtectedClasses;
  48     public Class<?>[] differentProtectedClasses;
  49 
  50     @Setup
  51     public void setup() {
  52         samePublicClasses = new Class<?>[]{Apub.class, Apub.class, Apub.class};
  53         differentPublicClasses = new Class<?>[]{Apub.class, Bpub.class, Cpub.class};
  54         differentPublicClassesConstant = new Class<?>[]{Apub.class, Bpub.class, Cpub.class};
  55         sameProtectedClasses = new Class<?>[]{Apro.class, Apro.class, Apro.class};
  56         differentProtectedClasses = new Class<?>[]{Apro.class, Bpro.class, Cpro.class};
  57     }
  58 
  59     /**
  60      * Performs Class.newInstance on the same class over and over again. That it is the same class is not provable at
  61      * compile time. The class is protected.
  62      */
  63     @Benchmark
  64     public void threeSameProtected(Blackhole bh) throws IllegalAccessException, InstantiationException {
  65         for (Class<?> cl : sameProtectedClasses) {
  66             bh.consume(cl.newInstance());
  67         }
  68     }
  69 
  70     /**
  71      * Performs Class.newInstance on three different classes, just allocating one instance of one class at a time. The
  72      * classes are all protected.
  73      */
  74     @Benchmark
  75     public void threeDifferentProtected(Blackhole bh) throws IllegalAccessException, InstantiationException {
  76         for (Class<?> cl : differentProtectedClasses) {
  77             bh.consume(cl.newInstance());
  78         }
  79     }
  80 
  81     /**
  82      * Performs Class.newInstance on the same class over and over again. That it is the same class is not provable at
  83      * compile time. The class is public.
  84      */
  85     @Benchmark
  86     public void threeSamePublic(Blackhole bh) throws IllegalAccessException, InstantiationException {
  87         for (Class<?> cl : samePublicClasses) {
  88             bh.consume(cl.newInstance());
  89         }
  90     }
  91 
  92     /**
  93      * Performs Class.newInstance on three different classes, just allocating one instance of one class at a time. The
  94      * classes are all public.
  95      */
  96     @Benchmark
  97     public void threeDifferentPublic(Blackhole bh) throws IllegalAccessException, InstantiationException {
  98         for (Class<?> cl : differentPublicClasses) {
  99             bh.consume(cl.newInstance());
 100         }
 101     }
 102 
 103     /**
 104      * Performs Class.newInstance on three different classes, just allocating one instance of one class at a time. The
 105      * classes are all public.
 106      */
 107     @Benchmark
 108     public void threeDifferentPublicConstant(Blackhole bh) throws IllegalAccessException, InstantiationException {
 109         bh.consume(Apub.class.newInstance());
 110         bh.consume(Bpub.class.newInstance());
 111         bh.consume(Cpub.class.newInstance());
 112     }
 113 
 114     @Benchmark
 115     public void threeDifferentPublicFinal(Blackhole bh) throws IllegalAccessException, InstantiationException {
 116         for (Class<?> cl : differentPublicClassesConstant) {
 117             bh.consume(cl.newInstance());
 118         }
 119     }
 120 
 121     /* Protected test classes */
 122     static class Apro {}
 123     static class Bpro {}
 124     static class Cpro {}
 125 
 126     /* Public test classes */
 127     public static class Apub {}
 128     public static class Bpub {}
 129     public static class Cpub {}
 130 
 131 }