Skip to content

Commit

Permalink
feat: print elapsed time of methods
Browse files Browse the repository at this point in the history
both within a method, and between two methods

Used to determine whether GZip makes sense
  • Loading branch information
david-allison committed Dec 16, 2023
1 parent 95e2ec5 commit 4945a96
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2360,6 +2360,7 @@ abstract class AbstractFlashcardViewer :
private val onPageFinishedCallback: OnPageFinishedCallback? = null
) : WebViewClient() {
private var pageFinishedFired = true
private val pageRenderStopwatch = Stopwatch.init("page render")

@TargetApi(Build.VERSION_CODES.N)
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
Expand All @@ -2378,6 +2379,7 @@ abstract class AbstractFlashcardViewer :
}

override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
pageRenderStopwatch.reset()
pageFinishedFired = false
}

Expand Down Expand Up @@ -2659,6 +2661,7 @@ abstract class AbstractFlashcardViewer :
return
}
pageFinishedFired = true
pageRenderStopwatch.logElapsed()
Timber.d("Java onPageFinished triggered: %s", url)
// onPageFinished will be called multiple times if the WebView redirects by setting window.location.href
onPageFinishedCallback?.onPageFinished()
Expand Down
81 changes: 81 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/utils/TimeUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2023 David Allison <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ichi2.utils

import androidx.annotation.CheckResult
import com.ichi2.libanki.utils.TimeManager
import timber.log.Timber

/**
* Usage:
*
* ```kotlin
* val result = measureTime("operation") { operation() }
* ```
* -> `D/TimeUtilKt executed mHtmlGenerator in 23ms`
*/
fun <T> measureTime(functionName: String? = "", function: () -> T): T {
val startTime = TimeManager.time.intTimeMS()
val result = function()
val endTime = TimeManager.time.intTimeMS()
Timber.d(
"executed %sin %dms",
if (functionName.isNullOrEmpty()) "" else "$functionName ",
endTime - startTime
)
return result
}

/**
* Used to time an operation across two function calls
*
* ```kotlin
* private val renderStopwatch: Stopwatch = Stopwatch.init("page render")
*
* fun start() {
* renderStopwatch.reset()
* }
*
* fun stop() {
* renderStopwatch.logElapsed()
* }
* ```
*
* -> `D/Stopwatch executed page render in 67ms`
*/
class Stopwatch(private val executionName: String?) {
private var startTime = TimeManager.time.intTimeMS()

fun logElapsed() {
val endTime = TimeManager.time.intTimeMS()
Timber.d(
"executed %sin %dms",
if (executionName.isNullOrEmpty()) "" else "$executionName ",
endTime - startTime
)
}

fun reset() {
startTime = TimeManager.time.intTimeMS()
}
companion object {

/** initializes the stopwatch to ensure `stop()` before `start()` won't crash */
@CheckResult
fun init(executionName: String? = null) = Stopwatch(executionName)
}
}

0 comments on commit 4945a96

Please sign in to comment.