• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 package android.platform.test.microbenchmark;
17 
18 import android.os.Bundle;
19 import android.platform.test.composer.Iterate;
20 import android.platform.test.rule.TracePointRule;
21 import androidx.annotation.VisibleForTesting;
22 import androidx.test.InstrumentationRegistry;
23 
24 import java.lang.annotation.ElementType;
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 import java.lang.annotation.Target;
28 import java.util.List;
29 
30 import org.junit.rules.TestRule;
31 import org.junit.runners.BlockJUnit4ClassRunner;
32 import org.junit.runners.model.InitializationError;
33 import org.junit.runners.model.FrameworkMethod;
34 import org.junit.runners.model.Statement;
35 
36 /**
37  * The {@code Microbenchmark} runner allows you to run test methods repeatedly and with {@link
38  * TightMethodRule}s in order to reliably measure a specific test method in isolation. Samples are
39  * soon to follow.
40  */
41 public class Microbenchmark extends BlockJUnit4ClassRunner {
42     private Bundle mArguments;
43 
44     /**
45      * Called reflectively on classes annotated with {@code @RunWith(Microbenchmark.class)}.
46      */
Microbenchmark(Class<?> klass)47     public Microbenchmark(Class<?> klass) throws InitializationError {
48         this(klass, InstrumentationRegistry.getArguments());
49     }
50 
51     /**
52      * Do not call. Called explicitly from tests to provide an arguments.
53      */
54     @VisibleForTesting
Microbenchmark(Class<?> klass, Bundle arguments)55     Microbenchmark(Class<?> klass, Bundle arguments) throws InitializationError {
56         super(klass);
57         mArguments = arguments;
58     }
59 
60     /**
61      * Returns a {@link Statement} that invokes {@code method} on {@code test}, surrounded by any
62      * explicit or command-line-supplied {@link TightMethodRule}s. This allows for tighter {@link
63      * TestRule}s that live inside {@link Before} and {@link After} statements.
64      */
65     @Override
methodInvoker(FrameworkMethod method, Object test)66     protected Statement methodInvoker(FrameworkMethod method, Object test) {
67         Statement start = super.methodInvoker(method, test);
68         // Wrap the inner-most test method with trace points.
69         start = getTracePointRule().apply(start, describeChild(method));
70         // Invoke special @TightMethodRules that wrap @Test methods.
71         List<TestRule> tightMethodRules =
72                 getTestClass().getAnnotatedFieldValues(test, TightMethodRule.class, TestRule.class);
73         for (TestRule tightMethodRule : tightMethodRules) {
74             start = tightMethodRule.apply(start, describeChild(method));
75         }
76         return start;
77     }
78 
79     @VisibleForTesting
getTracePointRule()80     protected TracePointRule getTracePointRule() {
81         return new TracePointRule();
82     }
83 
84     /**
85      * Returns a list of repeated {@link FrameworkMethod}s to execute.
86      */
87     @Override
getChildren()88     protected List<FrameworkMethod> getChildren() {
89        return new Iterate<FrameworkMethod>().apply(mArguments, super.getChildren());
90     }
91 
92     /**
93      * An annotation for the corresponding tight rules above. These rules are ordered differently
94      * from standard JUnit {@link Rule}s because they live between {@link Before} and {@link After}
95      * methods, instead of wrapping those methods.
96      *
97      * <p>In particular, these serve as a proxy for tight metric collection in microbenchmark-style
98      * tests, where collection is isolated to just the method under test. This is important for when
99      * {@link Before} and {@link After} methods will obscure signal reliability.
100      *
101      * <p> Currently these are only registered from inside a test class as follows, but should soon
102      * be extended for command-line support.
103      *
104      * ```
105      * @RunWith(Microbenchmark.class)
106      * public class TestClass {
107      *     @TightMethodRule
108      *     public ExampleRule exampleRule = new ExampleRule();
109      *
110      *     @Test
111      *     ...
112      * }
113      * ```
114      */
115     @Retention(RetentionPolicy.RUNTIME)
116     @Target({ElementType.FIELD, ElementType.METHOD})
117     public @interface TightMethodRule { }
118 }
119