1/* 2 * Copyright (C) 2011 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 17package com.android.rs.image; 18 19import android.os.Bundle; 20import android.os.Environment; 21import android.app.Instrumentation; 22import android.content.Context; 23import android.content.Intent; 24import android.net.Uri; 25import android.test.ActivityInstrumentationTestCase2; 26import android.test.suitebuilder.annotation.LargeTest; 27import android.util.Log; 28 29import java.io.BufferedWriter; 30import java.io.File; 31import java.io.FileWriter; 32import java.io.IOException; 33 34/** 35 * ImageProcessing benchmark test. 36 * To run the test, please use command 37 * 38 * adb shell am instrument -w com.android.rs.image/.ImageProcessingTestRunner 39 * 40 */ 41public class ImageProcessingTest extends ActivityInstrumentationTestCase2<ImageProcessingActivity> { 42 private final String TAG = "ImageProcessingTest"; 43 private final String RESULT_FILE = "image_processing_result.txt"; 44 private int ITERATION = 5; 45 private ImageProcessingActivity mAct; 46 47 public ImageProcessingTest() { 48 super(ImageProcessingActivity.class); 49 } 50 51 @Override 52 public void setUp() throws Exception { 53 super.setUp(); 54 mAct = getActivity(); 55 } 56 57 @Override 58 public void tearDown() throws Exception { 59 super.tearDown(); 60 } 61 62 /** 63 * ImageProcessing benchmark test 64 */ 65 @LargeTest 66 public void testImageProcessingBench() { 67 long t = 0; 68 long sum = 0; 69 // write result into a file 70 File externalStorage = Environment.getExternalStorageDirectory(); 71 if (!externalStorage.canWrite()) { 72 Log.v(TAG, "sdcard is not writable"); 73 return; 74 } 75 File resultFile = new File(externalStorage, RESULT_FILE); 76 resultFile.setWritable(true, false); 77 try { 78 BufferedWriter rsWriter = new BufferedWriter(new FileWriter(resultFile)); 79 Log.v(TAG, "Saved results in: " + resultFile.getAbsolutePath()); 80 for (int i = 0; i < ITERATION; i++ ) { 81 t = (long)mAct.getBenchmark(); 82 sum += t; 83 rsWriter.write("Renderscript frame time core: " + t + " ms\n"); 84 Log.v(TAG, "RenderScript framew time core: " + t + " ms"); 85 } 86 long avgValue = sum/ITERATION; 87 rsWriter.write("Average frame time: " + avgValue + " ms\n"); 88 Log.v(TAG, "Average frame time: " + avgValue + " ms"); 89 rsWriter.close(); 90 } catch (IOException e) { 91 Log.v(TAG, "Unable to write result file " + e.getMessage()); 92 } 93 } 94} 95