Home | History | Annotate | Download | only in stress
      1 /*
      2  * Copyright (C) 2012 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 com.android.camera.stress;
     18 
     19 import android.app.Instrumentation;
     20 import android.os.Environment;
     21 import android.test.ActivityInstrumentationTestCase2;
     22 import android.test.suitebuilder.annotation.LargeTest;
     23 import android.util.Log;
     24 import android.view.KeyEvent;
     25 import com.android.camera.CameraActivity;
     26 import java.io.BufferedWriter;
     27 import java.io.File;
     28 import java.io.FilenameFilter;
     29 import java.io.FileWriter;
     30 import java.io.IOException;
     31 import java.util.ArrayList;
     32 
     33 /**
     34  * Junit / Instrumentation test case for measuring camera shot to shot latency
     35  */
     36 public class ShotToShotLatency extends ActivityInstrumentationTestCase2<CameraActivity> {
     37     private String TAG = "ShotToShotLatency";
     38     private static final int TOTAL_NUMBER_OF_SNAPSHOTS = 250;
     39     private static final long SNAPSHOT_WAIT = 1000;
     40     private static final String CAMERA_TEST_OUTPUT_FILE =
     41             Environment.getExternalStorageDirectory().toString() + "/mediaStressOut.txt";
     42     private static final String CAMERA_IMAGE_DIRECTORY =
     43             Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/";
     44 
     45     public ShotToShotLatency() {
     46         super(CameraActivity.class);
     47     }
     48 
     49     @Override
     50     protected void setUp() throws Exception {
     51         getActivity();
     52         super.setUp();
     53     }
     54 
     55     @Override
     56     protected void tearDown() throws Exception {
     57         super.tearDown();
     58     }
     59 
     60     private void cleanupLatencyImages() {
     61         try {
     62             File sdcard = new File(CAMERA_IMAGE_DIRECTORY);
     63             File[] pics = null;
     64             FilenameFilter filter = new FilenameFilter() {
     65                 public boolean accept(File dir, String name) {
     66                     return name.endsWith(".jpg");
     67                 }
     68             };
     69             pics = sdcard.listFiles(filter);
     70             for (File f : pics) {
     71                 f.delete();
     72             }
     73         } catch (SecurityException e) {
     74             Log.e(TAG, "Security manager access violation: " + e.toString());
     75         }
     76     }
     77 
     78     private void sleep(long time) {
     79         try {
     80             Thread.sleep(time);
     81         } catch (InterruptedException e) {
     82             Log.e(TAG, "Sleep InterruptedException " + e.toString());
     83         }
     84     }
     85 
     86     @LargeTest
     87     public void testShotToShotLatency() {
     88         long sigmaOfDiffFromMeanSquared = 0;
     89         double mean = 0;
     90         double standardDeviation = 0;
     91         ArrayList<Long> captureTimes = new ArrayList<Long>();
     92         ArrayList<Long> latencyTimes = new ArrayList<Long>();
     93 
     94         Log.v(TAG, "start testShotToShotLatency test");
     95         Instrumentation inst = getInstrumentation();
     96 
     97         // Generate data points
     98         for (int i = 0; i < TOTAL_NUMBER_OF_SNAPSHOTS; i++) {
     99             inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
    100             sleep(SNAPSHOT_WAIT);
    101             CameraActivity c = getActivity();
    102             if (c.getCaptureStartTime() > 0) {
    103                 captureTimes.add(c.getCaptureStartTime());
    104             }
    105         }
    106 
    107         // Calculate latencies
    108         for (int j = 1; j < captureTimes.size(); j++) {
    109             latencyTimes.add(captureTimes.get(j) - captureTimes.get(j - 1));
    110         }
    111 
    112         // Crunch numbers
    113         for (long dataPoint : latencyTimes) {
    114             mean += (double) dataPoint;
    115         }
    116         mean /= latencyTimes.size();
    117 
    118         for (long dataPoint : latencyTimes) {
    119             sigmaOfDiffFromMeanSquared += (dataPoint - mean) * (dataPoint - mean);
    120         }
    121         standardDeviation = Math.sqrt(sigmaOfDiffFromMeanSquared / latencyTimes.size());
    122 
    123         // Report statistics
    124         File outFile = new File(CAMERA_TEST_OUTPUT_FILE);
    125         BufferedWriter output = null;
    126         try {
    127             output = new BufferedWriter(new FileWriter(outFile, true));
    128             output.write("Shot to shot latency - mean: " + mean + "\n");
    129             output.write("Shot to shot latency - standard deviation: " + standardDeviation + "\n");
    130             cleanupLatencyImages();
    131         } catch (IOException e) {
    132             Log.e(TAG, "testShotToShotLatency IOException writing to log " + e.toString());
    133         } finally {
    134             try {
    135                 if (output != null) {
    136                     output.close();
    137                 }
    138             } catch (IOException e) {
    139                 Log.e(TAG, "Error closing file: " + e.toString());
    140             }
    141         }
    142     }
    143 }
    144