• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 com.android.camera.Camera;
20 import com.android.camera.VideoCamera;
21 
22 import android.app.Activity;
23 import android.app.Instrumentation;
24 import android.content.Intent;
25 import android.os.Debug;
26 import android.os.Environment;
27 import android.test.InstrumentationTestCase;
28 import android.test.suitebuilder.annotation.LargeTest;
29 import android.util.Log;
30 
31 import java.io.FileWriter;
32 import java.io.BufferedWriter;
33 
34 /**
35  * Test cases to measure the camera and video recorder startup time.
36  */
37 public class CameraStartUp extends InstrumentationTestCase {
38 
39     private static final int TOTAL_NUMBER_OF_STARTUP = 20;
40 
41     private String TAG = "CameraStartUp";
42     private static final String CAMERA_TEST_OUTPUT_FILE =
43             Environment.getExternalStorageDirectory().toString() + "/mediaStressOut.txt";
44     private static int WAIT_TIME_FOR_PREVIEW = 1500; //1.5 second
45 
launchCamera()46     private long launchCamera() {
47         long startupTime = 0;
48         try {
49             Intent intent = new Intent(Intent.ACTION_MAIN);
50             intent.setClass(getInstrumentation().getTargetContext(), Camera.class);
51             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
52             long beforeStart = System.currentTimeMillis();
53             Instrumentation inst = getInstrumentation();
54             Activity cameraActivity = inst.startActivitySync(intent);
55             long cameraStarted = System.currentTimeMillis();
56             cameraActivity.finish();
57             startupTime = cameraStarted - beforeStart;
58             Thread.sleep(1000);
59             Log.v(TAG, "camera startup time: " + startupTime);
60         } catch (Exception e) {
61             Log.v(TAG, "Got exception", e);
62             fail("Fails to get the output file");
63         }
64         return startupTime;
65     }
66 
launchVideo()67     private long launchVideo() {
68         long startupTime = 0;
69 
70         try {
71             Intent intent = new Intent(Intent.ACTION_MAIN);
72             intent.setClass(getInstrumentation().getTargetContext(), VideoCamera.class);
73             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
74             long beforeStart = System.currentTimeMillis();
75             Instrumentation inst = getInstrumentation();
76             Activity recorderActivity = inst.startActivitySync(intent);
77             long cameraStarted = System.currentTimeMillis();
78             recorderActivity.finish();
79             startupTime = cameraStarted - beforeStart;
80             Log.v(TAG, "Video Startup Time = " + startupTime);
81             // wait for 1s to make sure it reach a clean stage
82             Thread.sleep(WAIT_TIME_FOR_PREVIEW);
83             Log.v(TAG, "video startup time: " + startupTime);
84         } catch (Exception e) {
85             Log.v(TAG, "Got exception", e);
86             fail("Fails to launch video output file");
87         }
88         return startupTime;
89     }
90 
writeToOutputFile(long totalStartupTime, String individualStartupTime, boolean firstStartUp, String Type)91     private void writeToOutputFile(long totalStartupTime,
92             String individualStartupTime, boolean firstStartUp, String Type) throws Exception {
93         // TODO (yslau) : Need to integrate the output data with central
94         // dashboard
95         try {
96             FileWriter fstream = null;
97             fstream = new FileWriter(CAMERA_TEST_OUTPUT_FILE, true);
98             BufferedWriter out = new BufferedWriter(fstream);
99             if (firstStartUp) {
100                 out.write("First " + Type + " Startup: " + totalStartupTime + "\n");
101             } else {
102                 long averageStartupTime = totalStartupTime / (TOTAL_NUMBER_OF_STARTUP -1);
103                 out.write(Type + "startup time: " + "\n");
104                 out.write("Number of loop: " + (TOTAL_NUMBER_OF_STARTUP -1)  + "\n");
105                 out.write(individualStartupTime + "\n\n");
106                 out.write(Type + " average startup time: " + averageStartupTime + " ms\n\n");
107             }
108             out.close();
109             fstream.close();
110         } catch (Exception e) {
111             fail("Camera write output to file");
112         }
113     }
114 
115     @LargeTest
testLaunchVideo()116     public void testLaunchVideo() throws Exception {
117         String individualStartupTime;
118         individualStartupTime = "Individual Video Startup Time = ";
119         long totalStartupTime = 0;
120         long startupTime = 0;
121         for (int i = 0; i < TOTAL_NUMBER_OF_STARTUP; i++) {
122             if (i == 0) {
123                 // Capture the first startup time individually
124                 long firstStartUpTime = launchVideo();
125                 writeToOutputFile(firstStartUpTime, "na", true, "Video");
126             } else {
127                 startupTime = launchVideo();
128                 totalStartupTime += startupTime;
129                 individualStartupTime += startupTime + " ,";
130             }
131         }
132         Log.v(TAG, "totalStartupTime =" + totalStartupTime);
133         writeToOutputFile(totalStartupTime, individualStartupTime, false, "Video");
134     }
135 
136     @LargeTest
testLaunchCamera()137     public void testLaunchCamera() throws Exception {
138         String individualStartupTime;
139         individualStartupTime = "Individual Camera Startup Time = ";
140         long totalStartupTime = 0;
141         long startupTime = 0;
142         for (int i = 0; i < TOTAL_NUMBER_OF_STARTUP; i++) {
143             if (i == 0) {
144                 // Capture the first startup time individually
145                 long firstStartUpTime = launchCamera();
146                 writeToOutputFile(firstStartUpTime, "na", true, "Camera");
147             } else {
148                 startupTime = launchCamera();
149                 totalStartupTime += startupTime;
150                 individualStartupTime += startupTime + " ,";
151             }
152         }
153         Log.v(TAG, "totalStartupTime =" + totalStartupTime);
154         writeToOutputFile(totalStartupTime,
155                 individualStartupTime, false, "Camera");
156     }
157 }