2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera.stress;
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;
28 import java.io.FilenameFilter;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.util.ArrayList;
34 * Junit / Instrumentation test case for measuring camera shot to shot latency
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/";
45 public ShotToShotLatency() {
46 super(CameraActivity.class);
50 protected void setUp() throws Exception {
56 protected void tearDown() throws Exception {
60 private void cleanupLatencyImages() {
62 File sdcard = new File(CAMERA_IMAGE_DIRECTORY);
64 FilenameFilter filter = new FilenameFilter() {
65 public boolean accept(File dir, String name) {
66 return name.endsWith(".jpg");
69 pics = sdcard.listFiles(filter);
73 } catch (SecurityException e) {
74 Log.e(TAG, "Security manager access violation: " + e.toString());
78 private void sleep(long time) {
81 } catch (InterruptedException e) {
82 Log.e(TAG, "Sleep InterruptedException " + e.toString());
87 public void testShotToShotLatency() {
88 long sigmaOfDiffFromMeanSquared = 0;
90 double standardDeviation = 0;
91 ArrayList<Long> captureTimes = new ArrayList<Long>();
92 ArrayList<Long> latencyTimes = new ArrayList<Long>();
94 Log.v(TAG, "start testShotToShotLatency test");
95 Instrumentation inst = getInstrumentation();
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());
107 // Calculate latencies
108 for (int j = 1; j < captureTimes.size(); j++) {
109 latencyTimes.add(captureTimes.get(j) - captureTimes.get(j - 1));
113 for (long dataPoint : latencyTimes) {
114 mean += (double) dataPoint;
116 mean /= latencyTimes.size();
118 for (long dataPoint : latencyTimes) {
119 sigmaOfDiffFromMeanSquared += (dataPoint - mean) * (dataPoint - mean);
121 standardDeviation = Math.sqrt(sigmaOfDiffFromMeanSquared / latencyTimes.size());
124 File outFile = new File(CAMERA_TEST_OUTPUT_FILE);
125 BufferedWriter output = null;
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());
135 if (output != null) {
138 } catch (IOException e) {
139 Log.e(TAG, "Error closing file: " + e.toString());