Implement MonoClock in JS

It uses either process.hrtime() in node.js or performance.now() in browser,
falling back to Date.now() if the latter is not available
This commit is contained in:
Ilya Gorbunov
2019-04-11 19:59:47 +03:00
parent b816f50d4d
commit ca29cfb88d
2 changed files with 92 additions and 4 deletions
@@ -1,12 +1,53 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
* 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 kotlin.time
import org.w3c.performance.GlobalPerformance
import org.w3c.performance.Performance
public actual object MonoClock : Clock {
override fun mark(initialElapsed: Duration): ClockMark {
TODO("not implemented: base on high-res browser or node perf counter")
private val actualClock: Clock = run {
val isNode: Boolean = js("typeof process !== 'undefined' && process.versions && !!process.versions.node")
if (isNode)
HrTimeClock(js("process").unsafeCast<Process>())
else
js("self").unsafeCast<GlobalPerformance?>()?.performance?.let(::PerformanceClock)
?: DateNowClock
}
override fun mark(initialElapsed: Duration): ClockMark = actualClock.mark(initialElapsed)
}
internal external interface Process {
fun hrtime(time: Array<Double> = definedExternally): Array<Double>
}
internal class HrTimeClock(val process: Process) : Clock {
override fun mark(initialElapsed: Duration): ClockMark = object : ClockMark {
val startedAt = process.hrtime()
override val clock: Clock get() = this@HrTimeClock // delegation problem?
override val elapsedFrom: Duration
get() = process.hrtime(startedAt).let { (seconds, nanos) -> seconds.seconds + nanos.nanoseconds + initialElapsed }
}
override fun toString(): String = "Clock(process.hrtime())"
}
internal class PerformanceClock(val performance: Performance) : DoubleReadingClock() {
override val unit: DurationUnit get() = DurationUnit.MILLISECONDS
override fun reading(): Double = performance.now()
override fun toString(): String = "Clock(self.performance.now())"
}
internal object DateNowClock : DoubleReadingClock() {
override val unit: DurationUnit get() = DurationUnit.MILLISECONDS
override fun reading(): Double = kotlin.js.Date.now()
override fun toString(): String = "Clock(Date.now())"
}
@@ -0,0 +1,47 @@
/*
* 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 test.time
import kotlin.random.Random
import kotlin.test.*
import kotlin.time.*
class MeasureTimeTest {
private fun longRunningCalc(): String = buildString {
repeat(10) {
while (Random.nextDouble() >= 0.001);
append(('a'..'z').random())
}
}
@Test
fun measureTimeOfCalc() {
val someResult: String
val elapsed = measureTime {
someResult = longRunningCalc()
}
println("elapsed: $elapsed")
assertEquals(10, someResult.length)
assertTrue(elapsed > Duration.ZERO)
}
@Test
fun measureTimeAndResult() {
val someResult: String
val measured: DurationMeasured<String> = withMeasureTime { longRunningCalc().also { someResult = it } }
println("measured: $measured")
val (result, elapsed) = measured
assertEquals(someResult, result)
assertTrue(elapsed > Duration.ZERO)
}
}