Make TimeMark returned by TimeSource.Monotonic a value class
This value class wraps Long on JVM and Native thus reducing allocations in time measurement scenarios when the default monotonic time source is statically known. KT-46132
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2022 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.
|
||||
*/
|
||||
|
||||
@@ -13,10 +13,12 @@ import kotlin.time.Duration.Companion.nanoseconds
|
||||
|
||||
class MeasureTimeTest {
|
||||
|
||||
private fun longRunningCalc(): String = buildString {
|
||||
repeat(10) {
|
||||
while (Random.nextDouble() >= 0.001);
|
||||
append(('a'..'z').random())
|
||||
companion object {
|
||||
fun longRunningCalc(): String = buildString {
|
||||
repeat(10) {
|
||||
while (Random.nextDouble() >= 0.001);
|
||||
append(('a'..'z').random())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2022 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.
|
||||
*/
|
||||
|
||||
@@ -8,6 +8,7 @@ package test.time
|
||||
|
||||
import kotlin.test.*
|
||||
import kotlin.time.*
|
||||
import kotlin.time.Duration.Companion.microseconds
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
import kotlin.time.Duration.Companion.nanoseconds
|
||||
|
||||
@@ -57,4 +58,23 @@ class TimeMarkTest {
|
||||
markPast1.assertHasPassed(true)
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun defaultTimeMarkAdjustment() {
|
||||
val baseMark = TimeSource.Monotonic.markNow()
|
||||
|
||||
var markBefore = baseMark
|
||||
markBefore -= 100.microseconds
|
||||
markBefore -= 100.microseconds
|
||||
|
||||
val markAfter = baseMark + 100.microseconds
|
||||
|
||||
MeasureTimeTest.longRunningCalc()
|
||||
|
||||
val elapsedAfter = markAfter.elapsedNow()
|
||||
val elapsedBase = baseMark.elapsedNow()
|
||||
val elapsedBefore = markBefore.elapsedNow()
|
||||
assertTrue(elapsedBefore >= elapsedBase + 200.microseconds)
|
||||
assertTrue(elapsedAfter <= elapsedBase - 100.microseconds)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user