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     public void testShotToShotLatency() {
     87         long sigmaOfDiffFromMeanSquared = 0;
     88         double mean = 0;
     89         double standardDeviation = 0;
     90         ArrayList<Long> captureTimes = new ArrayList<Long>();
     91         ArrayList<Long> latencyTimes = new ArrayList<Long>();
     92 
     93         Log.v(TAG, "start testShotToShotLatency test");
     94         Instrumentation inst = getInstrumentation();
     95 
     96         // Generate data points
     97         for (int i = 0; i < TOTAL_NUMBER_OF_SNAPSHOTS; i++) {
     98             inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
     99             sleep(SNAPSHOT_WAIT);
    100             CameraActivity c = getActivity();
    101             if (c.getCaptureStartTime() > 0) {
    102                 captureTimes.add(c.getCaptureStartTime());
    103             }
    104         }
    105 
    106         // Calculate latencies
    107         for (int j = 1; j < captureTimes.size(); j++) {
    108             latencyTimes.add(captureTimes.get(j) - captureTimes.get(j - 1));
    109         }
    110 
    111         // Crunch numbers
    112         for (long dataPoint : latencyTimes) {
    113             mean += (double) dataPoint;
    114         }
    115         mean /= latencyTimes.size();
    116 
    117         for (long dataPoint : latencyTimes) {
    118             sigmaOfDiffFromMeanSquared += (dataPoint - mean) * (dataPoint - mean);
    119         }
    120         standardDeviation = Math.sqrt(sigmaOfDiffFromMeanSquared / latencyTimes.size());
    121 
    122         // Report statistics
    123         File outFile = new File(CAMERA_TEST_OUTPUT_FILE);
    124         BufferedWriter output = null;
    125         try {
    126             output = new BufferedWriter(new FileWriter(outFile, true));
    127             output.write("Shot to shot latency - mean: " + mean + "\n");
    128             output.write("Shot to shot latency - standard deviation: " + standardDeviation + "\n");
    129             cleanupLatencyImages();
    130         } catch (IOException e) {
    131             Log.e(TAG, "testShotToShotLatency IOException writing to log " + e.toString());
    132         } finally {
    133             try {
    134                 if (output != null) {
    135                     output.close();
    136                 }
    137             } catch (IOException e) {
    138                 Log.e(TAG, "Error closing file: " + e.toString());
    139             }
    140         }
    141     }
    142 }
    143