Skip to content

Commit

Permalink
Merge pull request #88 from lazzzis/ci-benchmark-2
Browse files Browse the repository at this point in the history
test: setup benchmark test
  • Loading branch information
whitescent authored Mar 5, 2024
2 parents 3250b1f + c08c9c0 commit 47d9219
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 34 deletions.
30 changes: 29 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,30 @@ jobs:
- name: Set repository name as env variable
run: echo "repository_name=$(echo '${{ github.repository }}' | awk -F '/' '{print $2}')" >> $GITHUB_ENV

- name: Write value to Properties-file
env:
MASTODON_USERNAME: ${{ secrets.MASTODON_USERNAME }}
MASTODON_PASSWORD: ${{ secrets.MASTODON_PASSWORD }}
MASTODON_SITE: ${{ secrets.MASTODON_SITE }}
run: |
echo "MASTODON_USERNAME=$MASTODON_USERNAME" >> ./app/benchmark/mastodon.properties
echo "MASTODON_PASSWORD=$MASTODON_PASSWORD" >> ./app/benchmark/mastodon.properties
echo "MASTODON_SITE=$MASTODON_SITE" >> ./app/benchmark/mastodon.properties
- name: Set Up JDK
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: "17"
cache: "gradle"

# Required for Setting Up Benchmarks
# - name: Enable KVM
# run: |
# echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
# sudo udevadm control --reload-rules
# sudo udevadm trigger --name-match=kvm

- name: Change wrapper permissions
run: chmod +x ./gradlew

Expand All @@ -47,4 +64,15 @@ jobs:
- name: Run gradle tests
run: ./gradlew app:testDebugUnitTest

# TODO: setup benchmarks
# Setup benchmarks
# https://github.com/ReactiveCircus/android-emulator-runner/issues/161#issuecomment-1248757074
# - name: Run benchmarks
# env:
# MASTODON_USERNAME: ${{ secrets.MASTODON_USERNAME }}
# MASTODON_PASSWORD: ${{ secrets.MASTODON_PASSWORD }}
# MASTODON_SITE: ${{ secrets.MASTODON_SITE }}
# uses: reactivecircus/android-emulator-runner@v2
# with:
# api-level: 34
# # restart adb to fix broken emulator
# script: adb kill-server && adb start-server && ./gradlew app:benchmark:connectedBenchmarkAndroidTest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ local.properties
/app/release
/app/schemas/com.github.whitescent.mastify.timeline.TestAppDatabase/*
google-services.json
/app/benchmark/mastodon.properties
15 changes: 15 additions & 0 deletions app/benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
* see <http://www.gnu.org/licenses>.
*/

import java.io.FileInputStream
import java.util.Properties

val mastodonProperties = Properties().apply {
load(FileInputStream(project.file("mastodon.properties")))
}

@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
id("com.android.test")
Expand All @@ -41,6 +48,10 @@ android {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
buildConfig = true
}

buildTypes {
// This benchmark buildType is used for benchmarking, and should function like your
// release build (for example, with minification on). It"s signed with a debug key
Expand All @@ -49,6 +60,10 @@ android {
isDebuggable = true
signingConfig = getByName("debug").signingConfig
matchingFallbacks += listOf("release")

buildConfigField("String", "MASTODON_USERNAME", "\"${mastodonProperties["MASTODON_USERNAME"]}\"")
buildConfigField("String", "MASTODON_PASSWORD", "\"${mastodonProperties["MASTODON_PASSWORD"]}\"")
buildConfigField("String", "MASTODON_SITE", "\"${mastodonProperties["MASTODON_SITE"]}\"")
}
}

