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
17package com.android.frameworks.perftests.am.tests;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.ServiceConnection;
22import android.perftests.utils.ManualBenchmarkState;
23import android.perftests.utils.PerfManualStatusReporter;
24import android.support.test.InstrumentationRegistry;
25
26import com.android.frameworks.perftests.am.util.TargetPackageUtils;
27import com.android.frameworks.perftests.am.util.TimeReceiver;
28
29import org.junit.Before;
30import org.junit.Rule;
31
32import java.util.function.LongSupplier;
33
34public class BasePerfTest {
35    private static final String TAG = BasePerfTest.class.getSimpleName();
36
37    private TimeReceiver mTimeReceiver;
38    private ServiceConnection mAliveServiceConnection;
39
40    @Rule
41    public PerfManualStatusReporter mPerfManualStatusReporter = new PerfManualStatusReporter();
42
43    protected Context mContext;
44
45    @Before
46    public void setUp() {
47        mContext = InstrumentationRegistry.getTargetContext();
48        mTimeReceiver = new TimeReceiver();
49    }
50
51    protected void addReceivedTimeNs(String type) {
52        mTimeReceiver.addTimeForTypeToQueue(type, System.nanoTime());
53    }
54
55    protected Intent createServiceIntent() {
56        final Intent intent = new Intent();
57        intent.setClassName(TargetPackageUtils.PACKAGE_NAME,
58                TargetPackageUtils.SERVICE_NAME);
59        putTimeReceiverBinderExtra(intent);
60        return intent;
61    }
62
63    protected Intent createBroadcastIntent(String action) {
64        final Intent intent = new Intent(action);
65        intent.addFlags(
66                Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND | Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
67        putTimeReceiverBinderExtra(intent);
68        return intent;
69    }
70
71    protected void putTimeReceiverBinderExtra(Intent intent) {
72        intent.putExtras(mTimeReceiver.createReceiveTimeExtraBinder());
73    }
74
75    private void setUpIteration() {
76        mTimeReceiver.clear();
77    }
78
79    private void tearDownIteration() {
80        TargetPackageUtils.killTargetPackage(mContext, mAliveServiceConnection);
81    }
82
83    protected void startTargetPackage() {
84        mAliveServiceConnection = TargetPackageUtils.startTargetPackage(mContext);
85    }
86
87    protected long getReceivedTimeNs(String type) {
88        return mTimeReceiver.getReceivedTimeNs(type);
89    }
90
91    protected void runPerfFunction(LongSupplier func) {
92        final ManualBenchmarkState benchmarkState = mPerfManualStatusReporter.getBenchmarkState();
93        long elapsedTimeNs = 0;
94        while (benchmarkState.keepRunning(elapsedTimeNs)) {
95            setUpIteration();
96            elapsedTimeNs = func.getAsLong();
97            tearDownIteration();
98        }
99    }
100}
101