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:
OneWay committed 2026-10-28 00:14:17 +08:00
1 parent 85ee7d703b
commit 938d920725
9 files changed
+20791 -64

No files matched your search

+7
View File
@@ -67,6 +67,7 @@ plugins {
alias(libs.plugins.kotlin.compose) alias(libs.plugins.kotlin.compose)
alias(libs.plugins.kotlin.serialization) alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.aboutlibraries) alias(libs.plugins.aboutlibraries)
alias(libs.plugins.baselineprofile)
} }
kotlin { kotlin {
@@ -109,6 +110,10 @@ android {
} }
release { release {
if (project.hasProperty("research.debugSign")) {
signingConfig = signingConfigs.getByName("debug")
}
ndk { ndk {
abiFilters.clear() abiFilters.clear()
abiFilters += "arm64-v8a" abiFilters += "arm64-v8a"
@@ -158,6 +163,8 @@ aboutLibraries {
} }
dependencies { dependencies {
baselineProfile(project(":baselineprofile"))
implementation(libs.androidx.core) implementation(libs.androidx.core)
implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.androidx.lifecycle.runtime.ktx)
-64
View File
@@ -1,64 +0,0 @@
HSPLcom/example/research/ReSearchApplication**->**(**)**
HSPLcom/example/research/MainActivity**->**(**)**
HSPLcom/example/research/InitialLaunch**->**(**)**
HSPLcom/example/research/DictionaryStatus**->**(**)**
HSPLcom/example/research/core/**->**(**)**
HSPLcom/example/research/common/ui/**->**(**)**
HSPLcom/example/research/ui/theme/**->**(**)**
HSPLcom/example/research/ui/navigation/**->**(**)**
HSPLcom/example/research/data/local/preferences/**->**(**)**
HSPLcom/example/research/data/parser/ResourceManager**->**(**)**
HSPLcom/example/research/common/util/FileStorageManager**->**(**)**
HSPLcom/example/research/common/util/DateUtils**->**(**)**
HSPLcom/example/research/ui/main/**->**(**)**
HSPLandroidx/paging/**->**(**)**
HSPLcom/example/research/feature/search/**->**(**)**
HSPLcom/example/research/data/paging/**->**(**)**
HSPLcom/example/research/data/parser/IndexSearcher**->**(**)**
HSPLcom/example/research/data/parser/FileSource**->**(**)**
HSPLcom/example/research/data/parser/ResultsLruCache**->**(**)**
HSPLcom/example/research/data/parser/MetadataLruCache**->**(**)**
HSPLcom/example/research/data/parser/SearchRanking**->**(**)**
HSPLcom/example/research/data/parser/SortKey**->**(**)**
HSPLcom/example/research/data/repository/**->**(**)**
HSPLcom/example/research/data/dictzip/DictZipInputStream**->**(**)**
HSPLcom/example/research/data/dictzip/DictZipRandomAccessFile**->**(**)**
HSPLcom/example/research/data/dictzip/DictZipHeader**->**(**)**
HSPLcom/example/research/data/parser/DslArticleReader**->**(**)**
HSPLcom/example/research/data/parser/LruCharsetCache**->**(**)**
HSPLcom/example/research/ui/article/**->**(**)**
HSPLcom/example/research/data/parser/Dsl**->**(**)**
HSPLcom/example/research/data/parser/LineKind**->**(**)**
HSPLcom/example/research/data/parser/ByteTrackingLineReader**->**(**)**
HSPLcom/example/research/data/parser/Utf8Writer**->**(**)**
HSPLcom/example/research/ui/settings/**->**(**)**
HSPLcom/example/research/common/progress/**->**(**)**
HSPLcom/example/research/feature/download/**->**(**)**
HSPLcom/example/research/ui/about/AboutScreenKt**->**(**)**
Lcom/example/research/**
HSPLandroidx/compose/runtime/**->**(**)**
Landroidx/compose/runtime/**
HSPLandroidx/compose/ui/**->**(**)**
Landroidx/compose/ui/**
HSPLandroidx/compose/foundation/**->**(**)**
Landroidx/compose/foundation/**
HSPLandroidx/compose/material3/**->**(**)**
Landroidx/compose/material3/**
HSPLandroidx/compose/animation/**->**(**)**
Landroidx/compose/animation/**
HSPLandroidx/compose/material/**->**(**)**
Landroidx/compose/material/**
HSPLandroidx/lifecycle/**->**(**)**
Landroidx/lifecycle/**
HSPLandroidx/activity/compose/**->**(**)**
Landroidx/activity/compose/**
HSPLkotlinx/coroutines/**->**(**)**
Lkotlinx/coroutines/**
Landroidx/paging/**
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+31
View File
@@ -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)
}
@@ -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
}
}
+1
View File
@@ -1,6 +1,7 @@
plugins { plugins {
alias(libs.plugins.aboutlibraries) apply false alias(libs.plugins.aboutlibraries) apply false
alias(libs.plugins.android.application) apply false alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.test) apply false
alias(libs.plugins.kotlin.compose) apply false alias(libs.plugins.kotlin.compose) apply false
id("io.github.ben-manes.versions") version "0.66.0" id("io.github.ben-manes.versions") version "0.66.0"
} }
+9
View File
@@ -2,21 +2,26 @@
aboutlibraries = "15.3.0" aboutlibraries = "15.3.0"
activity_compose = "1.13.0" activity_compose = "1.13.0"
agp = "9.4.1" agp = "9.4.1"
baselineprofile = "1.5.0"
benchmark_macro = "1.5.0"
compose_bom = "2026.09.00" compose_bom = "2026.09.00"
core = "1.19.1" core = "1.19.1"
datastore_preferences = "1.3.0" datastore_preferences = "1.3.0"
documentfile = "1.1.0" documentfile = "1.1.0"
junit = "1.3.0"
kotlin = "2.4.21" kotlin = "2.4.21"
lifecycle_runtime_ktx = "2.11.0" lifecycle_runtime_ktx = "2.11.0"
material3 = "1.5.0-beta02" material3 = "1.5.0-beta02"
okhttp = "5.5.1" okhttp = "5.5.1"
paging = "3.5.3" paging = "3.5.3"
profileinstaller = "1.4.1" profileinstaller = "1.4.1"
uiautomator = "2.4.0"
kotlinx_coroutines = "1.11.0" kotlinx_coroutines = "1.11.0"
kotlinx_serialization = "1.12.0" kotlinx_serialization = "1.12.0"
[libraries] [libraries]
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activity_compose" } androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activity_compose" }
androidx-benchmark-macro-junit4 = { module = "androidx.benchmark:benchmark-macro-junit4", version.ref = "benchmark_macro" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "compose_bom" } androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "compose_bom" }
androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3", version.ref = "material3" } androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3", version.ref = "material3" }
androidx-compose-material3-adaptive-layout = { group = "androidx.compose.material3.adaptive", name = "adaptive-layout" } androidx-compose-material3-adaptive-layout = { group = "androidx.compose.material3.adaptive", name = "adaptive-layout" }
@@ -32,6 +37,8 @@ androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-ru
androidx-paging-runtime = { module = "androidx.paging:paging-runtime", version.ref = "paging" } androidx-paging-runtime = { module = "androidx.paging:paging-runtime", version.ref = "paging" }
androidx-paging-compose = { module = "androidx.paging:paging-compose", version.ref = "paging" } androidx-paging-compose = { module = "androidx.paging:paging-compose", version.ref = "paging" }
androidx-profileinstaller = { module = "androidx.profileinstaller:profileinstaller", version.ref = "profileinstaller" } androidx-profileinstaller = { module = "androidx.profileinstaller:profileinstaller", version.ref = "profileinstaller" }
androidx-test-junit = { module = "androidx.test.ext:junit", version.ref = "junit" }
androidx-uiautomator = { module = "androidx.test.uiautomator:uiautomator", version.ref = "uiautomator" }
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" } okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx_coroutines" } kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx_coroutines" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx_serialization" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx_serialization" }
@@ -46,5 +53,7 @@ compose = [
[plugins] [plugins]
aboutlibraries = { id = "com.mikepenz.aboutlibraries.plugin.android", version.ref = "aboutlibraries" } aboutlibraries = { id = "com.mikepenz.aboutlibraries.plugin.android", version.ref = "aboutlibraries" }
android-application = { id = "com.android.application", version.ref = "agp" } android-application = { id = "com.android.application", version.ref = "agp" }
android-test = { id = "com.android.test", version.ref = "agp" }
baselineprofile = { id = "androidx.baselineprofile", version.ref = "baselineprofile" }
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
+1
View File
@@ -23,3 +23,4 @@ dependencyResolutionManagement {
rootProject.name = "ReSearch" rootProject.name = "ReSearch"
include(":app") include(":app")
include(":baselineprofile")