blob: 996c5a5c558461aa653bc046efd79a95c79bc778 [file] [log] [blame]
Arthur Eubanks20a29572018-01-09 11:40:58 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.frameworks.perftests.am.tests;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.os.IBinder;
Brett Chabot502ec7a2019-03-01 14:43:20 -080024
25import androidx.test.filters.LargeTest;
26import androidx.test.runner.AndroidJUnit4;
Arthur Eubanks20a29572018-01-09 11:40:58 -080027
28import com.android.frameworks.perftests.am.util.Constants;
Arthur Eubanksebd2ea02018-02-13 10:20:39 -080029import com.android.frameworks.perftests.am.util.TargetPackageUtils;
Arthur Eubanks20a29572018-01-09 11:40:58 -080030
31import org.junit.Assert;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34
35@RunWith(AndroidJUnit4.class)
36@LargeTest
37public class ServiceBindPerfTest extends BasePerfTest {
38 /**
39 * Create and return a ServiceConnection that will add the current time with type
40 * Constants.TYPE_SERVICE_CONNECTED.
41 */
42 private ServiceConnection createServiceConnectionReportTime() {
43 return new ServiceConnection() {
44 @Override
45 public void onServiceConnected(ComponentName name, IBinder service) {
46 addReceivedTimeNs(Constants.TYPE_SERVICE_CONNECTED);
47 }
48
49 @Override
50 public void onServiceDisconnected(ComponentName name) {
51 }
52 };
53 }
54
55 /**
56 * Try to bind to the service with the input parameters, throwing a RuntimeException with the
57 * errorMessage on failure.
58 */
59 private void bindService(Intent intent, ServiceConnection serviceConnection, int flags) {
60 final boolean success = mContext.bindService(intent, serviceConnection, flags);
61 Assert.assertTrue("Could not bind to service", success);
62 }
63
64 /**
65 * Benchmark time from Context.bindService() to Service.onBind() when target package is not
66 * running.
67 */
68 @Test
69 public void bindServiceNotRunning() {
70 runPerfFunction(() -> {
71 final Intent intent = createServiceIntent();
72 final ServiceConnection serviceConnection = createServiceConnectionReportTime();
73
74 final long startTimeNs = System.nanoTime();
75 bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
76 try {
77 final long endTimeNs = getReceivedTimeNs(Constants.TYPE_SERVICE_CONNECTED);
78 return endTimeNs - startTimeNs;
79 } finally {
Arthur Eubanksebd2ea02018-02-13 10:20:39 -080080 TargetPackageUtils.unbindFromService(mContext, serviceConnection);
Arthur Eubanks20a29572018-01-09 11:40:58 -080081 }
82 });
83 }
84
85 /**
86 * Benchmark time from Context.bindService() to Service.onBind() when target package is running.
87 */
88 @Test
89 public void bindServiceRunning() {
90 runPerfFunction(() -> {
91 startTargetPackage();
92
93 final Intent intent = createServiceIntent();
94 final ServiceConnection serviceConnection = createServiceConnectionReportTime();
95
96 final long startTimeNs = System.nanoTime();
97 bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
98 try {
99 final long endTimeNs = getReceivedTimeNs(Constants.TYPE_SERVICE_CONNECTED);
100 return endTimeNs - startTimeNs;
101 } finally {
Arthur Eubanksebd2ea02018-02-13 10:20:39 -0800102 TargetPackageUtils.unbindFromService(mContext, serviceConnection);
Arthur Eubanks20a29572018-01-09 11:40:58 -0800103 }
104 });
105 }
106
107 /**
108 * Benchmark time from Context.bindService() to Service.onBind() when service is already bound
109 * to.
110 */
111 @Test
112 public void bindServiceAlreadyBound() {
113 runPerfFunction(() -> {
114 startTargetPackage();
115
116 final Intent intent = createServiceIntent();
Arthur Eubanksebd2ea02018-02-13 10:20:39 -0800117 final ServiceConnection alreadyBoundServiceConnection =
118 TargetPackageUtils.bindAndWaitForConnectedService(mContext, intent);
Arthur Eubanks20a29572018-01-09 11:40:58 -0800119
120 try {
121 final ServiceConnection serviceConnection = createServiceConnectionReportTime();
122
123 final long startTimeNs = System.nanoTime();
124 try {
125 bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
126 final long endTimeNs = getReceivedTimeNs(Constants.TYPE_SERVICE_CONNECTED);
127 return endTimeNs - startTimeNs;
128 } finally {
Arthur Eubanksebd2ea02018-02-13 10:20:39 -0800129 TargetPackageUtils.unbindFromService(mContext, serviceConnection);
Arthur Eubanks20a29572018-01-09 11:40:58 -0800130 }
131 } finally {
Arthur Eubanksebd2ea02018-02-13 10:20:39 -0800132 TargetPackageUtils.unbindFromService(mContext, alreadyBoundServiceConnection);
Arthur Eubanks20a29572018-01-09 11:40:58 -0800133 }
134 });
135 }
136
137 /**
138 * Benchmark time from Context.bindService() (without BIND_ALLOW_OOM_MANAGEMENT) to
139 * Service.onBind() when service is already bound to with BIND_ALLOW_OOM_MANAGEMENT.
140 */
141 @Test
142 public void bindServiceAllowOomManagement() {
143 runPerfFunction(() -> {
144 final Intent intentNoOom = createServiceIntent();
Arthur Eubanksebd2ea02018-02-13 10:20:39 -0800145 final ServiceConnection serviceConnectionOom =
146 TargetPackageUtils.bindAndWaitForConnectedService(mContext, intentNoOom,
147 Context.BIND_ALLOW_OOM_MANAGEMENT);
Arthur Eubanks20a29572018-01-09 11:40:58 -0800148
149 try {
150 final ServiceConnection serviceConnectionNoOom =
151 createServiceConnectionReportTime();
152 try {
153 final long startTimeNs = System.nanoTime();
154 bindService(intentNoOom, serviceConnectionNoOom, Context.BIND_AUTO_CREATE);
155 final long endTimeNs = getReceivedTimeNs(Constants.TYPE_SERVICE_CONNECTED);
156
157 return endTimeNs - startTimeNs;
158 } finally {
Arthur Eubanksebd2ea02018-02-13 10:20:39 -0800159 TargetPackageUtils.unbindFromService(mContext, serviceConnectionNoOom);
Arthur Eubanks20a29572018-01-09 11:40:58 -0800160 }
161 } finally {
Arthur Eubanksebd2ea02018-02-13 10:20:39 -0800162 TargetPackageUtils.unbindFromService(mContext, serviceConnectionOom);
Arthur Eubanks20a29572018-01-09 11:40:58 -0800163 }
164 });
165 }
166}