feat(profile): pass query and reference target through runner arguments

This commit is contained in:
OneWay committed 2026-11-07 22:03:00 +08:00
1 parent 8f16160794
commit 53adde3a95
2 files changed
+32 -10

No files matched your search

+6
View File
@@ -12,6 +12,12 @@ android {
minSdk = project.property("minSdk").toString().toInt()
targetSdk = project.property("targetSdk").toString().toInt()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
project.findProperty("profileQuery")?.let {
testInstrumentationRunnerArguments["query"] = it.toString()
}
project.findProperty("profileRefTarget")?.let {
testInstrumentationRunnerArguments["refTarget"] = it.toString()
}
}
compileOptions {
@@ -3,6 +3,7 @@ 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.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Direction
import androidx.test.uiautomator.UiDevice
@@ -180,7 +181,9 @@ class BaselineProfileGenerator {
/**
* 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.
* not depend on a specific dictionary. The `query` and `refTarget`
* runner arguments override the head parsing, which cannot read heavy
* archives because the UiAutomation shell output is size-capped.
*/
private fun loadProfileDictionary(device: UiDevice): ProfileDictionary {
val fileName = device.executeShellCommand("ls $DOWNLOAD_DIR")
@@ -188,15 +191,18 @@ class BaselineProfileGenerator {
.map(String::trim)
.firstOrNull { it.endsWith(GZ_SUFFIX) }
?: 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)),
)
val args = InstrumentationRegistry.getArguments()
val argQuery = args.getString("query")?.takeIf { it.isNotEmpty() }
val argRefTarget = args.getString("refTarget")?.takeIf { it.isNotEmpty() }
if (argQuery != null) return ProfileDictionary(fileName, argQuery, argRefTarget)
val head = try {
decodeWithBom(readGzipHead(device, "$DOWNLOAD_DIR/$fileName", HEAD_BYTES))
} catch (e: OutOfMemoryError) {
error(
"Staged archive does not fit the instrumentation heap; " +
"pass the query and refTarget runner arguments",
)
}
var entry: String? = null
var query: String? = null
@@ -238,6 +244,16 @@ class BaselineProfileGenerator {
return target.ifEmpty { null }
}
private fun readGzipHead(device: UiDevice, remotePath: String, textBytes: Int): ByteArray {
val archiveBytes = Base64.decode(
device.executeShellCommand("base64 $remotePath")
.filter { it != '\n' && it != '\r' },
Base64.DEFAULT,
)
val dictionaryBytes = ungzip(archiveBytes)
return dictionaryBytes.copyOfRange(0, minOf(textBytes, dictionaryBytes.size))
}
private fun ungzip(bytes: ByteArray): ByteArray {
val output = ByteArrayOutputStream()
GZIPInputStream(ByteArrayInputStream(bytes)).use { input ->