Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 20a29576 authored by Arthur Eubanks's avatar Arthur Eubanks
Browse files

Add performance tests for Service

Test: m ActivityManagerPerfTestsTestApp ActivityManagerPerfTests
Test: adb install \
$OUT/data/app/ActivityManagerPerfTestsTestApp/ActivityManagerPerfTestsTestApp.apk
Test: adb install \
$OUT/data/app/ActivityManagerPerfTests/ActivityManagerPerfTests.apk
Test: adb shell am instrument -w -e class \
com.android.frameworks.perftests.am.tests.ServiceStartPerfTest \
com.android.frameworks.perftests.amtests/android.support.test.runner.AndroidJUnitRunner
Test: adb shell am instrument -w -e class \
com.android.frameworks.perftests.am.tests.ServiceBindPerfTest \
com.android.frameworks.perftests.amtests/android.support.test.runner.AndroidJUnitRunner

BUG: 67460485

Change-Id: I16ea5752def13aabd481aeb29f1af0ac04e75f6a
parent c65eb444
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -29,5 +29,8 @@
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
        <service
                android:name=".TestService"
                android:exported="true" />
    </application>
</manifest>
+0 −1
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.frameworks.perftests.amteststestapp;

import android.app.Activity;
import android.os.Looper;
import android.os.MessageQueue;

import com.android.frameworks.perftests.am.util.Constants;
import com.android.frameworks.perftests.am.util.Utils;
+38 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.android.frameworks.perftests.amteststestapp;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.android.frameworks.perftests.am.util.Constants;
import com.android.frameworks.perftests.am.util.Utils;

public class TestService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return new Binder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Utils.sendTime(intent, Constants.TYPE_SERVICE_START);
        return super.onStartCommand(intent, flags, startId);
    }
}
+65 −2
Original line number Diff line number Diff line
@@ -16,8 +16,11 @@

package com.android.frameworks.perftests.am.tests;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.perftests.utils.ManualBenchmarkState;
import android.perftests.utils.PerfManualStatusReporter;
import android.support.test.InstrumentationRegistry;
@@ -26,13 +29,17 @@ import com.android.frameworks.perftests.am.util.TargetPackageUtils;
import com.android.frameworks.perftests.am.util.TimeReceiver;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.LongSupplier;

public class BasePerfTest {
    private static final String TAG = BasePerfTest.class.getSimpleName();
    private static final long AWAIT_SERVICE_CONNECT_MS = 2000;

    private TimeReceiver mTimeReceiver;

@@ -52,14 +59,70 @@ public class BasePerfTest {
        TargetPackageUtils.killTargetPackage(mContext);
    }

    protected Intent createIntent(String action) {
    protected void addReceivedTimeNs(String type) {
        mTimeReceiver.addTimeForTypeToQueue(type, System.nanoTime());
    }

    protected Intent createServiceIntent() {
        final Intent intent = new Intent();
        intent.setClassName(TargetPackageUtils.PACKAGE_NAME,
                TargetPackageUtils.SERVICE_NAME);
        putTimeReceiverBinderExtra(intent);
        return intent;
    }

    protected ServiceConnection bindAndWaitForConnectedService() {
        return bindAndWaitForConnectedService(0);
    }

    protected ServiceConnection bindAndWaitForConnectedService(int flags) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        final ServiceConnection serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                countDownLatch.countDown();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };

        final Intent intent = createServiceIntent();
        final boolean success = mContext.bindService(intent, serviceConnection,
                Context.BIND_AUTO_CREATE | flags);
        Assert.assertTrue("Could not bind to service", success);

        try {
            boolean connectedSuccess = countDownLatch.await(AWAIT_SERVICE_CONNECT_MS,
                    TimeUnit.MILLISECONDS);
            Assert.assertTrue("Timeout when waiting for ServiceConnection.onServiceConnected()",
                    connectedSuccess);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        return serviceConnection;
    }

    protected void unbindFromService(ServiceConnection serviceConnection) {
        if (serviceConnection != null) {
            mContext.unbindService(serviceConnection);
        }
    }

    protected Intent createBroadcastIntent(String action) {
        final Intent intent = new Intent(action);
        intent.addFlags(
                Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND | Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        intent.putExtras(mTimeReceiver.createReceiveTimeExtraBinder());
        putTimeReceiverBinderExtra(intent);
        return intent;
    }

    protected void putTimeReceiverBinderExtra(Intent intent) {
        intent.putExtras(mTimeReceiver.createReceiveTimeExtraBinder());
    }

    private void setUpIteration() {
        mTimeReceiver.clear();
        TargetPackageUtils.killTargetPackage(mContext);
+6 −3
Original line number Diff line number Diff line
@@ -33,7 +33,8 @@ public class BroadcastPerfTest extends BasePerfTest {
        runPerfFunction(() -> {
            startTargetPackage();

            final Intent intent = createIntent(Constants.ACTION_BROADCAST_MANIFEST_RECEIVE);
            final Intent intent = createBroadcastIntent(
                    Constants.ACTION_BROADCAST_MANIFEST_RECEIVE);

            final long startTime = System.nanoTime();

@@ -48,7 +49,8 @@ public class BroadcastPerfTest extends BasePerfTest {
    @Test
    public void manifestBroadcastNotRunning() {
        runPerfFunction(() -> {
            final Intent intent = createIntent(Constants.ACTION_BROADCAST_MANIFEST_RECEIVE);
            final Intent intent = createBroadcastIntent(
                    Constants.ACTION_BROADCAST_MANIFEST_RECEIVE);

            final long startTime = System.nanoTime();

@@ -65,7 +67,8 @@ public class BroadcastPerfTest extends BasePerfTest {
        runPerfFunction(() -> {
            startTargetPackage();

            final Intent intent = createIntent(Constants.ACTION_BROADCAST_REGISTERED_RECEIVE);
            final Intent intent = createBroadcastIntent(
                    Constants.ACTION_BROADCAST_REGISTERED_RECEIVE);

            final long startTime = System.nanoTime();

Loading