From 53adde3a95b58bd45896dbc8eaf47304e108d8bb Mon Sep 17 00:00:00 2001 From: OneWay Date: Sat, 7 Nov 2026 22:03:00 +0800 Subject: [PATCH] feat(profile): pass query and reference target through runner arguments --- baselineprofile/build.gradle.kts | 6 ++++ .../BaselineProfileGenerator.kt | 36 +++++++++++++------ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/baselineprofile/build.gradle.kts b/baselineprofile/build.gradle.kts index c38c013..bcb82a4 100644 --- a/baselineprofile/build.gradle.kts +++ b/baselineprofile/build.gradle.kts @@ -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 { diff --git a/baselineprofile/src/main/java/com/example/research/baselineprofile/BaselineProfileGenerator.kt b/baselineprofile/src/main/java/com/example/research/baselineprofile/BaselineProfileGenerator.kt index 7cbb61e..94ecbb2 100644 --- a/baselineprofile/src/main/java/com/example/research/baselineprofile/BaselineProfileGenerator.kt +++ b/baselineprofile/src/main/java/com/example/research/baselineprofile/BaselineProfileGenerator.kt @@ -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 ->