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 17package benchmarks.regression; 18 19import com.google.caliper.Runner; 20import com.google.caliper.SimpleBenchmark; 21import java.lang.annotation.Retention; 22import java.lang.annotation.RetentionPolicy; 23import java.lang.reflect.Field; 24import java.lang.reflect.Method; 25 26public class AnnotatedElementBenchmark extends SimpleBenchmark { 27 28 private Class<?> type; 29 private Field field; 30 private Method method; 31 32 @Override 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.getAnnotations(); 145 } 146 } 147 148 149 // the annotated elements 150 151 @Marker 152 public class Type { 153 @Marker public String field; 154 @Marker public void method(@Marker String parameter) {} 155 } 156 157 @Large(a = "on class", b = {"A", "B", "C" }, 158 c = @Small(e="E1", f=1695938256, g=7264081114510713000L), 159 d = { @Small(e="E2", f=1695938256, g=7264081114510713000L) }) 160 public class HasLargeAnnotation {} 161 162 @Small(e="E1", f=1695938256, g=7264081114510713000L) 163 public class HasSmallAnnotation {} 164 165 @Marker 166 public class HasMarkerAnnotation {} 167 168 public class HasNoAnnotations {} 169 170 @Large(a = "on class", b = {"A", "B", "C" }, 171 c = @Small(e="E1", f=1695938256, g=7264081114510713000L), 172 d = { @Small(e="E2", f=1695938256, g=7264081114510713000L) }) 173 @Small(e="E1", f=1695938256, g=7264081114510713000L) 174 @Marker 175 public class HasThreeAnnotations {} 176 177 public class ExtendsHasThreeAnnotations {} 178 179 180 // the annotations 181 182 @Retention(RetentionPolicy.RUNTIME) 183 public @interface Marker {} 184 185 @Retention(RetentionPolicy.RUNTIME) 186 public @interface Large { 187 String a() default ""; 188 String[] b() default {}; 189 Small c() default @Small; 190 Small[] d() default {}; 191 } 192 193 @Retention(RetentionPolicy.RUNTIME) 194 public @interface Small { 195 String e() default ""; 196 int f() default 0; 197 long g() default 0L; 198 } 199 200 public static void main(String[] args) throws Exception { 201 Runner.main(AnnotatedElementBenchmark.class, args); 202 } 203} 204