Skip to content

Commit

Permalink
Add perf example for examining debug.emergetools.is_perf (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
chromy authored Aug 7, 2024
1 parent 189c76b commit 9f3f2af
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.emergetools.performance.sample

import android.annotation.SuppressLint
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Bundle
import android.os.Trace
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
Expand All @@ -18,6 +20,16 @@ class MainActivity : ComponentActivity() {
}
super.onCreate(savedInstanceState)

// You should not normally need to detect if you are running under Emergetools
// perf test infra however it can be helpful in some situations. In these cases
// the recommend method is to check the "debug.emergetools.is_perf" build
// property as follows:
if (isPerfTest()) {
Log.i("PerformanceSample", "Running an Emergetools perf test")
} else {
Log.i("PerformanceSample", "Not running an Emergetools perftest")
}

setContent {
Column {
TextRowWithIcon(
Expand All @@ -32,4 +44,18 @@ class MainActivity : ComponentActivity() {
Trace.endAsyncSection("MainActivity.onCreateAsync", 1)
}
}

@SuppressLint("PrivateApi")
@Suppress("SwallowedException", "TooGenericExceptionCaught")
fun isPerfTest(): Boolean {
var value: String? = null
val key = "debug.emergetools.is_perf"
try {
value = Class.forName("android.os.SystemProperties")
.getMethod("get", String::class.java).invoke(null, key) as String
} catch (e: Exception) {
return false
}
return value == "1"
}
}

0 comments on commit 9f3f2af

Please sign in to comment.