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
@@ -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)
}
}