Home | History | Annotate | Download | only in utils
      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 
     17 package android.perftests.utils;
     18 
     19 import android.support.test.InstrumentationRegistry;
     20 
     21 import org.junit.rules.TestRule;
     22 import org.junit.runner.Description;
     23 import org.junit.runners.model.Statement;
     24 
     25 /**
     26  * Use this rule to make sure we report the status after the test success.
     27  *
     28  * <code>
     29  *
     30  * @Rule public PerfManualStatusReporter mPerfStatusReporter = new PerfManualStatusReporter();
     31  * @Test public void functionName() {
     32  *     ManualBenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     33  *
     34  *     int[] src = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
     35  *     long elapsedTime = 0;
     36  *     while (state.keepRunning(elapsedTime)) {
     37  *         long startTime = System.nanoTime();
     38  *         int[] dest = new int[src.length];
     39  *         System.arraycopy(src, 0, dest, 0, src.length);
     40  *         elapsedTime = System.nanoTime() - startTime;
     41  *     }
     42  * }
     43  * </code>
     44  *
     45  * When test succeeded, the status report will use the key as
     46  * "functionName_*"
     47  */
     48 
     49 public class PerfManualStatusReporter implements TestRule {
     50     private final ManualBenchmarkState mState;
     51 
     52     public PerfManualStatusReporter() {
     53         mState = new ManualBenchmarkState();
     54     }
     55 
     56     public ManualBenchmarkState getBenchmarkState() {
     57         return mState;
     58     }
     59 
     60     @Override
     61     public Statement apply(Statement base, Description description) {
     62         return new Statement() {
     63             @Override
     64             public void evaluate() throws Throwable {
     65                 base.evaluate();
     66 
     67                 mState.sendFullStatusReport(InstrumentationRegistry.getInstrumentation(),
     68                         description.getMethodName());
     69             }
     70         };
     71     }
     72 }
     73 
     74