1 /* 2 * Copyright (C) 2016 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.documentsui; 18 19 import android.app.Activity; 20 import android.app.ActivityManager; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 import android.os.Bundle; 26 import android.provider.DocumentsContract; 27 import android.support.test.uiautomator.UiDevice; 28 import android.test.InstrumentationTestCase; 29 import android.test.suitebuilder.annotation.LargeTest; 30 import android.util.Log; 31 32 import java.util.Arrays; 33 import java.util.List; 34 import java.util.concurrent.CountDownLatch; 35 36 @LargeTest 37 public class FilesAppPerfTest extends InstrumentationTestCase { 38 39 // Keys used to report metrics to APCT. 40 private static final String KEY_FILES_COLD_START_PERFORMANCE_MEDIAN = 41 "files-cold-start-performance-median"; 42 private static final String KEY_FILES_WARM_START_PERFORMANCE_MEDIAN = 43 "files-warm-start-performance-median"; 44 45 private static final String TARGET_PACKAGE = "com.android.documentsui"; 46 47 private static final int NUM_MEASUREMENTS = 10; 48 49 private LauncherActivity mActivity; 50 private UiDevice mDevice; 51 52 @Override setUp()53 public void setUp() { 54 mDevice = UiDevice.getInstance(getInstrumentation()); 55 } 56 testFilesColdStartPerformance()57 public void testFilesColdStartPerformance() throws Exception { 58 runFilesStartPerformanceTest(true); 59 } 60 testFilesWarmStartPerformance()61 public void testFilesWarmStartPerformance() throws Exception { 62 runFilesStartPerformanceTest(false); 63 } 64 runFilesStartPerformanceTest(boolean cold)65 public void runFilesStartPerformanceTest(boolean cold) throws Exception { 66 long[] measurements = new long[NUM_MEASUREMENTS]; 67 for (int i = 0; i < NUM_MEASUREMENTS; i++) { 68 if (cold) { 69 // Kill all providers, as well as DocumentsUI to measure a cold start. 70 killProviders(); 71 mDevice.executeShellCommand("am force-stop " + TARGET_PACKAGE); 72 } 73 mDevice.waitForIdle(); 74 75 LauncherActivity.testCaseLatch = new CountDownLatch(1); 76 mActivity = launchActivity(getInstrumentation().getTargetContext().getPackageName(), 77 LauncherActivity.class, null); 78 LauncherActivity.testCaseLatch.await(); 79 measurements[i] = LauncherActivity.measurement; 80 } 81 82 reportMetrics(cold ? KEY_FILES_COLD_START_PERFORMANCE_MEDIAN 83 : KEY_FILES_WARM_START_PERFORMANCE_MEDIAN, measurements); 84 } 85 reportMetrics(String key, long[] measurements)86 private void reportMetrics(String key, long[] measurements) { 87 final Bundle status = new Bundle(); 88 Arrays.sort(measurements); 89 final long median = measurements[NUM_MEASUREMENTS / 2 - 1]; 90 status.putDouble(key, median); 91 92 getInstrumentation().sendStatus(Activity.RESULT_OK, status); 93 } 94 killProviders()95 private void killProviders() throws Exception { 96 final Context context = getInstrumentation().getContext(); 97 final PackageManager pm = context.getPackageManager(); 98 final ActivityManager am = (ActivityManager) context.getSystemService( 99 Context.ACTIVITY_SERVICE); 100 final Intent intent = new Intent(DocumentsContract.PROVIDER_INTERFACE); 101 final List<ResolveInfo> providers = pm.queryIntentContentProviders(intent, 0); 102 for (ResolveInfo info : providers) { 103 final String packageName = info.providerInfo.packageName; 104 am.killBackgroundProcesses(packageName); 105 } 106 } 107 } 108