1 /*
2  * Copyright (C) 2022 The Android Open Source Project
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 android.libcore.regression;
18 
19 import android.perftests.utils.BenchmarkState;
20 import android.perftests.utils.PerfStatusReporter;
21 import android.test.suitebuilder.annotation.LargeTest;
22 
23 import androidx.test.runner.AndroidJUnit4;
24 
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 import java.lang.reflect.Field;
33 import java.lang.reflect.Method;
34 
35 @RunWith(AndroidJUnit4.class)
36 @LargeTest
37 public class AnnotatedElementPerfTest {
38     @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
39 
40     private Class<?> mType;
41     private Field mField;
42     private Method mMethod;
43 
44     @Before
setUp()45     public void setUp() throws Exception {
46         mType = Type.class;
47         mField = Type.class.getField("field");
48         mMethod = Type.class.getMethod("method", String.class);
49     }
50 
51     // get annotations by member type and method
52 
53     @Test
timeGetTypeAnnotations()54     public void timeGetTypeAnnotations() {
55         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
56         while (state.keepRunning()) {
57             mType.getAnnotations();
58         }
59     }
60 
61     @Test
timeGetFieldAnnotations()62     public void timeGetFieldAnnotations() {
63         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
64         while (state.keepRunning()) {
65             mField.getAnnotations();
66         }
67     }
68 
69     @Test
timeGetMethodAnnotations()70     public void timeGetMethodAnnotations() {
71         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
72         while (state.keepRunning()) {
73             mMethod.getAnnotations();
74         }
75     }
76 
77     @Test
timeGetParameterAnnotations()78     public void timeGetParameterAnnotations() {
79         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
80         while (state.keepRunning()) {
81             mMethod.getParameterAnnotations();
82         }
83     }
84 
85     @Test
timeGetTypeAnnotation()86     public void timeGetTypeAnnotation() {
87         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
88         while (state.keepRunning()) {
89             mType.getAnnotation(Marker.class);
90         }
91     }
92 
93     @Test
timeGetFieldAnnotation()94     public void timeGetFieldAnnotation() {
95         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
96         while (state.keepRunning()) {
97             mField.getAnnotation(Marker.class);
98         }
99     }
100 
101     @Test
timeGetMethodAnnotation()102     public void timeGetMethodAnnotation() {
103         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
104         while (state.keepRunning()) {
105             mMethod.getAnnotation(Marker.class);
106         }
107     }
108 
109     @Test
timeIsTypeAnnotationPresent()110     public void timeIsTypeAnnotationPresent() {
111         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
112         while (state.keepRunning()) {
113             mType.isAnnotationPresent(Marker.class);
114         }
115     }
116 
117     @Test
timeIsFieldAnnotationPresent()118     public void timeIsFieldAnnotationPresent() {
119         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
120         while (state.keepRunning()) {
121             mField.isAnnotationPresent(Marker.class);
122         }
123     }
124 
125     @Test
timeIsMethodAnnotationPresent()126     public void timeIsMethodAnnotationPresent() {
127         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
128         while (state.keepRunning()) {
129             mMethod.isAnnotationPresent(Marker.class);
130         }
131     }
132 
133     // get annotations by result size
134 
135     @Test
timeGetAllReturnsLargeAnnotation()136     public void timeGetAllReturnsLargeAnnotation() {
137         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
138         while (state.keepRunning()) {
139             HasLargeAnnotation.class.getAnnotations();
140         }
141     }
142 
143     @Test
timeGetAllReturnsSmallAnnotation()144     public void timeGetAllReturnsSmallAnnotation() {
145         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
146         while (state.keepRunning()) {
147             HasSmallAnnotation.class.getAnnotations();
148         }
149     }
150 
151     @Test
timeGetAllReturnsMarkerAnnotation()152     public void timeGetAllReturnsMarkerAnnotation() {
153         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
154         while (state.keepRunning()) {
155             HasMarkerAnnotation.class.getAnnotations();
156         }
157     }
158 
159     @Test
timeGetAllReturnsNoAnnotation()160     public void timeGetAllReturnsNoAnnotation() {
161         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
162         while (state.keepRunning()) {
163             HasNoAnnotations.class.getAnnotations();
164         }
165     }
166 
167     @Test
timeGetAllReturnsThreeAnnotations()168     public void timeGetAllReturnsThreeAnnotations() {
169         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
170         while (state.keepRunning()) {
171             HasThreeAnnotations.class.getAnnotations();
172         }
173     }
174 
175     // get annotations with inheritance
176 
177     @Test
timeGetAnnotationsOnSubclass()178     public void timeGetAnnotationsOnSubclass() {
179         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
180         while (state.keepRunning()) {
181             ExtendsHasThreeAnnotations.class.getAnnotations();
182         }
183     }
184 
185     @Test
timeGetDeclaredAnnotationsOnSubclass()186     public void timeGetDeclaredAnnotationsOnSubclass() {
187         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
188         while (state.keepRunning()) {
189             ExtendsHasThreeAnnotations.class.getDeclaredAnnotations();
190         }
191     }
192 
193     // get annotations with enclosing / inner classes
194 
195     @Test
timeGetDeclaredClasses()196     public void timeGetDeclaredClasses() {
197         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
198         while (state.keepRunning()) {
199             AnnotatedElementPerfTest.class.getDeclaredClasses();
200         }
201     }
202 
203     @Test
timeGetDeclaringClass()204     public void timeGetDeclaringClass() {
205         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
206         while (state.keepRunning()) {
207             HasSmallAnnotation.class.getDeclaringClass();
208         }
209     }
210 
211     @Test
timeGetEnclosingClass()212     public void timeGetEnclosingClass() {
213         Object anonymousClass = new Object() {};
214         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
215         while (state.keepRunning()) {
216             anonymousClass.getClass().getEnclosingClass();
217         }
218     }
219 
220     @Test
timeGetEnclosingConstructor()221     public void timeGetEnclosingConstructor() {
222         Object anonymousClass = new Object() {};
223         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
224         while (state.keepRunning()) {
225             anonymousClass.getClass().getEnclosingConstructor();
226         }
227     }
228 
229     @Test
timeGetEnclosingMethod()230     public void timeGetEnclosingMethod() {
231         Object anonymousClass = new Object() {};
232         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
233         while (state.keepRunning()) {
234             anonymousClass.getClass().getEnclosingMethod();
235         }
236     }
237 
238     @Test
timeGetModifiers()239     public void timeGetModifiers() {
240         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
241         while (state.keepRunning()) {
242             HasSmallAnnotation.class.getModifiers();
243         }
244     }
245 
246     @Test
timeGetSimpleName()247     public void timeGetSimpleName() {
248         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
249         while (state.keepRunning()) {
250             HasSmallAnnotation.class.getSimpleName();
251         }
252     }
253 
254     @Test
timeIsAnonymousClass()255     public void timeIsAnonymousClass() {
256         Object anonymousClass = new Object() {};
257         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
258         while (state.keepRunning()) {
259             anonymousClass.getClass().isAnonymousClass();
260         }
261     }
262 
263     @Test
timeIsLocalClass()264     public void timeIsLocalClass() {
265         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
266         while (state.keepRunning()) {
267             HasSmallAnnotation.class.isLocalClass();
268         }
269     }
270 
271     // the annotated elements
272 
273     @Marker
274     public class Type {
275         @Marker public String field;
276 
277         @Marker
method(@arker String parameter)278         public void method(@Marker String parameter) {}
279     }
280 
281     @Large(
282             a = "on class",
283             b = {"A", "B", "C"},
284             c = @Small(e = "E1", f = 1695938256, g = 7264081114510713000L),
285             d = {@Small(e = "E2", f = 1695938256, g = 7264081114510713000L)})
286     public class HasLargeAnnotation {}
287 
288     @Small(e = "E1", f = 1695938256, g = 7264081114510713000L)
289     public class HasSmallAnnotation {}
290 
291     @Marker
292     public class HasMarkerAnnotation {}
293 
294     public class HasNoAnnotations {}
295 
296     @Large(
297             a = "on class",
298             b = {"A", "B", "C"},
299             c = @Small(e = "E1", f = 1695938256, g = 7264081114510713000L),
300             d = {@Small(e = "E2", f = 1695938256, g = 7264081114510713000L)})
301     @Small(e = "E1", f = 1695938256, g = 7264081114510713000L)
302     @Marker
303     public class HasThreeAnnotations {}
304 
305     public class ExtendsHasThreeAnnotations extends HasThreeAnnotations {}
306 
307     // the annotations
308 
309     @Retention(RetentionPolicy.RUNTIME)
310     public @interface Marker {}
311 
312     @Retention(RetentionPolicy.RUNTIME)
313     public @interface Large {
a()314         String a() default "";
315 
b()316         String[] b() default {};
317 
c()318         Small c() default @Small;
319 
d()320         Small[] d() default {};
321     }
322 
323     @Retention(RetentionPolicy.RUNTIME)
324     public @interface Small {
e()325         String e() default "";
326 
f()327         int f() default 0;
328 
g()329         long g() default 0L;
330     }
331 }
332