mirror of
https://github.com/corda/corda.git
synced 2025-05-02 16:53:22 +00:00
Minor: small improvement to logElapsedTime (use the montonic clock and log even when an exception is thrown)
This commit is contained in:
parent
20dbdf9d1b
commit
ecb460b698
@ -205,16 +205,21 @@ val RunOnCallerThread: Executor = MoreExecutors.directExecutor()
|
|||||||
// TODO: Add inline back when a new Kotlin version is released and check if the java.lang.VerifyError
|
// TODO: Add inline back when a new Kotlin version is released and check if the java.lang.VerifyError
|
||||||
// returns in the IRSSimulationTest. If not, commit the inline back.
|
// returns in the IRSSimulationTest. If not, commit the inline back.
|
||||||
fun <T> logElapsedTime(label: String, logger: Logger? = null, body: () -> T): T {
|
fun <T> logElapsedTime(label: String, logger: Logger? = null, body: () -> T): T {
|
||||||
val now = System.currentTimeMillis()
|
// Use nanoTime as it's monotonic.
|
||||||
val r = body()
|
val now = System.nanoTime()
|
||||||
val elapsed = System.currentTimeMillis() - now
|
try {
|
||||||
if (logger != null)
|
return body()
|
||||||
logger.info("$label took $elapsed msec")
|
} finally {
|
||||||
else
|
val elapsed = Duration.ofNanos(System.nanoTime() - now).toMillis()
|
||||||
println("$label took $elapsed msec")
|
if (logger != null)
|
||||||
return r
|
logger.info("$label took $elapsed msec")
|
||||||
|
else
|
||||||
|
println("$label took $elapsed msec")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T> Logger.logElapsedTime(label: String, body: () -> T): T = logElapsedTime(label, this, body)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A threadbox is a simple utility that makes it harder to forget to take a lock before accessing some shared state.
|
* A threadbox is a simple utility that makes it harder to forget to take a lock before accessing some shared state.
|
||||||
* Simply define a private class to hold the data that must be grouped under the same lock, and then pass the only
|
* Simply define a private class to hold the data that must be grouped under the same lock, and then pass the only
|
||||||
|
Loading…
x
Reference in New Issue
Block a user