commit
73f607891c
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.samples.apps.nowinandroid.di
|
||||
|
||||
import android.app.Activity
|
||||
import android.util.Log
|
||||
import android.view.Window
|
||||
import androidx.metrics.performance.JankStats
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.components.ActivityComponent
|
||||
import java.util.concurrent.Executor
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.asExecutor
|
||||
|
||||
@Module
|
||||
@InstallIn(ActivityComponent::class)
|
||||
object JankStatsModule {
|
||||
@Provides
|
||||
fun providesOnFrameListener(): JankStats.OnFrameListener {
|
||||
return JankStats.OnFrameListener { frameData ->
|
||||
// Make sure to only log janky frames.
|
||||
if (frameData.isJank) {
|
||||
// We're currently logging this but would better report it to a backend.
|
||||
Log.v("NiA Jank", frameData.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun providesWindow(activity: Activity): Window {
|
||||
return activity.window
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun providesDefaultExecutor(): Executor {
|
||||
return Dispatchers.Default.asExecutor()
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun providesJankStats(
|
||||
window: Window,
|
||||
executor: Executor,
|
||||
frameListener: JankStats.OnFrameListener
|
||||
): JankStats {
|
||||
return JankStats.createAndTrack(window, executor, frameListener)
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.samples.apps.nowinandroid
|
||||
|
||||
import com.google.samples.apps.nowinandroid.benchmark.BuildConfig
|
||||
|
||||
/**
|
||||
* Convenience parameter to use proper package name with regards to build type and build flavor.
|
||||
*/
|
||||
const val PACKAGE_NAME =
|
||||
"com.google.samples.apps.nowinandroid.${BuildConfig.FLAVOR}.${BuildConfig.BUILD_TYPE}"
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.samples.apps.nowinandroid.foryou
|
||||
|
||||
import androidx.benchmark.macro.MacrobenchmarkScope
|
||||
import androidx.test.uiautomator.By
|
||||
import androidx.test.uiautomator.Direction
|
||||
import androidx.test.uiautomator.Until
|
||||
|
||||
fun MacrobenchmarkScope.forYouWaitForContent() {
|
||||
// Wait until content is loaded
|
||||
device.wait(Until.hasObject(By.text("What are you interested in?")), 30_000)
|
||||
}
|
||||
|
||||
fun MacrobenchmarkScope.forYouSelectAuthors() {
|
||||
val authors = device.findObject(By.res("forYou:authors"))
|
||||
// select some authors to show some feed content
|
||||
repeat(3) { index ->
|
||||
val author = authors.children[index % authors.childCount]
|
||||
author.click()
|
||||
device.waitForIdle()
|
||||
}
|
||||
}
|
||||
|
||||
fun MacrobenchmarkScope.forYouScrollFeedDownUp() {
|
||||
val feedList = device.findObject(By.res("forYou:feed"))
|
||||
feedList.fling(Direction.DOWN)
|
||||
device.waitForIdle()
|
||||
feedList.fling(Direction.UP)
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.samples.apps.nowinandroid.foryou
|
||||
|
||||
import androidx.benchmark.macro.CompilationMode
|
||||
import androidx.benchmark.macro.FrameTimingMetric
|
||||
import androidx.benchmark.macro.StartupMode
|
||||
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
|
||||
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
|
||||
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
@RunWith(AndroidJUnit4ClassRunner::class)
|
||||
class ScrollForYouFeedBenchmark {
|
||||
@get:Rule
|
||||
val benchmarkRule = MacrobenchmarkRule()
|
||||
|
||||
@Test
|
||||
fun scrollFeedCompilationNone() = scrollFeed(CompilationMode.None())
|
||||
|
||||
@Test
|
||||
fun scrollFeedCompilationBaselineProfile() = scrollFeed(CompilationMode.Partial())
|
||||
|
||||
private fun scrollFeed(compilationMode: CompilationMode) = benchmarkRule.measureRepeated(
|
||||
packageName = PACKAGE_NAME,
|
||||
metrics = listOf(FrameTimingMetric()),
|
||||
compilationMode = compilationMode,
|
||||
iterations = 10,
|
||||
startupMode = StartupMode.COLD,
|
||||
setupBlock = {
|
||||
// Start the app
|
||||
pressHome()
|
||||
startActivityAndWait()
|
||||
}
|
||||
) {
|
||||
forYouWaitForContent()
|
||||
forYouSelectAuthors()
|
||||
forYouScrollFeedDownUp()
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.samples.apps.nowinandroid.interests
|
||||
|
||||
import androidx.benchmark.macro.MacrobenchmarkScope
|
||||
import androidx.test.uiautomator.By
|
||||
import androidx.test.uiautomator.Direction
|
||||
|
||||
fun MacrobenchmarkScope.interestsScrollTopicsDownUp() {
|
||||
val topicsList = device.findObject(By.res("interests:topics"))
|
||||
topicsList.fling(Direction.DOWN)
|
||||
device.waitForIdle()
|
||||
topicsList.fling(Direction.UP)
|
||||
}
|
||||
|
||||
fun MacrobenchmarkScope.interestsScrollPeopleDownUp() {
|
||||
val peopleList = device.findObject(By.res("interests:people"))
|
||||
peopleList.fling(Direction.DOWN)
|
||||
device.waitForIdle()
|
||||
peopleList.fling(Direction.UP)
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
|
||||
class FirebasePerfConventionPlugin : Plugin<Project> {
|
||||
override fun apply(target: Project) {
|
||||
with(target) {
|
||||
pluginManager.findPlugin("com.google.firebase.firebase-perf").apply {
|
||||
version = "1.4.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
@ -0,0 +1,5 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.samples.apps.nowinandroid.core.ui
|
||||
|
||||
import androidx.compose.foundation.gestures.ScrollableState
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.DisposableEffectResult
|
||||
import androidx.compose.runtime.DisposableEffectScope
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.snapshotFlow
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import androidx.metrics.performance.PerformanceMetricsState
|
||||
import androidx.metrics.performance.PerformanceMetricsState.MetricsStateHolder
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
/**
|
||||
* Retrieves [PerformanceMetricsState.MetricsStateHolder] from current [LocalView] and
|
||||
* remembers it until the View changes.
|
||||
* @see PerformanceMetricsState.getForHierarchy
|
||||
*/
|
||||
@Composable
|
||||
fun rememberMetricsStateHolder(): MetricsStateHolder {
|
||||
val localView = LocalView.current
|
||||
|
||||
return remember(localView) {
|
||||
PerformanceMetricsState.getForHierarchy(localView)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function to work with [PerformanceMetricsState] state. The side effect is
|
||||
* re-launched if any of the [keys] value is not equal to the previous composition.
|
||||
* @see JankMetricDisposableEffect if you need to work with DisposableEffect to cleanup added state.
|
||||
*/
|
||||
@Composable
|
||||
fun JankMetricEffect(
|
||||
vararg keys: Any?,
|
||||
reportMetric: suspend CoroutineScope.(state: MetricsStateHolder) -> Unit
|
||||
) {
|
||||
val metrics = rememberMetricsStateHolder()
|
||||
LaunchedEffect(metrics, *keys) {
|
||||
reportMetric(metrics)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function to work with [PerformanceMetricsState] state that needs to be cleaned up.
|
||||
* The side effect is re-launched if any of the [keys] value is not equal to the previous composition.
|
||||
*/
|
||||
@Composable
|
||||
fun JankMetricDisposableEffect(
|
||||
vararg keys: Any?,
|
||||
reportMetric: DisposableEffectScope.(state: MetricsStateHolder) -> DisposableEffectResult
|
||||
) {
|
||||
val metrics = rememberMetricsStateHolder()
|
||||
DisposableEffect(metrics, *keys) {
|
||||
reportMetric(this, metrics)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun TrackScrollJank(scrollableState: ScrollableState, stateName: String) {
|
||||
JankMetricEffect(scrollableState) { metricsHolder ->
|
||||
snapshotFlow { scrollableState.isScrollInProgress }.collect { isScrollInProgress ->
|
||||
metricsHolder.state?.apply {
|
||||
if (isScrollInProgress) {
|
||||
addState(stateName, "Scrolling=true")
|
||||
} else {
|
||||
removeState(stateName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue