Created
March 13, 2020 14:49
-
-
Save diego-gomez-olvera/7c6e033e652cf2891975e62e4aa798b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.booking.debug.performance | |
import android.os.SystemClock | |
import android.util.Log | |
/** | |
* Simple class to check the execution time between several execution points across threads. | |
* Unlike [TraceCompat](https://developer.android.com/reference/androidx/core/os/TraceCompat) | |
* it is not expected to 'begin' and 'end' sections but it will just print the time between | |
* the points being tracked. | |
* | |
* Feel free to tune it for your use case, using milliseconds, nanoseconds, | |
* | |
* Example: | |
* ``` | |
* val logger = PerfLogger("SearchResults") | |
* ... | |
* logger.reset() | |
* logger.trace("User click") | |
* ... | |
* logger.trace("Request stared") | |
* ... | |
* logger.trace("Request sent") | |
* ... | |
* logger.trace("Response received") | |
* ... | |
* logger.trace("Response parsed") | |
* ... | |
* logger.trace("Data processed") | |
* ... | |
* logger.trace("Data handled") | |
* | |
* ``` | |
*/ | |
class PerfLogger(private val tag: String) { | |
@Volatile | |
private var lastAction: String = "" | |
@Volatile | |
private var lastTime: Long = -1L | |
fun trace(action: String) { | |
val current = SystemClock.elapsedRealtime() | |
if (lastTime >= 0) { | |
Log.e( | |
tag, | |
"%60s - %8d ms -> %60s | %s", | |
lastAction, | |
current - lastTime, | |
action, | |
Thread.currentThread() | |
) | |
} | |
lastAction = action | |
lastTime = current | |
} | |
fun reset() { | |
lastAction = "" | |
lastTime = -1L | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment