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