[go: nahoru, domu]

Merge "Validate process and compilation profiles." into androidx-master-dev
diff --git a/benchmark/benchmark-macro-runtime/build.gradle b/benchmark/benchmark-macro-runtime/build.gradle
index afef1e9..ee7d933 100644
--- a/benchmark/benchmark-macro-runtime/build.gradle
+++ b/benchmark/benchmark-macro-runtime/build.gradle
@@ -37,7 +37,6 @@
 dependencies {
     api(JUNIT)
     api(KOTLIN_STDLIB)
-    api(KOTLIN_COROUTINES_ANDROID)
     api("androidx.annotation:annotation:1.1.0")
     implementation(project(":benchmark:benchmark-common"))
     implementation(project(":benchmark:benchmark-perfetto"))
diff --git a/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/Actions.kt b/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/Actions.kt
index 25e42b1..544a39e 100644
--- a/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/Actions.kt
+++ b/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/Actions.kt
@@ -20,8 +20,6 @@
 import android.util.Log
 import androidx.test.platform.app.InstrumentationRegistry
 import androidx.test.uiautomator.UiDevice
-import kotlinx.coroutines.delay
-import kotlinx.coroutines.runBlocking
 
 private const val TAG = "MacroBenchmarks"
 
@@ -57,13 +55,14 @@
 
 /**
  * Compiles the application with the specified filter.
+ * For more information: https://source.android.com/devices/tech/dalvik/jit-compiler
  */
 internal fun compilationFilter(
     instrumentation: Instrumentation,
     packageName: String,
     mode: String,
     profileSaveTimeout: Long = 5000
-) = runBlocking {
+) {
     check(mode in COMPILE_MODES) {
         "Invalid compilation mode. Must be one of ${COMPILE_MODES.joinToString(",")}"
     }
@@ -72,13 +71,13 @@
         // For speed profile compilation, ART team recommended to wait for 5 secs when app
         // is in the foreground, dump the profile, wait for another 5 secs before
         // speed-profile compilation.
-        delay(profileSaveTimeout)
+        Thread.sleep(profileSaveTimeout)
         val response = device.executeShellCommand("killall -s SIGUSR1 $packageName")
         if (response.isNotBlank()) {
             Log.d(TAG, "Received dump profile response $response")
             throw RuntimeException("Failed to dump profile for $packageName ($response)")
         }
-        delay(profileSaveTimeout)
+        Thread.sleep(profileSaveTimeout)
     }
     val response = device.executeShellCommand("cmd package compile -f -m $mode $packageName")
     if (!response.contains("Success")) {
@@ -88,12 +87,22 @@
 }
 
 /**
+ * Clears existing compilation profiles.
+ */
+internal fun clearProfile(
+    instrumentation: Instrumentation,
+    packageName: String,
+) {
+    instrumentation.device().executeShellCommand("cmd package compile --reset $packageName")
+}
+
+/**
  * Presses the home button.
  */
-fun pressHome(instrumentation: Instrumentation, delayDurationMs: Long = 300) = runBlocking {
+fun pressHome(instrumentation: Instrumentation, delayDurationMs: Long = 300) {
     instrumentation.device().pressHome()
     // Sleep for statsd to update the metrics.
-    delay(delayDurationMs)
+    Thread.sleep(delayDurationMs)
 }
 
 /**
diff --git a/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/CompilationMode.kt b/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/CompilationMode.kt
index 586f917..bb93fa8 100644
--- a/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/CompilationMode.kt
+++ b/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/CompilationMode.kt
@@ -26,7 +26,16 @@
         }
         return compileArgument
     }
-    object None : CompilationMode(null)
-    class SpeedProfile(val warmupIterations: Int = 3) : CompilationMode("speed-profile")
-    object Speed : CompilationMode("speed")
-}
\ No newline at end of file
+
+    object None : CompilationMode(null) {
+        override fun toString() = "CompilationMode.None"
+    }
+
+    class SpeedProfile(val warmupIterations: Int = 3) : CompilationMode("speed-profile") {
+        override fun toString() = "CompilationMode.SpeedProfile (iterations =$warmupIterations)"
+    }
+
+    object Speed : CompilationMode("speed") {
+        override fun toString() = "CompilationMode.Speed"
+    }
+}
diff --git a/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/Macrobenchmark.kt b/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/Macrobenchmark.kt
index 2c0bc9c..12feb34 100644
--- a/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/Macrobenchmark.kt
+++ b/benchmark/benchmark-macro-runtime/src/main/java/androidx/benchmark/macro/Macrobenchmark.kt
@@ -107,11 +107,13 @@
 }
 
 internal fun CompilationMode.compile(packageName: String, block: () -> Unit) {
+    val instrumentation = InstrumentationRegistry.getInstrumentation()
+    // Clear profile between runs.
+    clearProfile(instrumentation, packageName)
     if (this == CompilationMode.None) {
         return // nothing to do
     }
     if (this is CompilationMode.SpeedProfile) {
-        // TODO: clear existing profiling state
         repeat(this.warmupIterations) {
             block()
         }
diff --git a/benchmark/benchmark-simple-macro-benchmark/src/androidTest/java/androidx/benchmark/macro/sample/ProcessSpeedProfileValidation.kt b/benchmark/benchmark-simple-macro-benchmark/src/androidTest/java/androidx/benchmark/macro/sample/ProcessSpeedProfileValidation.kt
new file mode 100644
index 0000000..7cef109
--- /dev/null
+++ b/benchmark/benchmark-simple-macro-benchmark/src/androidTest/java/androidx/benchmark/macro/sample/ProcessSpeedProfileValidation.kt
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2020 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 androidx.benchmark.macro.sample
+
+import android.content.Intent
+import androidx.benchmark.macro.CompilationMode
+import androidx.benchmark.macro.CpuUsageMetric
+import androidx.benchmark.macro.MacrobenchmarkConfig
+import androidx.benchmark.macro.StartupTimingMetric
+import androidx.benchmark.macro.macrobenchmark
+import androidx.test.filters.LargeTest
+import org.junit.Ignore
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.Parameterized
+
+@LargeTest
+@RunWith(Parameterized::class)
+class ProcessSpeedProfileValidation(
+    private val compilationMode: CompilationMode,
+    private val killProcess: Boolean
+) {
+    @Test
+    @Ignore("Not running the test in CI")
+    fun start() {
+        val benchmarkName = "speed_profile_process_validation"
+        val config = MacrobenchmarkConfig(
+            packageName = PACKAGE_NAME,
+            metrics = listOf(CpuUsageMetric(), StartupTimingMetric()),
+            compilationMode = compilationMode,
+            killProcessEachIteration = killProcess,
+            iterations = 10
+        )
+        macrobenchmark(
+            benchmarkName = benchmarkName,
+            config = config
+        ) {
+            pressHome()
+            launchPackageAndWait { launchIntent ->
+                // Clear out any previous instances
+                launchIntent.flags =
+                    Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
+            }
+        }
+    }
+
+    companion object {
+        private const val PACKAGE_NAME = "androidx.benchmark.integration.macro.target"
+
+        @Parameterized.Parameters(name = "compilation_mode={0}, kill_process={1}")
+        @JvmStatic
+        fun kilProcessParameters(): List<Array<Any>> {
+            val compilationModes = listOf(
+                CompilationMode.None,
+                CompilationMode.SpeedProfile(warmupIterations = 3)
+            )
+            val processKillOptions = listOf(true, false)
+            return compilationModes.zip(processKillOptions).map {
+                arrayOf(it.first, it.second)
+            }
+        }
+    }
+}