Skip to content

Commit

Permalink
Merge pull request #39 from niscy-eudiw/main
Browse files Browse the repository at this point in the history
Android baseline profile implementation, setup and generation
  • Loading branch information
stzouvaras authored Mar 1, 2024
2 parents 710a1eb + db5367f commit 7ae7684
Show file tree
Hide file tree
Showing 16 changed files with 38,068 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ android {

dependencies {
implementation(project(LibraryModule.AssemblyLogic.path))
"baselineProfile"(project(LibraryModule.BaselineProfileLogic.path))
}
18,874 changes: 18,874 additions & 0 deletions app/src/main/baseline-prof.txt

Large diffs are not rendered by default.

18,874 changes: 18,874 additions & 0 deletions app/src/main/startup-prof.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions baseline-profile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
23 changes: 23 additions & 0 deletions baseline-profile/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 European Commission
*
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work
* except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the Licence for the specific language
* governing permissions and limitations under the Licence.
*/

plugins {
id("eudi.android.base.profile")
}

android {
namespace = "eu.europa.ec.baselineprofile"
}
17 changes: 17 additions & 0 deletions baseline-profile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--
~ Copyright (c) 2023 European Commission
~
~ Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European
~ Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work
~ except in compliance with the Licence.
~
~ You may obtain a copy of the Licence at:
~ https://joinup.ec.europa.eu/software/page/eupl
~
~ Unless required by applicable law or agreed to in writing, software distributed under
~ the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
~ ANY KIND, either express or implied. See the Licence for the specific language
~ governing permissions and limitations under the Licence.
-->

<manifest />
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2023 European Commission
*
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work
* except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the Licence for the specific language
* governing permissions and limitations under the Licence.
*/

package eu.europa.ec.baselineprofile

import androidx.benchmark.macro.junit4.BaselineProfileRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* This test class generates a basic startup baseline profile for the target package.
*
* We recommend you start with this but add important user flows to the profile to improve their performance.
* Refer to the [baseline profile documentation](https://d.android.com/topic/performance/baselineprofiles)
* for more information.
*
* You can run the generator with the "Generate Baseline Profile" run configuration in Android Studio or
* the equivalent `generateBaselineProfile` gradle task:
* ```
* ./gradlew :app:generateReleaseBaselineProfile
* ```
* The run configuration runs the Gradle task and applies filtering to run only the generators.
*
* Check [documentation](https://d.android.com/topic/performance/benchmarking/macrobenchmark-instrumentation-args)
* for more information about available instrumentation arguments.
*
* After you run the generator, you can verify the improvements running the [StartupBenchmarks] benchmark.
*
* When using this class to generate a baseline profile, only API 33+ or rooted API 28+ are supported.
*
* The minimum required version of androidx.benchmark to generate a baseline profile is 1.2.0.
**/
@RunWith(AndroidJUnit4::class)
@LargeTest
class BaselineProfileGenerator {

@get:Rule
val rule = BaselineProfileRule()

@Test
fun generate() {
// The application id for the running build variant is read from the instrumentation arguments.
rule.collect(
packageName = InstrumentationRegistry.getArguments().getString("targetAppId")
?: throw Exception("targetAppId not passed as instrumentation runner arg"),

// See: https://d.android.com/topic/performance/baselineprofiles/dex-layout-optimizations
includeInStartupProfile = true
) {
// This block defines the app's critical user journey. Here we are interested in
// optimizing for app startup. But you can also navigate and scroll through your most important UI.

// Start default activity for your app
pressHome()
startActivityAndWait()

// TODO Write more interactions to optimize advanced journeys of your app.
// For example:
// 1. Wait until the content is asynchronously loaded
// 2. Scroll the feed content
// 3. Navigate to detail screen

// Check UiAutomator documentation for more information how to interact with the app.
// https://d.android.com/training/testing/other-components/ui-automator
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2023 European Commission
*
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work
* except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the Licence for the specific language
* governing permissions and limitations under the Licence.
*/

package eu.europa.ec.baselineprofile

import androidx.benchmark.macro.BaselineProfileMode
import androidx.benchmark.macro.CompilationMode
import androidx.benchmark.macro.StartupMode
import androidx.benchmark.macro.StartupTimingMetric
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* This test class benchmarks the speed of app startup.
* Run this benchmark to verify how effective a Baseline Profile is.
* It does this by comparing [CompilationMode.None], which represents the app with no Baseline
* Profiles optimizations, and [CompilationMode.Partial], which uses Baseline Profiles.
*
* Run this benchmark to see startup measurements and captured system traces for verifying
* the effectiveness of your Baseline Profiles. You can run it directly from Android
* Studio as an instrumentation test, or run all benchmarks for a variant, for example benchmarkRelease,
* with this Gradle task:
* ```
* ./gradlew :baseline-profile:connectedBenchmarkReleaseAndroidTest
* ```
*
* You should run the benchmarks on a physical device, not an Android emulator, because the
* emulator doesn't represent real world performance and shares system resources with its host.
*
* For more information, see the [Macrobenchmark documentation](https://d.android.com/macrobenchmark#create-macrobenchmark)
* and the [instrumentation arguments documentation](https://d.android.com/topic/performance/benchmarking/macrobenchmark-instrumentation-args).
**/
@RunWith(AndroidJUnit4::class)
@LargeTest
class StartupBenchmarks {

@get:Rule
val rule = MacrobenchmarkRule()

@Test
fun startupCompilationNone() =
benchmark(CompilationMode.None())

@Test
fun startupCompilationBaselineProfiles() =
benchmark(CompilationMode.Partial(BaselineProfileMode.Require))

private fun benchmark(compilationMode: CompilationMode) {
// The application id for the running build variant is read from the instrumentation arguments.
rule.measureRepeated(
packageName = InstrumentationRegistry.getArguments().getString("targetAppId")
?: throw Exception("targetAppId not passed as instrumentation runner arg"),
metrics = listOf(StartupTimingMetric()),
compilationMode = compilationMode,
startupMode = StartupMode.COLD,
iterations = 10,
setupBlock = {
pressHome()
},
measureBlock = {
startActivityAndWait()

// TODO Add interactions to wait for when your app is fully drawn.
// The app is fully drawn when Activity.reportFullyDrawn is called.
// For Jetpack Compose, you can use ReportDrawn, ReportDrawnWhen and ReportDrawnAfter
// from the AndroidX Activity library.

// Check the UiAutomator documentation for more information on how to
// interact with the app.
// https://d.android.com/training/testing/other-components/ui-automator
}
)
}
}
5 changes: 5 additions & 0 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
compileOnly(libs.secrets.gradlePlugin)
compileOnly(libs.owasp.dependencycheck.gradlePlugin)
compileOnly(libs.kotlinx.kover.gradlePlugin)
compileOnly(libs.baselineprofile.gradlePlugin)
}

gradlePlugin {
Expand Down Expand Up @@ -113,5 +114,9 @@ gradlePlugin {
id = "eudi.sonar"
implementationClass = "SonarPlugin"
}
register("androidBaseProfile") {
id = "eudi.android.base.profile"
implementationClass = "BaseLineProfilePlugin"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import eu.europa.ec.euidi.configureGradleManagedDevices
import eu.europa.ec.euidi.configureKotlinAndroid
import eu.europa.ec.euidi.configurePrintApksTask
import eu.europa.ec.euidi.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies

class AndroidApplicationConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
Expand All @@ -34,6 +36,7 @@ class AndroidApplicationConventionPlugin : Plugin<Project> {
apply("eudi.android.lint")
apply("eudi.owasp.dependency.check")
apply("eudi.sonar")
apply("androidx.baselineprofile")
}

extensions.configure<ApplicationExtension> {
Expand All @@ -44,6 +47,10 @@ class AndroidApplicationConventionPlugin : Plugin<Project> {
extensions.configure<ApplicationAndroidComponentsExtension> {
configurePrintApksTask(this)
}

dependencies {
add("implementation", libs.findLibrary("androidx-profileinstaller").get())
}
}
}
}
79 changes: 79 additions & 0 deletions build-logic/convention/src/main/kotlin/BaseLineProfilePlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2023 European Commission
*
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work
* except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the Licence for the specific language
* governing permissions and limitations under the Licence.
*/
import androidx.baselineprofile.gradle.producer.BaselineProfileProducerExtension
import com.android.build.api.variant.TestAndroidComponentsExtension
import com.android.build.gradle.TestExtension
import eu.europa.ec.euidi.configureFlavors
import eu.europa.ec.euidi.configureGradleManagedDevices
import eu.europa.ec.euidi.configureKotlinAndroid
import eu.europa.ec.euidi.getProperty
import eu.europa.ec.euidi.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies

@Suppress("UnstableApiUsage")
class BaseLineProfilePlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {

val storedVersion = getProperty<String>(
"VERSION_NAME",
"version.properties"
).orEmpty()

with(pluginManager) {
apply("com.android.test")
apply("org.jetbrains.kotlin.android")
apply("androidx.baselineprofile")
}

extensions.configure<TestExtension> {
configureKotlinAndroid(this)
with(defaultConfig) {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
configureFlavors(this, storedVersion)
configureGradleManagedDevices(this)
targetProjectPath = ":app"
}

extensions.configure<BaselineProfileProducerExtension> {
managedDevices += "pixel6api34google"
useConnectedDevices = false
}

extensions.configure<TestAndroidComponentsExtension> {
onVariants { v ->
v.instrumentationRunnerArguments.put(
"targetAppId",
v.testedApks.map {
v.artifacts.getBuiltArtifactsLoader().load(it)?.applicationId.orEmpty()
}
)
}
}

dependencies {
add("implementation", libs.findLibrary("androidx-test-orchestrator").get())
add("implementation", libs.findLibrary("androidx-test-uiautomator").get())
add("implementation", libs.findLibrary("androidx-test-espresso-core").get())
add("implementation", libs.findLibrary("androidx-benchmark-macro").get())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* governing permissions and limitations under the Licence.
*/

@file:Suppress("UnstableApiUsage")

package eu.europa.ec.euidi

import com.android.build.api.dsl.CommonExtension
Expand All @@ -28,7 +30,7 @@ internal fun configureGradleManagedDevices(
commonExtension: CommonExtension<*, *, *, *, *, *>,
) {
val pixel4 = DeviceConfig("Pixel 4", 30, "aosp-atd")
val pixel6 = DeviceConfig("Pixel 6", 31, "aosp")
val pixel6 = DeviceConfig("Pixel 6", 34, "google")
val pixelC = DeviceConfig("Pixel C", 30, "aosp-atd")

val allDevices = listOf(pixel4, pixel6, pixelC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum class LibraryModule(val path: String) {
UiLogic(":ui-logic"),
NetworkLogic(":network-logic"),
ResourcesLogic(":resources-logic"),
BaselineProfileLogic(":baseline-profile"),
CommonFeature(":common-feature"),
StartupFeature(":startup-feature"),
LoginFeature(":login-feature"),
Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ plugins {
alias(libs.plugins.owasp.dependencycheck) apply false
alias(libs.plugins.kotlinx.kover) apply false
alias(libs.plugins.sonar) apply false
alias(libs.plugins.android.test) apply false
alias(libs.plugins.baselineprofile) apply false
}
true
Loading

0 comments on commit 7ae7684

Please sign in to comment.