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.util.Log;
23 import android.view.KeyEvent;
24 
25 import com.android.camera.CameraActivity;
26 
27 import java.io.BufferedWriter;
28 import java.io.File;
29 import java.io.FileWriter;
30 import java.io.FilenameFilter;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 
34 /**
35  * Junit / Instrumentation test case for measuring camera shot to shot latency
36  */
37 public class ShotToShotLatency extends ActivityInstrumentationTestCase2<CameraActivity> {
38     private String TAG = "ShotToShotLatency";
39     private static final int TOTAL_NUMBER_OF_SNAPSHOTS = 250;
40     private static final long SNAPSHOT_WAIT = 1000;
41     private static final String CAMERA_TEST_OUTPUT_FILE =
42             Environment.getExternalStorageDirectory().toString() + "/mediaStressOut.txt";
43     private static final String CAMERA_IMAGE_DIRECTORY =
44             Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/";
45 
ShotToShotLatency()46     public ShotToShotLatency() {
47         super(CameraActivity.class);
48     }
49 
50     @Override
setUp()51     protected void setUp() throws Exception {
52         getActivity();
53         super.setUp();
54     }
55 
56     @Override
tearDown()57     protected void tearDown() throws Exception {
58         super.tearDown();
59     }
60 
cleanupLatencyImages()61     private void cleanupLatencyImages() {
62         try {
63             File sdcard = new File(CAMERA_IMAGE_DIRECTORY);
64             File[] pics = null;
65             FilenameFilter filter = new FilenameFilter() {
66                 public boolean accept(File dir, String name) {
67                     return name.endsWith(".jpg");
68                 }
69             };
70             pics = sdcard.listFiles(filter);
71             for (File f : pics) {
72                 f.delete();
73             }
74         } catch (SecurityException e) {
75             Log.e(TAG, "Security manager access violation: " + e.toString());
76         }
77     }
78 
sleep(long time)79     private void sleep(long time) {
80         try {
81             Thread.sleep(time);
82         } catch (InterruptedException e) {
83             Log.e(TAG, "Sleep InterruptedException " + e.toString());
84         }
85     }
86 
testShotToShotLatency()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