Home | History | Annotate | Download | only in regression
      1 /*
      2  * Copyright (C) 2011 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package benchmarks.regression;
     18 
     19 import com.google.caliper.BeforeExperiment;
     20 import java.lang.annotation.Retention;
     21 import java.lang.annotation.RetentionPolicy;
     22 import java.lang.reflect.Field;
     23 import java.lang.reflect.Method;
     24 
     25 public class AnnotatedElementBenchmark {
     26 
     27     private Class<?> type;
     28     private Field field;
     29     private Method method;
     30 
     31     @BeforeExperiment
     32     protected void setUp() throws Exception {
     33         type = Type.class;
     34         field = Type.class.getField("field");
     35         method = Type.class.getMethod("method", String.class);
     36     }
     37 
     38 
     39     // get annotations by member type and method
     40 
     41     public void timeGetTypeAnnotations(int reps) {
     42         for (int i = 0; i < reps; i++) {
     43             type.getAnnotations();
     44         }
     45     }
     46 
     47     public void timeGetFieldAnnotations(int reps) {
     48         for (int i = 0; i < reps; i++) {
     49             field.getAnnotations();
     50         }
     51     }
     52 
     53     public void timeGetMethodAnnotations(int reps) {
     54         for (int i = 0; i < reps; i++) {
     55             method.getAnnotations();
     56         }
     57     }
     58 
     59     public void timeGetParameterAnnotations(int reps) {
     60         for (int i = 0; i < reps; i++) {
     61             method.getParameterAnnotations();
     62         }
     63     }
     64 
     65     public void timeGetTypeAnnotation(int reps) {
     66         for (int i = 0; i < reps; i++) {
     67             type.getAnnotation(Marker.class);
     68         }
     69     }
     70 
     71     public void timeGetFieldAnnotation(int reps) {
     72         for (int i = 0; i < reps; i++) {
     73             field.getAnnotation(Marker.class);
     74         }
     75     }
     76 
     77     public void timeGetMethodAnnotation(int reps) {
     78         for (int i = 0; i < reps; i++) {
     79             method.getAnnotation(Marker.class);
     80         }
     81     }
     82 
     83     public void timeIsTypeAnnotationPresent(int reps) {
     84         for (int i = 0; i < reps; i++) {
     85             type.isAnnotationPresent(Marker.class);
     86         }
     87     }
     88 
     89     public void timeIsFieldAnnotationPresent(int reps) {
     90         for (int i = 0; i < reps; i++) {
     91             field.isAnnotationPresent(Marker.class);
     92         }
     93     }
     94 
     95     public void timeIsMethodAnnotationPresent(int reps) {
     96         for (int i = 0; i < reps; i++) {
     97             method.isAnnotationPresent(Marker.class);
     98         }
     99     }
    100 
    101     // get annotations by result size
    102 
    103     public void timeGetAllReturnsLargeAnnotation(int reps) {
    104         for (int i = 0; i < reps; i++) {
    105             HasLargeAnnotation.class.getAnnotations();
    106         }
    107     }
    108 
    109     public void timeGetAllReturnsSmallAnnotation(int reps) {
    110         for (int i = 0; i < reps; i++) {
    111             HasSmallAnnotation.class.getAnnotations();
    112         }
    113     }
    114 
    115     public void timeGetAllReturnsMarkerAnnotation(int reps) {
    116         for (int i = 0; i < reps; i++) {
    117             HasMarkerAnnotation.class.getAnnotations();
    118         }
    119     }
    120 
    121     public void timeGetAllReturnsNoAnnotation(int reps) {
    122         for (int i = 0; i < reps; i++) {
    123             HasNoAnnotations.class.getAnnotations();
    124         }
    125     }
    126 
    127     public void timeGetAllReturnsThreeAnnotations(int reps) {
    128         for (int i = 0; i < reps; i++) {
    129             HasThreeAnnotations.class.getAnnotations();
    130         }
    131     }
    132 
    133 
    134     // get annotations with inheritance
    135 
    136     public void timeGetAnnotationsOnSubclass(int reps) {
    137         for (int i = 0; i < reps; i++) {
    138             ExtendsHasThreeAnnotations.class.getAnnotations();
    139         }
    140     }
    141 
    142     public void timeGetDeclaredAnnotationsOnSubclass(int reps) {
    143         for (int i = 0; i < reps; i++) {
    144             ExtendsHasThreeAnnotations.class.getDeclaredAnnotations();
    145         }
    146     }
    147 
    148 
    149     // get annotations with enclosing / inner classes
    150 
    151     public void timeGetDeclaredClasses(int reps) {
    152         for (int i = 0; i < reps; i++) {
    153             AnnotatedElementBenchmark.class.getDeclaredClasses();
    154         }
    155     }
    156 
    157     public void timeGetDeclaringClass(int reps) {
    158         for (int i = 0; i < reps; i++) {
    159             HasSmallAnnotation.class.getDeclaringClass();
    160         }
    161     }
    162 
    163     public void timeGetEnclosingClass(int reps) {
    164         Object anonymousClass = new Object() {};
    165         for (int i = 0; i < reps; i++) {
    166             anonymousClass.getClass().getEnclosingClass();
    167         }
    168     }
    169 
    170     public void timeGetEnclosingConstructor(int reps) {
    171         Object anonymousClass = new Object() {};
    172         for (int i = 0; i < reps; i++) {
    173             anonymousClass.getClass().getEnclosingConstructor();
    174         }
    175     }
    176 
    177     public void timeGetEnclosingMethod(int reps) {
    178         Object anonymousClass = new Object() {};
    179         for (int i = 0; i < reps; i++) {
    180             anonymousClass.getClass().getEnclosingMethod();
    181         }
    182     }
    183 
    184     public void timeGetModifiers(int reps) {
    185         for (int i = 0; i < reps; i++) {
    186             HasSmallAnnotation.class.getModifiers();
    187         }
    188     }
    189 
    190     public void timeGetSimpleName(int reps) {
    191         for (int i = 0; i < reps; i++) {
    192             HasSmallAnnotation.class.getSimpleName();
    193         }
    194     }
    195 
    196     public void timeIsAnonymousClass(int reps) {
    197         Object anonymousClass = new Object() {};
    198         for (int i = 0; i < reps; i++) {
    199             anonymousClass.getClass().isAnonymousClass();
    200         }
    201     }
    202 
    203     public void timeIsLocalClass(int reps) {
    204         for (int i = 0; i < reps; i++) {
    205             HasSmallAnnotation.class.isLocalClass();
    206         }
    207     }
    208 
    209 
    210     // the annotated elements
    211 
    212     @Marker
    213     public class Type {
    214         @Marker public String field;
    215         @Marker public void method(@Marker String parameter) {}
    216     }
    217 
    218     @Large(a = "on class", b = {"A", "B", "C" },
    219             c = @Small(e="E1", f=1695938256, g=7264081114510713000L),
    220             d = { @Small(e="E2", f=1695938256, g=7264081114510713000L) })
    221     public class HasLargeAnnotation {}
    222 
    223     @Small(e="E1", f=1695938256, g=7264081114510713000L)
    224     public class HasSmallAnnotation {}
    225 
    226     @Marker
    227     public class HasMarkerAnnotation {}
    228 
    229     public class HasNoAnnotations {}
    230 
    231     @Large(a = "on class", b = {"A", "B", "C" },
    232             c = @Small(e="E1", f=1695938256, g=7264081114510713000L),
    233             d = { @Small(e="E2", f=1695938256, g=7264081114510713000L) })
    234     @Small(e="E1", f=1695938256, g=7264081114510713000L)
    235     @Marker
    236     public class HasThreeAnnotations {}
    237 
    238     public class ExtendsHasThreeAnnotations extends HasThreeAnnotations {}
    239 
    240 
    241     // the annotations
    242 
    243     @Retention(RetentionPolicy.RUNTIME)
    244     public @interface Marker {}
    245 
    246     @Retention(RetentionPolicy.RUNTIME)
    247     public @interface Large {
    248         String a() default "";
    249         String[] b() default {};
    250         Small c() default @Small;
    251         Small[] d() default {};
    252     }
    253 
    254     @Retention(RetentionPolicy.RUNTIME)
    255     public @interface Small {
    256         String e() default "";
    257         int f() default 0;
    258         long g() default 0L;
    259     }
    260 }
    261