Arthur Eubanks | 20a2957 | 2018-01-09 11:40:58 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.frameworks.perftests.am.tests; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.ServiceConnection; |
| 23 | import android.os.IBinder; |
Brett Chabot | 502ec7a | 2019-03-01 14:43:20 -0800 | [diff] [blame^] | 24 | |
| 25 | import androidx.test.filters.LargeTest; |
| 26 | import androidx.test.runner.AndroidJUnit4; |
Arthur Eubanks | 20a2957 | 2018-01-09 11:40:58 -0800 | [diff] [blame] | 27 | |
| 28 | import com.android.frameworks.perftests.am.util.Constants; |
Arthur Eubanks | ebd2ea0 | 2018-02-13 10:20:39 -0800 | [diff] [blame] | 29 | import com.android.frameworks.perftests.am.util.TargetPackageUtils; |
Arthur Eubanks | 20a2957 | 2018-01-09 11:40:58 -0800 | [diff] [blame] | 30 | |
| 31 | import org.junit.Assert; |
| 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | |
| 35 | @RunWith(AndroidJUnit4.class) |
| 36 | @LargeTest |
| 37 | public 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 Eubanks | ebd2ea0 | 2018-02-13 10:20:39 -0800 | [diff] [blame] | 80 | TargetPackageUtils.unbindFromService(mContext, serviceConnection); |
Arthur Eubanks | 20a2957 | 2018-01-09 11:40:58 -0800 | [diff] [blame] | 81 | } |
| 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 Eubanks | ebd2ea0 | 2018-02-13 10:20:39 -0800 | [diff] [blame] | 102 | TargetPackageUtils.unbindFromService(mContext, serviceConnection); |
Arthur Eubanks | 20a2957 | 2018-01-09 11:40:58 -0800 | [diff] [blame] | 103 | } |
| 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 Eubanks | ebd2ea0 | 2018-02-13 10:20:39 -0800 | [diff] [blame] | 117 | final ServiceConnection alreadyBoundServiceConnection = |
| 118 | TargetPackageUtils.bindAndWaitForConnectedService(mContext, intent); |
Arthur Eubanks | 20a2957 | 2018-01-09 11:40:58 -0800 | [diff] [blame] | 119 | |
| 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 Eubanks | ebd2ea0 | 2018-02-13 10:20:39 -0800 | [diff] [blame] | 129 | TargetPackageUtils.unbindFromService(mContext, serviceConnection); |
Arthur Eubanks | 20a2957 | 2018-01-09 11:40:58 -0800 | [diff] [blame] | 130 | } |
| 131 | } finally { |
Arthur Eubanks | ebd2ea0 | 2018-02-13 10:20:39 -0800 | [diff] [blame] | 132 | TargetPackageUtils.unbindFromService(mContext, alreadyBoundServiceConnection); |
Arthur Eubanks | 20a2957 | 2018-01-09 11:40:58 -0800 | [diff] [blame] | 133 | } |
| 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 Eubanks | ebd2ea0 | 2018-02-13 10:20:39 -0800 | [diff] [blame] | 145 | final ServiceConnection serviceConnectionOom = |
| 146 | TargetPackageUtils.bindAndWaitForConnectedService(mContext, intentNoOom, |
| 147 | Context.BIND_ALLOW_OOM_MANAGEMENT); |
Arthur Eubanks | 20a2957 | 2018-01-09 11:40:58 -0800 | [diff] [blame] | 148 | |
| 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 Eubanks | ebd2ea0 | 2018-02-13 10:20:39 -0800 | [diff] [blame] | 159 | TargetPackageUtils.unbindFromService(mContext, serviceConnectionNoOom); |
Arthur Eubanks | 20a2957 | 2018-01-09 11:40:58 -0800 | [diff] [blame] | 160 | } |
| 161 | } finally { |
Arthur Eubanks | ebd2ea0 | 2018-02-13 10:20:39 -0800 | [diff] [blame] | 162 | TargetPackageUtils.unbindFromService(mContext, serviceConnectionOom); |
Arthur Eubanks | 20a2957 | 2018-01-09 11:40:58 -0800 | [diff] [blame] | 163 | } |
| 164 | }); |
| 165 | } |
| 166 | } |