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
+31 -9

No files matched your search

+6
View File
@@ -12,6 +12,12 @@ android {
minSdk = project.property("minSdk").toString().toInt() minSdk = project.property("minSdk").toString().toInt()
targetSdk = project.property("targetSdk").toString().toInt() targetSdk = project.property("targetSdk").toString().toInt()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
project.findProperty("profileQuery")?.let {
testInstrumentationRunnerArguments["query"] = it.toString()
}
project.findProperty("profileRefTarget")?.let {
testInstrumentationRunnerArguments["refTarget"] = it.toString()
}
} }
compileOptions { compileOptions {
@@ -3,6 +3,7 @@ package com.example.research.baselineprofile
import android.util.Base64 import android.util.Base64
import androidx.benchmark.macro.junit4.BaselineProfileRule import androidx.benchmark.macro.junit4.BaselineProfileRule
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By import androidx.test.uiautomator.By
import androidx.test.uiautomator.Direction import androidx.test.uiautomator.Direction
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
@@ -180,7 +181,9 @@ class BaselineProfileGenerator {
/** /**
* Reads the first .dsl.gz archive from the shared Download folder and * Reads the first .dsl.gz archive from the shared Download folder and
* extracts a search query from its first article, so the scenario does * 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 { private fun loadProfileDictionary(device: UiDevice): ProfileDictionary {
val fileName = device.executeShellCommand("ls $DOWNLOAD_DIR") val fileName = device.executeShellCommand("ls $DOWNLOAD_DIR")
@@ -188,15 +191,18 @@ class BaselineProfileGenerator {
.map(String::trim) .map(String::trim)
.firstOrNull { it.endsWith(GZ_SUFFIX) } .firstOrNull { it.endsWith(GZ_SUFFIX) }
?: error("No $GZ_SUFFIX archives available in $DOWNLOAD_DIR") ?: error("No $GZ_SUFFIX archives available in $DOWNLOAD_DIR")
val archiveBytes = Base64.decode( val args = InstrumentationRegistry.getArguments()
device.executeShellCommand("base64 $DOWNLOAD_DIR/$fileName") val argQuery = args.getString("query")?.takeIf { it.isNotEmpty() }
.filter { it != '\n' && it != '\r' }, val argRefTarget = args.getString("refTarget")?.takeIf { it.isNotEmpty() }
Base64.DEFAULT, if (argQuery != null) return ProfileDictionary(fileName, argQuery, argRefTarget)
) val head = try {
val dictionaryBytes = ungzip(archiveBytes) decodeWithBom(readGzipHead(device, "$DOWNLOAD_DIR/$fileName", HEAD_BYTES))
val head = decodeWithBom( } catch (e: OutOfMemoryError) {
dictionaryBytes.copyOfRange(0, minOf(HEAD_BYTES, dictionaryBytes.size)), error(
"Staged archive does not fit the instrumentation heap; " +
"pass the query and refTarget runner arguments",
) )
}
var entry: String? = null var entry: String? = null
var query: String? = null var query: String? = null
@@ -238,6 +244,16 @@ class BaselineProfileGenerator {
return target.ifEmpty { null } 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 { private fun ungzip(bytes: ByteArray): ByteArray {
val output = ByteArrayOutputStream() val output = ByteArrayOutputStream()
GZIPInputStream(ByteArrayInputStream(bytes)).use { input -> GZIPInputStream(ByteArrayInputStream(bytes)).use { input ->