[Commonizer] Replace ResettableClockMark by TimeSource.Monotonic
This commit is contained in:
committed by
Space Team
parent
09f895efe6
commit
3b626ea97c
@@ -7,23 +7,38 @@ package org.jetbrains.kotlin.commonizer.utils
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.commonizer.CommonizerTarget
|
import org.jetbrains.kotlin.commonizer.CommonizerTarget
|
||||||
import org.jetbrains.kotlin.util.Logger
|
import org.jetbrains.kotlin.util.Logger
|
||||||
|
import kotlin.time.Duration
|
||||||
|
import kotlin.time.TimeSource
|
||||||
|
|
||||||
private const val ansiReset = "\u001B[0m"
|
private const val ansiReset = "\u001B[0m"
|
||||||
private const val ansiTimeColor = "\u001B[36m"
|
private const val ansiTimeColor = "\u001B[36m"
|
||||||
private const val ansiTargetColor = "\u001B[32m"
|
private const val ansiTargetColor = "\u001B[32m"
|
||||||
|
|
||||||
internal inline fun <T> Logger?.progress(message: String, action: () -> T): T {
|
internal inline fun <T> Logger?.progress(message: String, action: () -> T): T {
|
||||||
val clock = ResettableClockMark()
|
if (this == null)
|
||||||
clock.reset()
|
return action()
|
||||||
|
|
||||||
|
val timeMark = TimeSource.Monotonic.markNow()
|
||||||
try {
|
try {
|
||||||
return action()
|
return action()
|
||||||
} finally {
|
} finally {
|
||||||
this?.log("$message ${ansiTimeColor}in ${clock.elapsedSinceLast()}$ansiReset")
|
val duration = timeMark.elapsedNow().toPrettyString()
|
||||||
|
log("$message ${ansiTimeColor}in $duration$ansiReset")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal inline fun <T> Logger?.progress(
|
internal inline fun <T> Logger?.progress(
|
||||||
target: CommonizerTarget, message: String, action: () -> T
|
target: CommonizerTarget, message: String, action: () -> T,
|
||||||
): T {
|
): T {
|
||||||
return progress("[$ansiTargetColor$target$ansiReset]: $message", action)
|
return progress("[$ansiTargetColor$target$ansiReset]: $message", action)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun Duration.toPrettyString() = buildString {
|
||||||
|
toComponents { hours, minutes, seconds, nanos ->
|
||||||
|
// human-friendly formatted duration
|
||||||
|
if (hours > 0) append(hours).append("h ")
|
||||||
|
if (minutes > 0 || isNotEmpty()) append(minutes).append("m ")
|
||||||
|
if (seconds > 0 || isNotEmpty()) append(seconds).append("s ")
|
||||||
|
append(nanos / 1000_000).append("ms")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.kotlin.commonizer.utils
|
|
||||||
|
|
||||||
import java.lang.System.currentTimeMillis
|
|
||||||
|
|
||||||
internal class ResettableClockMark {
|
|
||||||
internal class Period(val start: Long, val end: Long) {
|
|
||||||
override fun toString(): String {
|
|
||||||
var remainder = end - start
|
|
||||||
|
|
||||||
val millis = remainder % 1000
|
|
||||||
remainder /= 1000
|
|
||||||
|
|
||||||
val seconds = remainder % 60
|
|
||||||
remainder /= 60
|
|
||||||
|
|
||||||
val minutes = remainder % 60
|
|
||||||
|
|
||||||
val hours = remainder / 60
|
|
||||||
|
|
||||||
// human-friendly formatted duration
|
|
||||||
return buildString {
|
|
||||||
if (hours > 0) append(hours).append("h ")
|
|
||||||
if (minutes > 0 || isNotEmpty()) append(minutes).append("m ")
|
|
||||||
if (seconds > 0 || isNotEmpty()) append(seconds).append("s ")
|
|
||||||
append(millis).append("ms")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private var startMark: Long = 0
|
|
||||||
private var lastMark: Long = 0
|
|
||||||
|
|
||||||
fun elapsedSinceLast(): Period = Period(lastMark, currentTimeMillis()).also { lastMark = it.end }
|
|
||||||
fun elapsedSinceStart(): Period = Period(startMark, currentTimeMillis())
|
|
||||||
|
|
||||||
fun reset() {
|
|
||||||
startMark = currentTimeMillis()
|
|
||||||
lastMark = startMark
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: this is how it should be when Kotlin Time will become non-experimental
|
|
||||||
//@ExperimentalTime
|
|
||||||
//internal class ResettableClockMark {
|
|
||||||
// private val startMark = MonoClock.markNow()
|
|
||||||
// private var lastMark = startMark
|
|
||||||
//
|
|
||||||
// fun elapsedSinceLast(): Duration = lastMark.elapsedNow().also { lastMark = lastMark.plus(it) }
|
|
||||||
// fun elapsedSinceStart(): Duration = startMark.elapsedNow()
|
|
||||||
//}
|
|
||||||
Reference in New Issue
Block a user