Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2014 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.sample.cts;
     17 
     18 import android.sample.SampleDeviceActivity;
     19 import android.test.ActivityInstrumentationTestCase2;
     20 
     21 import com.android.compatibility.common.util.DeviceReportLog;
     22 import com.android.compatibility.common.util.MeasureRun;
     23 import com.android.compatibility.common.util.MeasureTime;
     24 import com.android.compatibility.common.util.ResultType;
     25 import com.android.compatibility.common.util.ResultUnit;
     26 import com.android.compatibility.common.util.Stat;
     27 
     28 import java.util.Arrays;
     29 import java.util.Random;
     30 
     31 /**
     32  * A simple compatibility test which includes results in the report.
     33  *
     34  * This test measures the time taken to run a workload and adds in the report.
     35  */
     36 public class SampleDeviceResultTest extends ActivityInstrumentationTestCase2<SampleDeviceActivity> {
     37 
     38     /**
     39      * Name of the report log to store test metrics.
     40      */
     41     private static final String REPORT_LOG_NAME = "CtsSampleDeviceTestCases";
     42 
     43     /**
     44      * The number of times to repeat the test.
     45      */
     46     private static final int REPEAT = 5;
     47 
     48     /**
     49      * A {@link Random} to generate random integers to test the sort.
     50      */
     51     private static final Random random = new Random(12345);
     52 
     53     /**
     54      * Constructor which passes the class of the activity to be instrumented.
     55      */
     56     public SampleDeviceResultTest() {
     57         super(SampleDeviceActivity.class);
     58     }
     59 
     60     /**
     61      * Measures the time taken to sort an array.
     62      */
     63     public void testSort() throws Exception {
     64         // MeasureTime runs the workload N times and records the time taken by each run.
     65         double[] result = MeasureTime.measure(REPEAT, new MeasureRun() {
     66             /**
     67              * The size of the array to sort.
     68              */
     69             private static final int ARRAY_SIZE = 100000;
     70             private int[] array;
     71             @Override
     72             public void prepare(int i) throws Exception {
     73                 array = createArray(ARRAY_SIZE);
     74             }
     75             @Override
     76             public void run(int i) throws Exception {
     77                 Arrays.sort(array);
     78                 assertTrue("Array not sorted", isSorted(array));
     79             }
     80         });
     81         // Compute the stats.
     82         Stat.StatResult stat = Stat.getStat(result);
     83         // Create a new report to hold the metrics.
     84         String streamName = "test_sort";
     85         DeviceReportLog reportLog = new DeviceReportLog(REPORT_LOG_NAME, streamName);
     86         // Add the results to the report.
     87         reportLog.addValues("times", result, ResultType.LOWER_BETTER, ResultUnit.MS);
     88         reportLog.addValue("min", stat.mMin, ResultType.LOWER_BETTER, ResultUnit.MS);
     89         reportLog.addValue("max", stat.mMax, ResultType.LOWER_BETTER, ResultUnit.MS);
     90         // Set a summary.
     91         reportLog.setSummary("average", stat.mAverage, ResultType.LOWER_BETTER, ResultUnit.MS);
     92         // Submit the report to the given instrumentation.
     93         reportLog.submit(getInstrumentation());
     94     }
     95 
     96     /**
     97      * Creates an array filled with random numbers of the given size.
     98      */
     99     private static int[] createArray(int size) {
    100         int[] array = new int[size];
    101         for (int i = 0; i < size; i++) {
    102             array[i] = random.nextInt();
    103         }
    104         return array;
    105     }
    106 
    107     /**
    108      * Tests an array is sorted.
    109      */
    110     private static boolean isSorted(int[] array) {
    111         int len = array.length;
    112         for (int i = 0, j = 1; j < len; i++, j++) {
    113             if (array[i] > array[j]) {
    114                 return false;
    115             }
    116         }
    117         return true;
    118     }
    119 }
    120