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 17 package com.android.rs.imagejb; 18 19 20 import android.os.Bundle; 21 import android.util.Log; 22 import android.app.Activity; 23 24 import com.android.rs.imagejb.IPTestListJB.TestName; 25 26 import android.test.ActivityInstrumentationTestCase2; 27 import android.test.suitebuilder.annotation.MediumTest; 28 29 import android.os.Environment; 30 import java.io.BufferedWriter; 31 import java.io.File; 32 import java.io.FileWriter; 33 import java.io.IOException; 34 35 /** 36 * ImageProcessing benchmark test. 37 * To run the test, please use command 38 * 39 * adb shell am instrument -w com.android.rs.imagejb/android.support.test.runner.AndroidJUnitRunner 40 * 41 */ 42 public class ImageProcessingTest extends ActivityInstrumentationTestCase2<ImageProcessingActivityJB> { 43 private final String TAG = "ImageProcessingTest"; 44 // Only run 1 iteration now to fit the MediumTest time requirement. 45 // One iteration means running the tests continuous for 1s. 46 private ImageProcessingActivityJB mActivity; 47 ImageProcessingTest()48 public ImageProcessingTest() { 49 super(ImageProcessingActivityJB.class); 50 } 51 52 53 // Initialize the parameter for ImageProcessingActivityJB. prepareTest()54 protected void prepareTest() { 55 mActivity = getActivity(); 56 mActivity.prepareInstrumentationTest(); 57 } 58 59 @Override setUp()60 public void setUp() throws Exception { 61 super.setUp(); 62 prepareTest(); 63 setActivityInitialTouchMode(false); 64 } 65 66 @Override tearDown()67 public void tearDown() throws Exception { 68 if (mActivity.mProcessor != null) { 69 mActivity.mProcessor.exit(); 70 mActivity.mProcessor = null; 71 } 72 super.tearDown(); 73 } 74 75 class TestAction implements Runnable { 76 private TestName mTestName; 77 private Result mResult; TestAction(TestName testName)78 public TestAction(TestName testName) { 79 mTestName = testName; 80 } run()81 public void run() { 82 mResult = mActivity.mProcessor.getInstrumentationResult(mTestName); 83 Log.v(TAG, "Benchmark for test \"" + mTestName.toString() + "\" is: " + 84 mResult.getAvg() * 1000.f); 85 synchronized(this) { 86 this.notify(); 87 } 88 } getBenchmark()89 public Result getBenchmark() { 90 return mResult; 91 } 92 } 93 94 // Set the benchmark thread to run on ui thread 95 // Synchronized the thread such that the test will wait for the benchmark thread to finish runOnUiThread(Runnable action)96 public void runOnUiThread(Runnable action) { 97 synchronized(action) { 98 mActivity.runOnUiThread(action); 99 try { 100 action.wait(); 101 } catch (InterruptedException e) { 102 Log.v(TAG, "waiting for action running on UI thread is interrupted: " + 103 e.toString()); 104 } 105 } 106 } 107 108 // TODO: Report more info: mean, median, std, etc. runTest(TestAction ta, String testName)109 public void runTest(TestAction ta, String testName) { 110 runOnUiThread(ta); 111 Result times = ta.getBenchmark(); 112 113 // post result to INSTRUMENTATION_STATUS 114 Bundle results = new Bundle(); 115 results.putFloat(testName + "_avg", times.getAvg() * 1000.0f); // ms 116 results.putFloat(testName + "_stdevp", times.getStdevp() * 1000.0f); // ms 117 results.putFloat(testName + "_stdcoef", times.getStdCoef() * 100.0f); // % 118 getInstrumentation().sendStatus(Activity.RESULT_OK, results); 119 120 // save the runtime distribution to a file on the sdcard so a script can plot it 121 writeResults("rsTimes/", testName + "_DATA.txt", times); 122 } 123 writeResults(String directory, String filename, Result times)124 private void writeResults(String directory, String filename, Result times) { 125 // write result into a file 126 File externalStorage = Environment.getExternalStorageDirectory(); 127 if (!externalStorage.canWrite()) { 128 Log.v(TAG, "sdcard is not writable"); 129 return; 130 } 131 File resultDirectory = new File(externalStorage, directory); 132 resultDirectory.mkdirs(); 133 File resultFile = new File(externalStorage, directory + filename); 134 resultFile.setWritable(true, false); 135 try { 136 BufferedWriter rsWriter = new BufferedWriter(new FileWriter(resultFile)); 137 Log.v(TAG, "Saved results in: " + resultFile.getAbsolutePath()); 138 139 float[] datapoints = times.getTimes(); 140 for (int i = 0; i < times.getIterations(); i++) { 141 rsWriter.write(String.format("%d %f\n", i, datapoints[i] * 1000.0)); 142 } 143 rsWriter.close(); 144 } catch (IOException e) { 145 Log.v(TAG, "Unable to write result file " + e.getMessage()); 146 } 147 } 148 149 // Test case 0: Levels Vec3 Relaxed 150 @MediumTest testLevelsVec3Relaxed()151 public void testLevelsVec3Relaxed() { 152 TestAction ta = new TestAction(TestName.LEVELS_VEC3_RELAXED); 153 runTest(ta, TestName.LEVELS_VEC3_RELAXED.name()); 154 } 155 156 // Test case 1: Levels Vec4 Relaxed 157 @MediumTest testLevelsVec4Relaxed()158 public void testLevelsVec4Relaxed() { 159 TestAction ta = new TestAction(TestName.LEVELS_VEC4_RELAXED); 160 runTest(ta, TestName.LEVELS_VEC4_RELAXED.name()); 161 } 162 163 // Test case 2: Levels Vec3 Full 164 @MediumTest testLevelsVec3Full()165 public void testLevelsVec3Full() { 166 TestAction ta = new TestAction(TestName.LEVELS_VEC3_FULL); 167 runTest(ta, TestName.LEVELS_VEC3_FULL.name()); 168 } 169 170 // Test case 3: Levels Vec4 Full 171 @MediumTest testLevelsVec4Full()172 public void testLevelsVec4Full() { 173 TestAction ta = new TestAction(TestName.LEVELS_VEC4_FULL); 174 runTest(ta, TestName.LEVELS_VEC4_FULL.name()); 175 } 176 177 // Test case 4: Blur Radius 25 178 @MediumTest testBlurRadius25()179 public void testBlurRadius25() { 180 TestAction ta = new TestAction(TestName.BLUR_RADIUS_25); 181 runTest(ta, TestName.BLUR_RADIUS_25.name()); 182 } 183 184 // Test case 5: Intrinsic Blur Radius 25 185 @MediumTest testIntrinsicBlurRadius25()186 public void testIntrinsicBlurRadius25() { 187 TestAction ta = new TestAction(TestName.INTRINSIC_BLUR_RADIUS_25); 188 runTest(ta, TestName.INTRINSIC_BLUR_RADIUS_25.name()); 189 } 190 191 // Test case 6: Greyscale 192 @MediumTest testGreyscale()193 public void testGreyscale() { 194 TestAction ta = new TestAction(TestName.GREYSCALE); 195 runTest(ta, TestName.GREYSCALE.name()); 196 } 197 198 // Test case 7: Grain 199 @MediumTest testGrain()200 public void testGrain() { 201 TestAction ta = new TestAction(TestName.GRAIN); 202 runTest(ta, TestName.GRAIN.name()); 203 } 204 205 // Test case 8: Fisheye Full 206 @MediumTest testFisheyeFull()207 public void testFisheyeFull() { 208 TestAction ta = new TestAction(TestName.FISHEYE_FULL); 209 runTest(ta, TestName.FISHEYE_FULL.name()); 210 } 211 212 // Test case 9: Fisheye Relaxed 213 @MediumTest testFishEyeRelaxed()214 public void testFishEyeRelaxed() { 215 TestAction ta = new TestAction(TestName.FISHEYE_RELAXED); 216 runTest(ta, TestName.FISHEYE_RELAXED.name()); 217 } 218 219 // Test case 10: Fisheye Approximate Full 220 @MediumTest testFisheyeApproximateFull()221 public void testFisheyeApproximateFull() { 222 TestAction ta = new TestAction(TestName.FISHEYE_APPROXIMATE_FULL); 223 runTest(ta, TestName.FISHEYE_APPROXIMATE_FULL.name()); 224 } 225 226 // Test case 11: Fisheye Approximate Relaxed 227 @MediumTest testFisheyeApproximateRelaxed()228 public void testFisheyeApproximateRelaxed() { 229 TestAction ta = new TestAction(TestName.FISHEYE_APPROXIMATE_RELAXED); 230 runTest(ta, TestName.FISHEYE_APPROXIMATE_RELAXED.name()); 231 } 232 233 // Test case 12: Vignette Full 234 @MediumTest testVignetteFull()235 public void testVignetteFull() { 236 TestAction ta = new TestAction(TestName.VIGNETTE_FULL); 237 runTest(ta, TestName.VIGNETTE_FULL.name()); 238 } 239 240 // Test case 13: Vignette Relaxed 241 @MediumTest testVignetteRelaxed()242 public void testVignetteRelaxed() { 243 TestAction ta = new TestAction(TestName.VIGNETTE_RELAXED); 244 runTest(ta, TestName.VIGNETTE_RELAXED.name()); 245 } 246 247 // Test case 14: Vignette Approximate Full 248 @MediumTest testVignetteApproximateFull()249 public void testVignetteApproximateFull() { 250 TestAction ta = new TestAction(TestName.VIGNETTE_APPROXIMATE_FULL); 251 runTest(ta, TestName.VIGNETTE_APPROXIMATE_FULL.name()); 252 } 253 254 // Test case 15: Vignette Approximate Relaxed 255 @MediumTest testVignetteApproximateRelaxed()256 public void testVignetteApproximateRelaxed() { 257 TestAction ta = new TestAction(TestName.VIGNETTE_APPROXIMATE_RELAXED); 258 runTest(ta, TestName.VIGNETTE_APPROXIMATE_RELAXED.name()); 259 } 260 261 // Test case 16: Group Test (emulated) 262 @MediumTest testGroupTestEmulated()263 public void testGroupTestEmulated() { 264 TestAction ta = new TestAction(TestName.GROUP_TEST_EMULATED); 265 runTest(ta, TestName.GROUP_TEST_EMULATED.name()); 266 } 267 268 // Test case 17: Group Test (native) 269 @MediumTest testGroupTestNative()270 public void testGroupTestNative() { 271 TestAction ta = new TestAction(TestName.GROUP_TEST_NATIVE); 272 runTest(ta, TestName.GROUP_TEST_NATIVE.name()); 273 } 274 275 // Test case 18: Convolve 3x3 276 @MediumTest testConvolve3x3()277 public void testConvolve3x3() { 278 TestAction ta = new TestAction(TestName.CONVOLVE_3X3); 279 runTest(ta, TestName.CONVOLVE_3X3.name()); 280 } 281 282 // Test case 19: Intrinsics Convolve 3x3 283 @MediumTest testIntrinsicsConvolve3x3()284 public void testIntrinsicsConvolve3x3() { 285 TestAction ta = new TestAction(TestName.INTRINSICS_CONVOLVE_3X3); 286 runTest(ta, TestName.INTRINSICS_CONVOLVE_3X3.name()); 287 } 288 289 // Test case 20: ColorMatrix 290 @MediumTest testColorMatrix()291 public void testColorMatrix() { 292 TestAction ta = new TestAction(TestName.COLOR_MATRIX); 293 runTest(ta, TestName.COLOR_MATRIX.name()); 294 } 295 296 // Test case 21: Intrinsics ColorMatrix 297 @MediumTest testIntrinsicsColorMatrix()298 public void testIntrinsicsColorMatrix() { 299 TestAction ta = new TestAction(TestName.INTRINSICS_COLOR_MATRIX); 300 runTest(ta, TestName.INTRINSICS_COLOR_MATRIX.name()); 301 } 302 303 // Test case 22: Intrinsics ColorMatrix Grey 304 @MediumTest testIntrinsicsColorMatrixGrey()305 public void testIntrinsicsColorMatrixGrey() { 306 TestAction ta = new TestAction(TestName.INTRINSICS_COLOR_MATRIX_GREY); 307 runTest(ta, TestName.INTRINSICS_COLOR_MATRIX_GREY.name()); 308 } 309 310 // Test case 23: Copy 311 @MediumTest testCopy()312 public void testCopy() { 313 TestAction ta = new TestAction(TestName.COPY); 314 runTest(ta, TestName.COPY.name()); 315 } 316 317 // Test case 24: CrossProcess (using LUT) 318 @MediumTest testCrossProcessUsingLUT()319 public void testCrossProcessUsingLUT() { 320 TestAction ta = new TestAction(TestName.CROSS_PROCESS_USING_LUT); 321 runTest(ta, TestName.CROSS_PROCESS_USING_LUT.name()); 322 } 323 324 // Test case 25: Convolve 5x5 325 @MediumTest testConvolve5x5()326 public void testConvolve5x5() { 327 TestAction ta = new TestAction(TestName.CONVOLVE_5X5); 328 runTest(ta, TestName.CONVOLVE_5X5.name()); 329 } 330 331 // Test case 26: Intrinsics Convolve 5x5 332 @MediumTest testIntrinsicsConvolve5x5()333 public void testIntrinsicsConvolve5x5() { 334 TestAction ta = new TestAction(TestName.INTRINSICS_CONVOLVE_5X5); 335 runTest(ta, TestName.INTRINSICS_CONVOLVE_5X5.name()); 336 } 337 338 // Test case 27: Mandelbrot 339 @MediumTest testMandelbrot()340 public void testMandelbrot() { 341 TestAction ta = new TestAction(TestName.MANDELBROT_FLOAT); 342 runTest(ta, TestName.MANDELBROT_FLOAT.name()); 343 } 344 345 // Test case 28: Intrinsics Blend 346 @MediumTest testIntrinsicsBlend()347 public void testIntrinsicsBlend() { 348 TestAction ta = new TestAction(TestName.INTRINSICS_BLEND); 349 runTest(ta, TestName.INTRINSICS_BLEND.name()); 350 } 351 352 // Test case 29: Intrinsics Blur 25 uchar 353 @MediumTest testIntrinsicsBlur25G()354 public void testIntrinsicsBlur25G() { 355 TestAction ta = new TestAction(TestName.INTRINSICS_BLUR_25G); 356 runTest(ta, TestName.INTRINSICS_BLUR_25G.name()); 357 } 358 359 // Test case 30: Vibrance 360 @MediumTest testVibrance()361 public void testVibrance() { 362 TestAction ta = new TestAction(TestName.VIBRANCE); 363 runTest(ta, TestName.VIBRANCE.name()); 364 } 365 366 // Test case 31: BWFilter 367 @MediumTest testBWFilter()368 public void testBWFilter() { 369 TestAction ta = new TestAction(TestName.BW_FILTER); 370 runTest(ta, TestName.BW_FILTER.name()); 371 } 372 373 // Test case 32: Shadows 374 @MediumTest testShadows()375 public void testShadows() { 376 TestAction ta = new TestAction(TestName.SHADOWS); 377 runTest(ta, TestName.SHADOWS.name()); 378 } 379 380 // Test case 33: Contrast 381 @MediumTest testContrast()382 public void testContrast() { 383 TestAction ta = new TestAction(TestName.CONTRAST); 384 runTest(ta, TestName.CONTRAST.name()); 385 } 386 387 // Test case 34: Exposure 388 @MediumTest testExposure()389 public void testExposure(){ 390 TestAction ta = new TestAction(TestName.EXPOSURE); 391 runTest(ta, TestName.EXPOSURE.name()); 392 } 393 394 // Test case 35: White Balance 395 @MediumTest testWhiteBalance()396 public void testWhiteBalance() { 397 TestAction ta = new TestAction(TestName.WHITE_BALANCE); 398 runTest(ta, TestName.WHITE_BALANCE.name()); 399 } 400 401 // Test case 36: Color Cube 402 @MediumTest testColorCube()403 public void testColorCube() { 404 TestAction ta = new TestAction(TestName.COLOR_CUBE); 405 runTest(ta, TestName.COLOR_CUBE.name()); 406 } 407 408 // Test case 37: Color Cube (3D Intrinsic) 409 @MediumTest testColorCube3DIntrinsic()410 public void testColorCube3DIntrinsic() { 411 TestAction ta = new TestAction(TestName.COLOR_CUBE_3D_INTRINSIC); 412 runTest(ta, TestName.COLOR_CUBE_3D_INTRINSIC.name()); 413 } 414 415 // Test case 38: Artistic 1 416 @MediumTest testArtistic1()417 public void testArtistic1() { 418 TestAction ta = new TestAction(TestName.ARTISTIC1); 419 runTest(ta, TestName.ARTISTIC1.name()); 420 } 421 422 // Test case 39: Resize BiCubic Script 423 @MediumTest testResizeBiCubicScript()424 public void testResizeBiCubicScript() { 425 TestAction ta = new TestAction(TestName.RESIZE_BI_SCRIPT); 426 runTest(ta, TestName.RESIZE_BI_SCRIPT.name()); 427 } 428 429 // Test case 40: Resize BiCubic Intrinsic 430 @MediumTest testResizeBiCubicIntrinsic()431 public void testResizeBiCubicIntrinsic() { 432 TestAction ta = new TestAction(TestName.RESIZE_BI_INTRINSIC); 433 runTest(ta, TestName.RESIZE_BI_INTRINSIC.name()); 434 } 435 436 // Test case 41: Posterize with invoke 437 @MediumTest testPosterizeInvoke()438 public void testPosterizeInvoke() { 439 TestAction ta = new TestAction(TestName.POSTERIZE_INVOKE); 440 runTest(ta, TestName.POSTERIZE_INVOKE.name()); 441 } 442 443 // Test case 42: Posterize with set 444 @MediumTest testPosterizeSet()445 public void testPosterizeSet() { 446 TestAction ta = new TestAction(TestName.POSTERIZE_SET); 447 runTest(ta, TestName.POSTERIZE_SET.name()); 448 } 449 450 // Test case 43 Histogram intrinsic 451 @MediumTest testHistogramIntrinsic()452 public void testHistogramIntrinsic() { 453 TestAction ta = new TestAction(TestName.HISTOGRAM_INTRINSIC); 454 runTest(ta, TestName.HISTOGRAM_INTRINSIC.name()); 455 } 456 457 // Test case 44 Histogram script 458 @MediumTest testHistogramScript()459 public void testHistogramScript() { 460 TestAction ta = new TestAction(TestName.HISTOGRAM_SCRIPT); 461 runTest(ta, TestName.HISTOGRAM_SCRIPT.name()); 462 } 463 464 // Test case 45: Mandelbrot fp64 465 @MediumTest testMandelbrotfp64()466 public void testMandelbrotfp64() { 467 TestAction ta = new TestAction(TestName.MANDELBROT_DOUBLE); 468 runTest(ta, TestName.MANDELBROT_DOUBLE.name()); 469 } 470 471 // Test case 46: Blur Radius 25 Half Precision 472 @MediumTest testBlurRadius25Half()473 public void testBlurRadius25Half() { 474 TestAction ta = new TestAction(TestName.BLUR_RADIUS_25_HALF); 475 runTest(ta, TestName.BLUR_RADIUS_25_HALF.name()); 476 } 477 } 478