Home | History | Annotate | Download | only in stress
      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 
     21 import android.app.Instrumentation;
     22 import android.content.Intent;
     23 import android.os.Environment;
     24 import android.test.ActivityInstrumentationTestCase2;
     25 import android.test.suitebuilder.annotation.LargeTest;
     26 import android.util.Log;
     27 import android.view.KeyEvent;
     28 
     29 import java.io.FileWriter;
     30 import java.io.Writer;
     31 import java.io.File;
     32 import java.io.FileWriter;
     33 import java.io.BufferedWriter;
     34 import java.io.IOException;
     35 import java.io.InputStream;
     36 /**
     37  * Junit / Instrumentation test case for camera test
     38  *
     39  * Running the test suite:
     40  *
     41  * adb shell am instrument \
     42  *    -e class com.android.camera.stress.ImageCapture \
     43  *    -w com.android.camera.tests/com.android.camera.CameraStressTestRunner
     44  *
     45  */
     46 
     47 public class ImageCapture extends ActivityInstrumentationTestCase2 <Camera> {
     48     private String TAG = "ImageCapture";
     49     private static final int TOTAL_NUMBER_OF_IMAGECAPTURE = 100;
     50     private static final int TOTAL_NUMBER_OF_VIDEOCAPTURE = 100;
     51     private static final long WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN = 1500;   //1.5 sedconds
     52     private static final long WAIT_FOR_VIDEO_CAPTURE_TO_BE_TAKEN = 20000; //20seconds
     53     private static final long WAIT_FOR_PREVIEW = 1500; //1.5 seconds
     54     private static final long WAIT_FOR_STABLE_STATE = 2000; //2 seconds
     55     private static final int NO_OF_LOOPS_TAKE_MEMORY_SNAPSHOT = 10;
     56 
     57     //the tolerant memory leak
     58     private static final int MAX_ACCEPTED_MEMORY_LEAK_KB = 150;
     59 
     60     private static int mStartMemory = 0;
     61     private static int mEndMemory = 0;
     62     private static int mStartPid = 0;
     63     private static int mEndPid = 0;
     64 
     65     private BufferedWriter mOut;
     66     private FileWriter mfstream;
     67 
     68     public ImageCapture() {
     69         super("com.google.android.camera", Camera.class);
     70     }
     71 
     72     @Override
     73     protected void setUp() throws Exception {
     74         getActivity();
     75         prepareOutputFile();
     76         super.setUp();
     77     }
     78 
     79     @Override
     80     protected void tearDown() throws Exception {
     81         closeOutputFile();
     82         super.tearDown();
     83     }
     84 
     85     private void prepareOutputFile(){
     86       String camera_test_output_file =
     87           Environment.getExternalStorageDirectory().toString() + "/mediaStressOut.txt";
     88         try{
     89             mfstream = new FileWriter(camera_test_output_file, true);
     90             mOut = new BufferedWriter(mfstream);
     91         } catch (Exception e){
     92             assertTrue("ImageCapture open output",false);
     93         }
     94     }
     95 
     96     private void closeOutputFile() {
     97         try {
     98             mOut.write("\n");
     99             mOut.close();
    100             mfstream.close();
    101         } catch (Exception e) {
    102             assertTrue("ImageCapture close output", false);
    103         }
    104     }
    105 
    106     //Write the ps output to the file
    107     public void getMemoryWriteToLog(Writer output) {
    108         String memusage = null;
    109         memusage = captureMediaserverInfo();
    110         Log.v(TAG, memusage);
    111         try {
    112             //Write to file output
    113             output.write(memusage);
    114         } catch (Exception e) {
    115             e.toString();
    116         }
    117     }
    118 
    119     public String captureMediaserverInfo() {
    120         String cm = "ps mediaserver";
    121         String memoryUsage = null;
    122 
    123         int ch;
    124         try {
    125             Process p = Runtime.getRuntime().exec(cm);
    126             InputStream in = p.getInputStream();
    127             StringBuffer sb = new StringBuffer(512);
    128             while ((ch = in.read()) != -1) {
    129                 sb.append((char) ch);
    130             }
    131             memoryUsage = sb.toString();
    132         } catch (IOException e) {
    133             Log.v(TAG, e.toString());
    134         }
    135         String[] poList = memoryUsage.split("\r|\n|\r\n");
    136         String memusage = poList[1].concat("\n");
    137         return memusage;
    138     }
    139 
    140     public int getMediaserverPid(){
    141         String memoryUsage = null;
    142         int pidvalue = 0;
    143         memoryUsage = captureMediaserverInfo();
    144         String[] poList2 = memoryUsage.split("\t|\\s+");
    145         String pid = poList2[1];
    146         pidvalue = Integer.parseInt(pid);
    147         Log.v(TAG, "PID = " + pidvalue);
    148         return pidvalue;
    149     }
    150 
    151     public int getMediaserverVsize(){
    152         String memoryUsage = captureMediaserverInfo();
    153         String[] poList2 = memoryUsage.split("\t|\\s+");
    154         String vsize = poList2[3];
    155         int vsizevalue = Integer.parseInt(vsize);
    156         Log.v(TAG, "VSIZE = " + vsizevalue);
    157         return vsizevalue;
    158     }
    159 
    160     public boolean validateMemoryResult (int startPid, int startMemory, Writer output) throws Exception {
    161         Thread.sleep(20000);
    162         mEndPid = getMediaserverPid();
    163         mEndMemory = getMediaserverVsize();
    164         output.write("Start Memory = " + startMemory + "\n");
    165         output.write("End Memory = " + mEndMemory + "\n");
    166         Log.v(TAG, "End memory :" + mEndMemory);
    167         //Write the total memory different into the output file
    168         output.write("The total diff = " + (mEndMemory - startMemory));
    169         output.write("\n\n");
    170         //mediaserver crash
    171         if (startPid != mEndPid){
    172             output.write("mediaserver died. Test failed\n");
    173             return false;
    174         }
    175         //memory leak greter than the tolerant
    176         if ((mEndMemory - startMemory) > MAX_ACCEPTED_MEMORY_LEAK_KB )
    177             return false;
    178         return true;
    179     }
    180 
    181     @LargeTest
    182     public void testImageCapture() {
    183         int total_num_of_images = CameraStressTestRunner.mImageIterations;
    184 
    185         //TODO(yslau): Need to integrate the outoput with the central dashboard,
    186         //write to a txt file as a temp solution
    187         String camera_mem_out =
    188             Environment.getExternalStorageDirectory().toString() + "/ImageCaptureMemOut.txt";
    189         boolean memoryResult = false;
    190         Instrumentation inst = getInstrumentation();
    191         File imageCaptureMemFile = new File(camera_mem_out);
    192 
    193         mStartPid = getMediaserverPid();
    194         mStartMemory = getMediaserverVsize();
    195         Log.v(TAG, "start memory : " + mStartMemory);
    196 
    197         try {
    198             Writer output = new BufferedWriter(new FileWriter(imageCaptureMemFile, true));
    199             output.write("Camera Image capture\n");
    200             output.write("No of loops : " + total_num_of_images + "\n");
    201             getMemoryWriteToLog(output);
    202 
    203             mOut.write("Camera Image Capture\n");
    204             mOut.write("No of loops :" + total_num_of_images + "\n");
    205             mOut.write("loop: ");
    206 
    207             for (int i = 0; i < total_num_of_images; i++) {
    208                 Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);
    209                 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
    210                 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
    211                 Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);
    212                 if (( i % NO_OF_LOOPS_TAKE_MEMORY_SNAPSHOT) == 0){
    213                     Log.v(TAG, "value of i :" + i);
    214                     getMemoryWriteToLog(output);
    215                 }
    216                 //Check if the mediaserver died, if so, exit the test
    217                 if (Camera.mMediaServerDied){
    218                     mOut.write("\nmedia server died\n");
    219                     mOut.flush();
    220                     output.close();
    221                     Camera.mMediaServerDied = false;
    222                     assertTrue("Camera Image Capture", false);
    223                 }
    224                 mOut.write(" ," + i);
    225                 mOut.flush();
    226             }
    227             Thread.sleep(WAIT_FOR_STABLE_STATE);
    228             memoryResult = validateMemoryResult(mStartPid, mStartMemory, output);
    229             Log.v(TAG, "End memory : " + getMediaserverVsize());
    230             output.close();
    231             assertTrue("Camera image capture memory test", memoryResult);
    232         } catch (Exception e) {
    233             Log.v(TAG, e.toString());
    234             assertTrue("testImageCapture", false);
    235         }
    236         assertTrue("testImageCapture", true);
    237     }
    238 
    239     @LargeTest
    240     public void testVideoCapture() {
    241         //TODO(yslau): Need to integrate the output with the central dashboard,
    242         //write to a txt file as a temp solution
    243         String camera_mem_out =
    244             Environment.getExternalStorageDirectory().toString() + "/ImageCaptureMemOut.txt";
    245         boolean memoryResult = false;
    246         int total_num_of_videos = CameraStressTestRunner.mVideoIterations;
    247         int video_duration = CameraStressTestRunner.mVideoDuration;
    248 
    249         Instrumentation inst = getInstrumentation();
    250         File imageCaptureMemFile = new File(camera_mem_out);
    251         mStartPid = getMediaserverPid();
    252         mStartMemory = getMediaserverVsize();
    253         Log.v(TAG, "start memory : " + mStartMemory);
    254 
    255         try {
    256             Writer output = new BufferedWriter(new FileWriter(imageCaptureMemFile, true));
    257             output.write("Camera Video capture\n");
    258             output.write("No of loops : " + total_num_of_videos + "\n");
    259             getMemoryWriteToLog(output);
    260             mOut.write("Camera Video Capture\n");
    261             mOut.write("No of loops :" + total_num_of_videos + "\n");
    262             mOut.write("loop: ");
    263             // Switch to the video mode
    264             Intent intent = new Intent();
    265             intent.setClassName("com.google.android.camera",
    266                     "com.android.camera.VideoCamera");
    267             getActivity().startActivity(intent);
    268             for (int i = 0; i < total_num_of_videos; i++) {
    269                 Thread.sleep(WAIT_FOR_PREVIEW);
    270                 // record a video
    271                 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
    272                 Thread.sleep(video_duration);
    273                 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
    274                 Thread.sleep(WAIT_FOR_PREVIEW);
    275                 mOut.write(" ," + i);
    276                 mOut.flush();
    277             }
    278             Thread.sleep(WAIT_FOR_STABLE_STATE);
    279             memoryResult = validateMemoryResult(mStartPid, mStartMemory, output);
    280             Log.v(TAG, "End memory : " + getMediaserverVsize());
    281             output.close();
    282             assertTrue("Camera video capture memory test", memoryResult);
    283         } catch (Exception e) {
    284             Log.v(TAG, e.toString());
    285             fail("Fails to capture video");
    286         }
    287     }
    288 
    289 }
    290