Skip to content

Commit

Permalink
Add macro benchmarking support for the Android editor
Browse files Browse the repository at this point in the history
  • Loading branch information
m4gr3d committed Jul 22, 2024
1 parent 4e5ed0b commit 27e2e3c
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 6 deletions.
9 changes: 9 additions & 0 deletions platform/android/java/editor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
implementation "androidx.window:window:1.3.0"
implementation "androidx.core:core-splashscreen:$versions.splashscreenVersion"
implementation "androidx.constraintlayout:constraintlayout:2.1.4"
implementation "androidx.profileinstaller:profileinstaller:1.3.1"
}

ext {
Expand Down Expand Up @@ -132,6 +133,14 @@ android {
applicationIdSuffix ".dev"
manifestPlaceholders += [editorBuildSuffix: " (dev)"]
}
benchmark {
initWith release
applicationIdSuffix ".benchmark"
manifestPlaceholders += [editorBuildSuffix: " (benchmark)"]
signingConfig signingConfigs.debug
matchingFallbacks = ['release']
debuggable false
}

debug {
initWith release
Expand Down
1 change: 1 addition & 0 deletions platform/android/java/editor/macrobenchmark/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
55 changes: 55 additions & 0 deletions platform/android/java/editor/macrobenchmark/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
plugins {
id 'com.android.test'
id 'org.jetbrains.kotlin.android'
}

android {
namespace 'org.godotengine.editor.macrobenchmark'
compileSdk versions.compileSdk

compileOptions {
sourceCompatibility versions.javaVersion
targetCompatibility versions.javaVersion
}

kotlinOptions {
jvmTarget = versions.javaVersion
}

defaultConfig {
minSdk 24
targetSdk versions.targetSdk
missingDimensionStrategy 'products', 'editor'

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = 'EMULATOR'
}

buildTypes {
// This benchmark buildType is used for benchmarking, and should function like your
// release build (for example, with minification on). It's signed with a debug key
// for easy local/CI testing.
benchmark {
debuggable = true
signingConfig = debug.signingConfig
matchingFallbacks = ["release"]
}
}

targetProjectPath = ":editor"
experimentalProperties["android.experimental.self-instrumenting"] = true
}

dependencies {
implementation 'androidx.test:rules:1.5.0'
implementation 'androidx.test.ext:junit:1.1.5'
implementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'androidx.test.uiautomator:uiautomator:2.3.0'
implementation 'androidx.benchmark:benchmark-macro-junit4:1.2.4'
}

androidComponents {
beforeVariants(selector().all()) {
enable = buildType == "benchmark"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest />
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.godotengine.editor.macrobenchmark

import android.Manifest
import androidx.benchmark.macro.ExperimentalMetricApi
import androidx.benchmark.macro.MemoryUsageMetric
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.rule.GrantPermissionRule
import androidx.test.uiautomator.By
import androidx.test.uiautomator.StaleObjectException
import androidx.test.uiautomator.Until
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Set of editor macro benchmarks.
*
* Before running, switch the editor's active build variant to 'benchmark'
*/
@RunWith(AndroidJUnit4::class)
class EditorBenchmarks {

companion object {
const val PACKAGE_NAME = "org.godotengine.editor.v4.benchmark"
}

@get:Rule val benchmarkRule = MacrobenchmarkRule()
@get:Rule val grantPermissionRule = GrantPermissionRule.grant(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE)

/**
* Navigates to the device's home screen, and launches the Project Manager.
*/
@OptIn(ExperimentalMetricApi::class)
@Test
fun startupProjectManager() = benchmarkRule.measureRepeated(
packageName = PACKAGE_NAME,
metrics = listOf(
StartupTimingMetric(),
MemoryUsageMetric(MemoryUsageMetric.Mode.Max),
),
iterations = 5,
startupMode = StartupMode.COLD
) {
pressHome()
startActivityAndWait()

try {
val editorLoadingIndicator = device.findObject(By.res(PACKAGE_NAME, "editor_loading_indicator"))
editorLoadingIndicator.wait(Until.gone(By.res(PACKAGE_NAME, "editor_loading_indicator")), 5_000)
} catch (ignored: StaleObjectException) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ open class GodotEditor : GodotActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()

// We exclude certain permissions from the set we request at startup, as they'll be
// requested on demand based on use-cases.
PermissionsUtil.requestManifestPermissions(this, setOf(Manifest.permission.RECORD_AUDIO))
if (BuildConfig.BUILD_TYPE != "benchmark") {
// We exclude certain permissions from the set we request at startup, as they'll be
// requested on demand based on use-cases.
PermissionsUtil.requestManifestPermissions(this, setOf(Manifest.permission.RECORD_AUDIO))
}

val params = intent.getStringArrayExtra(EXTRA_COMMAND_LINE_PARAMS)
Log.d(TAG, "Starting intent $intent with parameters ${params.contentToString()}")
Expand Down
3 changes: 3 additions & 0 deletions platform/android/java/lib/src/org/godotengine/godot/Godot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,9 @@ class Godot(private val context: Context) : SensorEventListener {
*/
private fun onGodotMainLoopStarted() {
Log.v(TAG, "OnGodotMainLoopStarted")
runOnUiThread {
getActivity()?.reportFullyDrawn()
}

for (plugin in pluginRegistry.allPlugins) {
plugin.onGodotMainLoopStarted()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private val benchmarkTracker = Collections.synchronizedMap(LinkedHashMap<Pair<St
* Note: Only enabled on 'editorDev' build variant.
*/
fun beginBenchmarkMeasure(scope: String, label: String) {
if (BuildConfig.FLAVOR != "editor" || BuildConfig.BUILD_TYPE != "dev") {
if (BuildConfig.FLAVOR != "editor" || (BuildConfig.BUILD_TYPE != "dev" && BuildConfig.BUILD_TYPE != "benchmark")) {
return
}
val key = Pair(scope, label)
Expand All @@ -83,7 +83,7 @@ fun beginBenchmarkMeasure(scope: String, label: String) {
*/
@JvmOverloads
fun endBenchmarkMeasure(scope: String, label: String, dumpBenchmark: Boolean = false) {
if (BuildConfig.FLAVOR != "editor" || BuildConfig.BUILD_TYPE != "dev") {
if (BuildConfig.FLAVOR != "editor" || (BuildConfig.BUILD_TYPE != "dev" && BuildConfig.BUILD_TYPE != "benchmark")) {
return
}
val key = Pair(scope, label)
Expand All @@ -108,7 +108,7 @@ fun endBenchmarkMeasure(scope: String, label: String, dumpBenchmark: Boolean = f
*/
@JvmOverloads
fun dumpBenchmark(fileAccessHandler: FileAccessHandler? = null, filepath: String? = benchmarkFile) {
if (BuildConfig.FLAVOR != "editor" || BuildConfig.BUILD_TYPE != "dev") {
if (BuildConfig.FLAVOR != "editor" || (BuildConfig.BUILD_TYPE != "dev" && BuildConfig.BUILD_TYPE != "benchmark")) {
return
}
if (!useBenchmark || benchmarkTracker.isEmpty()) {
Expand Down
2 changes: 2 additions & 0 deletions platform/android/java/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pluginManagement {
id 'com.android.asset-pack' version versions.androidGradlePlugin
id 'org.jetbrains.kotlin.android' version versions.kotlinVersion
id 'io.github.gradle-nexus.publish-plugin' version versions.nexusPublishVersion
id 'com.android.test' version versions.androidGradlePlugin
}
repositories {
google()
Expand All @@ -26,3 +27,4 @@ include ':editor'

include ':assetPacks:installTime'
project(':assetPacks:installTime').projectDir = file("app/assetPacks/installTime")
include ':editor:macrobenchmark'

0 comments on commit 27e2e3c

Please sign in to comment.