Expand Down
4 changes: 4 additions & 0 deletions app/benchmark/mastodon-sample.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# rename to mastodon.properties before running benchmarks
MASTODON_USERNAME=username
MASTODON_PASSWORD=password
MASTODON_SITE=example.com
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Direction
import androidx.test.uiautomator.Until
import com.github.whitescent.benchmark.BuildConfig
import com.github.whitescent.benchmark.utils.isLoggedIn
import com.github.whitescent.benchmark.utils.waitForObject
import org.junit.Rule
Expand All @@ -39,39 +40,52 @@ class BaselineProfileGenerator {

@RequiresApi(Build.VERSION_CODES.P)
@Test
fun generate() = baselineProfileRule.collect(
packageName = "com.github.whitescent.mastify",
maxIterations = 1,
stableIterations = 1
) {
startActivityAndWait()
fun generate() {
val username = BuildConfig.MASTODON_USERNAME
val password = BuildConfig.MASTODON_PASSWORD
val site = BuildConfig.MASTODON_SITE

if (!device.isLoggedIn) {
val loginInput = device.findObject(By.res("domain input"))
baselineProfileRule.collect(
packageName = "com.github.whitescent.mastify",
maxIterations = 1,
stableIterations = 1
) {
startActivityAndWait()

loginInput.text = "m.cmx.im"
if (!device.isLoggedIn) {
val loginInput = device.findObject(By.res("domain input"))

device.waitForObject(By.res("login button"), 5000L)
loginInput.text = site

device.performActionAndWait(
{ device.findObject(By.res("login button")).click() },
Until.newWindow(),
2000
)
device.waitForObject(By.res("login button"), 5000L)

device.waitForObject(By.text("同意授权"), 5000L)
device.findObject(By.text("同意授权")).click()
device.performActionAndWait(
{ device.findObject(By.res("login button")).click() },
Until.newWindow(),
2000
)

device.waitForObject(By.res("home timeline"), 5000L)
}
if (device.hasObject(By.hint("电子邮件地址"))) {
device.waitForObject(By.hint("电子邮件地址"), 5000L).text = username
device.waitForObject(By.hint("密码"), 5000L).text = password
device.findObject(By.text("登录")).click()
}

val column = device.findObject(By.res("home timeline"))

repeat(20) {
column.swipe(Direction.UP, 1f)
}
repeat(10) {
column.swipe(Direction.DOWN, 1f)
device.waitForObject(By.text("同意授权"), 5000L)
device.findObject(By.text("同意授权")).click()

device.waitForObject(By.res("home timeline"), 5000L)
}

val column = device.findObject(By.res("home timeline"))

repeat(20) {
column.swipe(Direction.UP, 1f)
}
repeat(10) {
column.swipe(Direction.DOWN, 1f)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,36 @@

package com.github.whitescent.benchmark.startup

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.benchmark.macro.StartupMode
import androidx.benchmark.macro.StartupTimingMetric
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Direction
import androidx.test.uiautomator.Until
import com.github.whitescent.benchmark.BuildConfig
import com.github.whitescent.benchmark.utils.isLoggedIn
import com.github.whitescent.benchmark.utils.waitForObject
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

// issue:java.lang.IllegalArgumentException: Unable to read any metrics during benchmark
// (metric list: [androidx.benchmark.macro.StartupTimingMetric@849c8a0]
// https://github.com/android/performance-samples/issues/268

@RunWith(AndroidJUnit4::class)
class StartupBenchmark {

@get:Rule
val benchmarkRule = MacrobenchmarkRule()

// By.hint requires O
@RequiresApi(Build.VERSION_CODES.O)
@Test
fun startBenchmark() {
val username = BuildConfig.MASTODON_USERNAME
val password = BuildConfig.MASTODON_PASSWORD
val site = BuildConfig.MASTODON_SITE

benchmarkRule.measureRepeated(
packageName = "com.github.whitescent.mastify",
metrics = listOf(StartupTimingMetric()),
Expand All @@ -53,7 +58,7 @@ class StartupBenchmark {
if (!device.isLoggedIn) {
val loginInput = device.findObject(By.res("domain input"))

loginInput.text = "m.cmx.im"
loginInput.text = site

device.waitForObject(By.res("login button"), 5000L)

Expand All @@ -63,6 +68,13 @@ class StartupBenchmark {
2000
)

if (device.hasObject(By.hint("电子邮件地址"))) {
device.waitForObject(By.hint("电子邮件地址"), 5000L).text = username
device.waitForObject(By.hint("密码"), 5000L).text = password
device.findObject(By.text("登录")).click()
}


device.waitForObject(By.text("同意授权"), 5000L)
device.findObject(By.text("同意授权")).click()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fun Login(
disabledContainerColor = Color.Gray.copy(.5f),
disabledContentColor = Color.White.copy(.5f)
),
modifier = Modifier.fillMaxWidth(),
modifier = Modifier.fillMaxWidth().testTag("login button"),
enabled = !viewModel.instanceLocalError
) {
when (state.loginStatus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTagsAsResourceId
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.github.whitescent.mastify.paging.factory.UnreadEvent
import com.github.whitescent.mastify.screen.NavGraphs
Expand Down Expand Up @@ -64,7 +67,7 @@ import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.launch

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterialNavigationApi::class, ExperimentalAnimationApi::class)
@OptIn(ExperimentalMaterialNavigationApi::class, ExperimentalAnimationApi::class, ExperimentalComposeUiApi::class)
@Composable
fun AppScaffold(
startRoute: Route,
Expand Down Expand Up @@ -134,7 +137,9 @@ fun AppScaffold(
}
},
gesturesEnabled = destination.shouldShowScaffoldElements(),
modifier = Modifier.fillMaxSize()
modifier = Modifier.fillMaxSize().semantics {
testTagsAsResourceId = true
}
) {
Scaffold(
bottomBar = {
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.

0 comments on commit 47d9219

Please sign in to comment.