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

Commit 7ae3983f authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Add benchmark tests for coroutine tracing" into main am: e2dc8258

parents c6c8d945 e2dc8258
Loading
Loading
Loading
Loading
+48 −0
Original line number Diff line number Diff line
// Copyright (C) 2024 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 {
    default_team: "trendy_team_performance",
    default_applicable_licenses: ["Android-Apache-2.0"],
}

android_test {
    name: "tracinglib-benchmark",

    srcs: ["src/**/*.kt"],

    static_libs: [
        "androidx.annotation_annotation",
        "androidx.benchmark_benchmark-common",
        "androidx.benchmark_benchmark-junit4",
        "androidx.test.core",
        "androidx.test.ext.junit",
        "androidx.test.rules",
        "androidx.test.runner",
        "apct-perftests-utils",
        "collector-device-lib",
        "flag-junit",
        "kotlinx_coroutines_android",
        "platform-test-rules",
        "tracinglib-platform",
    ],

    data: [":perfetto_artifacts"],

    sdk_version: "current",
    certificate: "platform",
    use_resource_processor: true,

    test_suites: ["device-tests"],
}
+26 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2024 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.app.tracing.benchmark" >

    <application/>

    <instrumentation android:name="androidx.benchmark.junit4.AndroidBenchmarkRunner"
                     android:targetPackage="com.android.app.tracing.benchmark"
                     android:label="Benchmark tests for tracinglib"/>

</manifest>
+61 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2024 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.
-->
<configuration description="Runs tracinglib-benchmark metric instrumentation.">
    <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer"/>
    <!-- Needed for pushing the trace config file -->
    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
        <option name="push-file" key="trace_config_detailed.textproto" value="/data/misc/perfetto-traces/trace_config.textproto" />
    </target_preparer>

    <target_preparer class="com.android.tradefed.targetprep.TestFilePushSetup" />
    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer" />

    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
        <option name="cleanup-apks" value="true" />
        <option name="test-file-name" value="tracinglib-benchmark.apk" />
    </target_preparer>

    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
        <option name="run-command" value="input keyevent KEYCODE_WAKEUP" />
    </target_preparer>

    <!-- Needed for pulling the collected trace config on to the host -->
    <metrics_collector class="com.android.tradefed.device.metric.FilePullerLogCollector">
        <option name="pull-pattern-keys" value="perfetto_file_path" />
    </metrics_collector>

    <!-- Needed for storing the perfetto trace files in the sdcard/test_results -->
    <option name="isolated-storage" value="false" />

    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
        <option name="package" value="com.android.app.tracing.benchmark" />
        <!-- <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" /> -->

        <option name="device-listeners" value="android.device.collectors.ProcLoadListener,android.device.collectors.PerfettoListener" />
        <!-- ProcLoadListener related arguments -->
        <!-- Wait for device last minute threshold to reach 3 with 2 minute timeout before starting the test run -->
        <option name="instrumentation-arg" key="procload-collector:per_run" value="true" />
        <option name="instrumentation-arg" key="proc-loadavg-threshold" value="3" />
        <option name="instrumentation-arg" key="proc-loadavg-timeout" value="120000" />
        <option name="instrumentation-arg" key="proc-loadavg-interval" value="10000" />

        <!-- PerfettoListener related arguments -->
        <option name="instrumentation-arg" key="perfetto_config_text_proto" value="true" />
        <option name="instrumentation-arg" key="perfetto_config_file" value="trace_config.textproto" />

        <option name="instrumentation-arg" key="newRunListenerMode" value="true" />
    </test>
</configuration>
+142 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 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.app.tracing.benchmark

import android.os.Trace
import android.perftests.utils.BenchmarkState
import android.perftests.utils.PerfStatusReporter
import android.platform.test.annotations.EnableFlags
import android.platform.test.flag.junit.SetFlagsRule
import android.platform.test.rule.EnsureDeviceSettingsRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.app.tracing.coroutines.TraceContextElement
import com.android.app.tracing.coroutines.launch
import com.android.app.tracing.coroutines.traceCoroutine
import com.android.systemui.Flags
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.ClassRule
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

private val TAG: String = TraceContextMicroBenchmark::class.java.simpleName

@RunWith(AndroidJUnit4::class)
@EnableFlags(Flags.FLAG_COROUTINE_TRACING)
class TraceContextMicroBenchmark {

    @get:Rule val perfStatusReporter = PerfStatusReporter()

    @get:Rule val setFlagsRule = SetFlagsRule()

    companion object {
        @JvmField @ClassRule(order = 1) var ensureDeviceSettingsRule = EnsureDeviceSettingsRule()
    }

    @Before
    fun before() {
        Assert.assertTrue(Trace.isEnabled())
    }

    @After
    fun after() {
        Assert.assertTrue(Trace.isEnabled())
    }

    private suspend fun ensureSuspend(state: BenchmarkState) {
        state.pauseTiming()
        delay(1)
        state.resumeTiming()
    }

    @SmallTest
    @Test
    fun testSingleTraceSection() {
        val state = perfStatusReporter.benchmarkState
        runBlocking(TraceContextElement()) {
            while (state.keepRunning()) {
                traceCoroutine("hello-world") { ensureSuspend(state) }
            }
        }
    }

    @SmallTest
    @Test
    fun testNestedContext() {
        val state = perfStatusReporter.benchmarkState

        val context1 = TraceContextElement()
        val context2 = TraceContextElement()
        runBlocking {
            while (state.keepRunning()) {
                withContext(context1) {
                    traceCoroutine("hello") {
                        traceCoroutine("world") {
                            withContext(context2) {
                                traceCoroutine("hallo") {
                                    traceCoroutine("welt") { ensureSuspend(state) }
                                    ensureSuspend(state)
                                }
                            }
                            ensureSuspend(state)
                        }
                        ensureSuspend(state)
                    }
                }
            }
        }
    }

    @SmallTest
    @Test
    fun testInterleavedLaunch() {
        val state = perfStatusReporter.benchmarkState

        runBlocking(TraceContextElement()) {
            val job1 =
                launch(TraceContextElement()) {
                    while (true) {
                        traceCoroutine("hello") {
                            traceCoroutine("world") { yield() }
                            yield()
                        }
                    }
                }
            val job2 =
                launch(TraceContextElement()) {
                    while (true) {
                        traceCoroutine("hallo") {
                            traceCoroutine("welt") { yield() }
                            yield()
                        }
                    }
                }
            while (state.keepRunning()) {
                repeat(10_000) { traceCoroutine("main-loop") { yield() } }
            }
            job1.cancel()
            job2.cancel()
        }
    }
}