Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aba3bba3 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Add TimeMeasurer for Spa

To measure the app list first loaded time.

Bug: 235727273
Test: Manual with App List page
Change-Id: I4edc54edd850e2d7d3026782aa317428b13c27aa
parent 1c2241d9
Loading
Loading
Loading
Loading
+56 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 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
 *
 *      http://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.
 */

@file:OptIn(ExperimentalTime::class)

package com.android.settingslib.spa.framework.compose

import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import kotlin.time.ExperimentalTime
import kotlin.time.TimeSource

const val ENABLE_MEASURE_TIME = false

interface TimeMeasurer {
    fun log(msg: String) {}
    fun logFirst(msg: String) {}

    companion object {
        private object EmptyTimeMeasurer : TimeMeasurer

        @Composable
        fun rememberTimeMeasurer(tag: String): TimeMeasurer = remember {
            if (ENABLE_MEASURE_TIME) TimeMeasurerImpl(tag) else EmptyTimeMeasurer
        }
    }
}

private class TimeMeasurerImpl(private val tag: String) : TimeMeasurer {
    private val mark = TimeSource.Monotonic.markNow()
    private val msgLogged = mutableSetOf<String>()

    override fun log(msg: String) {
        Log.d(tag, "Timer $msg: ${mark.elapsedNow()}")
    }

    override fun logFirst(msg: String) {
        if (msgLogged.add(msg)) {
            Log.d(tag, "Timer $msg: ${mark.elapsedNow()}")
        }
    }
}
+3 −0
Original line number Original line Diff line number Diff line
@@ -28,6 +28,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.android.settingslib.spa.framework.compose.LogCompositions
import com.android.settingslib.spa.framework.compose.LogCompositions
import com.android.settingslib.spa.framework.compose.TimeMeasurer.Companion.rememberTimeMeasurer
import com.android.settingslib.spa.framework.compose.toState
import com.android.settingslib.spa.framework.compose.toState
import com.android.settingslib.spa.framework.theme.SettingsDimension
import com.android.settingslib.spa.framework.theme.SettingsDimension
import com.android.settingslib.spa.widget.ui.PlaceholderTitle
import com.android.settingslib.spa.widget.ui.PlaceholderTitle
@@ -66,7 +67,9 @@ private fun <T : AppRecord> AppListWidget(
    listModel: AppListModel<T>,
    listModel: AppListModel<T>,
    appItem: @Composable (itemState: AppListItemModel<T>) -> Unit,
    appItem: @Composable (itemState: AppListItemModel<T>) -> Unit,
) {
) {
    val timeMeasurer = rememberTimeMeasurer(TAG)
    appListData.value?.let { (list, option) ->
    appListData.value?.let { (list, option) ->
        timeMeasurer.logFirst("app list first loaded")
        if (list.isEmpty()) {
        if (list.isEmpty()) {
            PlaceholderTitle(stringResource(R.string.no_applications))
            PlaceholderTitle(stringResource(R.string.no_applications))
            return
            return