perf(profile): generate baseline and startup profiles
Add a reproducible generator for startup, search, article opening and scrolling, search history, the settings and About screens and their dialogs. Replace the hand-written profile with generated release profiles.
This commit is contained in:
1 parent
85ee7d703b
commit
938d920725
9 files changed
+20791
-64
No files matched your search
@@ -0,0 +1,31 @@
|
||||
plugins {
|
||||
alias(libs.plugins.android.test)
|
||||
alias(libs.plugins.baselineprofile)
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "com.example.research.baselineprofile"
|
||||
compileSdk = project.property("compileSdk").toString().toInt()
|
||||
targetProjectPath = ":app"
|
||||
|
||||
defaultConfig {
|
||||
minSdk = project.property("minSdk").toString().toInt()
|
||||
targetSdk = project.property("targetSdk").toString().toInt()
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_21
|
||||
targetCompatibility = JavaVersion.VERSION_21
|
||||
}
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.androidx.test.junit)
|
||||
implementation(libs.androidx.uiautomator)
|
||||
implementation(libs.androidx.benchmark.macro.junit4)
|
||||
}
|
||||
+332
@@ -0,0 +1,332 @@
|
||||
package com.example.research.baselineprofile
|
||||
|
||||
import android.util.Base64
|
||||
import androidx.benchmark.macro.junit4.BaselineProfileRule
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.uiautomator.By
|
||||
import androidx.test.uiautomator.Direction
|
||||
import androidx.test.uiautomator.UiDevice
|
||||
import androidx.test.uiautomator.UiObject2
|
||||
import androidx.test.uiautomator.Until
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.util.zip.GZIPInputStream
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class BaselineProfileGenerator {
|
||||
@get:Rule
|
||||
val rule = BaselineProfileRule()
|
||||
|
||||
@Test
|
||||
fun generate() = rule.collect(
|
||||
packageName = "com.example.research",
|
||||
includeInStartupProfile = true,
|
||||
) {
|
||||
device.executeShellCommand(
|
||||
"pm grant com.example.research android.permission.POST_NOTIFICATIONS",
|
||||
)
|
||||
val dictionary = loadProfileDictionary(device)
|
||||
|
||||
startActivityAndWait()
|
||||
|
||||
if (device.wait(Until.hasObject(By.res("settings_screen")), SCREEN_TIMEOUT_MS)) {
|
||||
importDictionary(device, dictionary.fileName)
|
||||
startActivityAndWait()
|
||||
}
|
||||
|
||||
device.waitForObject("search_field", SEARCH_TIMEOUT_MS)
|
||||
|
||||
// Search and read an article.
|
||||
searchAndOpenFirstResult(device, dictionary.query)
|
||||
device.findObject(By.res("article_content")).scroll(Direction.DOWN, 1.0f)
|
||||
device.waitForIdle()
|
||||
device.pressBack()
|
||||
device.waitForObject("search_result")
|
||||
|
||||
// Follow a cross-reference target. It drives the same SearchAndLoadArticle
|
||||
// path an in-article [ref] link uses and adds a second history entry.
|
||||
dictionary.refTarget?.let { refTarget ->
|
||||
searchAndOpenFirstResult(device, refTarget)
|
||||
device.pressBack()
|
||||
device.waitForObject("search_result")
|
||||
}
|
||||
|
||||
// Search history: apply an entry, then remove and clear entries.
|
||||
exerciseSearchHistory(device)
|
||||
|
||||
// Settings: URL source validation error.
|
||||
device.findObject(By.res("settings_button")).click()
|
||||
device.waitForObject("settings_screen")
|
||||
exerciseUrlSourceDialog(device)
|
||||
|
||||
// About: expand a library and open its license dialog.
|
||||
device.findObject(By.res("about_card")).click()
|
||||
device.waitForObject("about_libraries_list")
|
||||
exerciseAboutLicense(device)
|
||||
device.pressBack()
|
||||
device.waitForObject("settings_screen")
|
||||
device.pressBack()
|
||||
device.waitForObject("search_field")
|
||||
}
|
||||
|
||||
/** Runs a search and opens its first result, waiting for the article. */
|
||||
private fun searchAndOpenFirstResult(device: UiDevice, query: String) {
|
||||
device.findObject(By.res("search_field")).text = query
|
||||
device.waitForObject("search_result")
|
||||
val result = device.wait(
|
||||
Until.findObject(By.res("search_result_row_0")),
|
||||
TIMEOUT_MS,
|
||||
) ?: error("Search result is not shown")
|
||||
result.click()
|
||||
device.waitForObject("article_content")
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the newest history entry through the sheet, opens its article
|
||||
* and then edits the history: removes one entry and clears the rest.
|
||||
*/
|
||||
private fun exerciseSearchHistory(device: UiDevice) {
|
||||
device.findObject(By.res("history_button")).click()
|
||||
// The sheet itself has no resource-id tag; its list does.
|
||||
device.waitForObject("search_history")
|
||||
device.waitForObject("search_history_entry")
|
||||
|
||||
// On phones an entry click only fills the search field; the search
|
||||
// runs when its first result is clicked.
|
||||
device.findObject(By.res("search_history_entry")).click()
|
||||
device.waitForObject("search_result")
|
||||
val result = device.wait(
|
||||
Until.findObject(By.res("search_result_row_0")),
|
||||
TIMEOUT_MS,
|
||||
) ?: error("History search result is not shown")
|
||||
result.click()
|
||||
device.waitForObject("article_content")
|
||||
device.pressBack()
|
||||
device.waitForObject("search_result")
|
||||
|
||||
device.findObject(By.res("history_button")).click()
|
||||
device.waitForObject("search_history")
|
||||
device.findObject(By.res("remove_history_entry")).click()
|
||||
device.waitForIdle()
|
||||
// Removing the last entry closes the sheet by itself; with more entries
|
||||
// left the sheet stays open and can be cleared.
|
||||
device.wait(Until.findObject(By.res("clear_history")), SHORT_TIMEOUT_MS)?.click()
|
||||
device.wait(Until.gone(By.res("search_history")), TIMEOUT_MS)
|
||||
}
|
||||
|
||||
/** Opens the URL source dialog and confirms the protocol validation. */
|
||||
private fun exerciseUrlSourceDialog(device: UiDevice) {
|
||||
device.findObject(By.res("add_source_button")).click()
|
||||
device.waitForObject("url_input_field")
|
||||
device.findObject(By.res("url_input_field")).text = INVALID_SOURCE_URL
|
||||
device.findObject(By.res("add_confirm_button")).click()
|
||||
device.waitForText(INVALID_PROTOCOL_TEXT)
|
||||
device.pressBack()
|
||||
device.waitForObject("settings_screen")
|
||||
}
|
||||
|
||||
/** Expands the first library row and opens its license dialog if present. */
|
||||
private fun exerciseAboutLicense(device: UiDevice) {
|
||||
device.wait(
|
||||
Until.findObject(By.res("about_library_row")),
|
||||
TIMEOUT_MS,
|
||||
) ?: error("Library list is not shown")
|
||||
device.findObject(By.res("about_library_row")).click()
|
||||
device.waitForObject("about_library_expanded_actions")
|
||||
device.wait(Until.findObject(By.res("about_license_button")), SHORT_TIMEOUT_MS)?.let { license ->
|
||||
license.click()
|
||||
device.waitForText(CLOSE_BUTTON_TEXT)
|
||||
device.findObject(By.text(CLOSE_BUTTON_TEXT)).click()
|
||||
device.waitForObject("about_libraries_list")
|
||||
}
|
||||
device.waitForIdle()
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports a compressed dictionary through the real UI flow: opens the
|
||||
* system file picker from the settings screen, selects the archive and
|
||||
* waits until the import pipeline (extraction, compression, indexing)
|
||||
* finishes and the dictionary appears in the list.
|
||||
*/
|
||||
private fun importDictionary(device: UiDevice, fileName: String) {
|
||||
device.findObject(By.text(IMPORT_BUTTON_TEXT)).click()
|
||||
pickDictionaryFile(device, fileName)
|
||||
device.waitForGone(By.text(EMPTY_DICTIONARIES_TEXT), PIPELINE_TIMEOUT_MS)
|
||||
}
|
||||
|
||||
/**
|
||||
* Drives the system file picker (DocumentsUI) opened via
|
||||
* ActivityResultContracts.OpenMultipleDocuments and selects the
|
||||
* compressed dictionary archive from the Downloads root.
|
||||
*/
|
||||
private fun pickDictionaryFile(device: UiDevice, fileName: String) {
|
||||
val pickerObject: UiObject2 = device.wait(
|
||||
Until.findObject(By.text(fileName)),
|
||||
PICKER_TIMEOUT_MS,
|
||||
) ?: run {
|
||||
device.findObject(By.desc(SHOW_ROOTS_DESCRIPTION)).click()
|
||||
device.wait(Until.findObject(By.text(DOWNLOADS_ROOT)), TIMEOUT_MS)?.click()
|
||||
device.wait(
|
||||
Until.findObject(By.text(fileName)),
|
||||
PICKER_TIMEOUT_MS,
|
||||
) ?: error("Dictionary file $fileName is not found in the picker")
|
||||
}
|
||||
pickerObject.click()
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the first .dsl.gz archive from the shared Download folder and
|
||||
* extracts a search query from its first article, so the scenario does
|
||||
* not depend on a specific dictionary.
|
||||
*/
|
||||
private fun loadProfileDictionary(device: UiDevice): ProfileDictionary {
|
||||
val fileName = device.executeShellCommand("ls $DOWNLOAD_DIR")
|
||||
.lineSequence()
|
||||
.map(String::trim)
|
||||
.filter { it.endsWith(GZ_SUFFIX) }
|
||||
.firstOrNull()
|
||||
?: error("No $GZ_SUFFIX archives available in $DOWNLOAD_DIR")
|
||||
val archiveBytes = Base64.decode(
|
||||
device.executeShellCommand("base64 $DOWNLOAD_DIR/$fileName")
|
||||
.filter { it != '\n' && it != '\r' },
|
||||
Base64.DEFAULT,
|
||||
)
|
||||
val dictionaryBytes = ungzip(archiveBytes)
|
||||
val head = decodeWithBom(
|
||||
dictionaryBytes.copyOfRange(0, minOf(HEAD_BYTES, dictionaryBytes.size)),
|
||||
)
|
||||
|
||||
var entry: String? = null
|
||||
var query: String? = null
|
||||
var refTarget: String? = null
|
||||
for (line in head.lineSequence()) {
|
||||
if (line.isNotBlank() && line.first().isWhitespace()) {
|
||||
if (query == null) {
|
||||
val headword = entry?.let(::firstHeadword).orEmpty()
|
||||
if (headword.isNotEmpty()) query = headword
|
||||
} else {
|
||||
refTarget = refTarget ?: firstReference(line)
|
||||
if (refTarget != null) break
|
||||
}
|
||||
}
|
||||
val value = line.trim()
|
||||
if (value.isNotBlank() && !value.startsWith(DSL_DIRECTIVE)) {
|
||||
entry = value
|
||||
}
|
||||
}
|
||||
if (query == null) error("No dictionary entry found in $fileName")
|
||||
return ProfileDictionary(fileName, query, refTarget)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the plain target of the first [ref]...[/ref] pair on the line,
|
||||
* dropping any nested DSL tags, so the scenario can follow a real
|
||||
* cross-reference instead of relying on a hardcoded word.
|
||||
*/
|
||||
private fun firstReference(line: String): String? {
|
||||
val openIndex = line.indexOf(REF_OPEN_TAG)
|
||||
if (openIndex == -1) return null
|
||||
val start = openIndex + REF_OPEN_TAG.length
|
||||
val end = line.indexOf(REF_CLOSE_TAG, start)
|
||||
if (end == -1) return null
|
||||
val target = line
|
||||
.substring(start, end)
|
||||
.replace(NESTED_TAG_REGEX, "")
|
||||
.trim()
|
||||
return target.ifEmpty { null }
|
||||
}
|
||||
|
||||
private fun ungzip(bytes: ByteArray): ByteArray {
|
||||
val output = ByteArrayOutputStream()
|
||||
GZIPInputStream(ByteArrayInputStream(bytes)).use { input ->
|
||||
val buffer = ByteArray(8192)
|
||||
while (true) {
|
||||
val read = input.read(buffer)
|
||||
if (read == -1) break
|
||||
output.write(buffer, 0, read)
|
||||
}
|
||||
}
|
||||
return output.toByteArray()
|
||||
}
|
||||
|
||||
private fun decodeWithBom(bytes: ByteArray): String = when {
|
||||
bytes.size >= UTF8_BOM.size &&
|
||||
bytes.copyOfRange(0, UTF8_BOM.size).contentEquals(UTF8_BOM) ->
|
||||
String(bytes, UTF8_BOM.size, bytes.size - UTF8_BOM.size, Charsets.UTF_8)
|
||||
|
||||
bytes.size >= UTF16_BOM.size &&
|
||||
bytes[0] == UTF16_BOM[0] && bytes[1] == UTF16_BOM[1] ->
|
||||
String(bytes, UTF16_BOM.size, bytes.size - UTF16_BOM.size, Charsets.UTF_16LE)
|
||||
|
||||
bytes.size >= UTF16_BOM.size &&
|
||||
bytes[0] == UTF16_BOM[1] && bytes[1] == UTF16_BOM[0] ->
|
||||
String(bytes, UTF16_BOM.size, bytes.size - UTF16_BOM.size, Charsets.UTF_16BE)
|
||||
|
||||
bytes.size >= 2 && bytes[0] == 0.toByte() ->
|
||||
String(bytes, Charsets.UTF_16BE)
|
||||
|
||||
bytes.size >= 2 && bytes[1] == 0.toByte() ->
|
||||
String(bytes, Charsets.UTF_16LE)
|
||||
|
||||
else -> String(bytes, Charsets.UTF_8)
|
||||
}
|
||||
|
||||
private fun firstHeadword(line: String): String =
|
||||
line
|
||||
.substringBefore(",")
|
||||
.substringBefore(",")
|
||||
.trim()
|
||||
|
||||
private fun UiDevice.waitForObject(resourceId: String, timeoutMs: Long = TIMEOUT_MS) {
|
||||
check(wait(Until.hasObject(By.res(resourceId)), timeoutMs)) {
|
||||
"Timed out waiting for $resourceId"
|
||||
}
|
||||
}
|
||||
|
||||
private fun UiDevice.waitForText(text: String, timeoutMs: Long = TIMEOUT_MS) {
|
||||
check(wait(Until.hasObject(By.text(text)), timeoutMs)) {
|
||||
"Timed out waiting for text $text"
|
||||
}
|
||||
}
|
||||
|
||||
private fun UiDevice.waitForGone(selector: androidx.test.uiautomator.BySelector, timeoutMs: Long) {
|
||||
check(wait(Until.gone(selector), timeoutMs)) {
|
||||
"Timed out waiting for $selector to disappear"
|
||||
}
|
||||
}
|
||||
|
||||
private data class ProfileDictionary(
|
||||
val fileName: String,
|
||||
val query: String,
|
||||
val refTarget: String?,
|
||||
)
|
||||
|
||||
private companion object {
|
||||
val UTF8_BOM = byteArrayOf(0xEF.toByte(), 0xBB.toByte(), 0xBF.toByte())
|
||||
val UTF16_BOM = byteArrayOf(0xFE.toByte(), 0xFF.toByte())
|
||||
val NESTED_TAG_REGEX = Regex("\\[[^\\]]*\\]")
|
||||
|
||||
const val DOWNLOAD_DIR = "/sdcard/Download"
|
||||
const val GZ_SUFFIX = ".dsl.gz"
|
||||
const val HEAD_BYTES = 16384
|
||||
const val DSL_DIRECTIVE = "#"
|
||||
const val REF_OPEN_TAG = "[ref]"
|
||||
const val REF_CLOSE_TAG = "[/ref]"
|
||||
const val IMPORT_BUTTON_TEXT = "Select files"
|
||||
const val SHOW_ROOTS_DESCRIPTION = "Show roots"
|
||||
const val DOWNLOADS_ROOT = "Downloads"
|
||||
const val EMPTY_DICTIONARIES_TEXT = "No dictionaries downloaded yet"
|
||||
const val INVALID_SOURCE_URL = "ftp://example.com"
|
||||
const val INVALID_PROTOCOL_TEXT = "Must start with https://"
|
||||
const val CLOSE_BUTTON_TEXT = "Close"
|
||||
const val TIMEOUT_MS = 30_000L
|
||||
const val SCREEN_TIMEOUT_MS = 10_000L
|
||||
const val SHORT_TIMEOUT_MS = 10_000L
|
||||
const val PICKER_TIMEOUT_MS = 30_000L
|
||||
const val SEARCH_TIMEOUT_MS = 300_000L
|
||||
const val PIPELINE_TIMEOUT_MS = 300_000L
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user