diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/test/TestRunner.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/test/TestRunner.kt index b901f4c8fa9..0751ff3439a 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/test/TestRunner.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/test/TestRunner.kt @@ -6,10 +6,11 @@ package kotlin.native.internal.test import kotlin.IllegalArgumentException -import kotlin.system.getTimeMillis -import kotlin.system.measureTimeMillis import kotlin.text.StringBuilder +import kotlin.time.TimeSource +import kotlin.time.measureTime +@OptIn(kotlin.time.ExperimentalTime::class) internal class TestRunner(val suites: List, args: Array) { private val filters = mutableListOf<(TestCase) -> Boolean>() private val listeners = mutableSetOf() @@ -239,13 +240,13 @@ internal class TestRunner(val suites: List, args: Array) { if (testCase.ignored) { sendToListeners { ignore(testCase) } } else { - val startTime = getTimeMillis() + val startTime = TimeSource.Monotonic.markNow() try { sendToListeners { start(testCase) } testCase.run() - sendToListeners { pass(testCase, getTimeMillis() - startTime) } + sendToListeners { pass(testCase, startTime.elapsedNow().inWholeMilliseconds) } } catch (e: Throwable) { - sendToListeners { fail(testCase, e, getTimeMillis() - startTime) } + sendToListeners { fail(testCase, e, startTime.elapsedNow().inWholeMilliseconds) } if (useExitCode) { exitCode = 1 } @@ -258,7 +259,7 @@ internal class TestRunner(val suites: List, args: Array) { private fun runIteration(iteration: Int) { val suitesFiltered = filterSuites() sendToListeners { startIteration(this@TestRunner, iteration, suitesFiltered) } - val iterationTime = measureTimeMillis { + val iterationTime = measureTime { suitesFiltered.forEach { if (it.ignored) { sendToListeners { ignoreSuite(it) } @@ -268,11 +269,11 @@ internal class TestRunner(val suites: List, args: Array) { return@forEach } sendToListeners { startSuite(it) } - val time = measureTimeMillis { it.run() } + val time = measureTime { it.run() }.inWholeMilliseconds sendToListeners { finishSuite(it, time) } } } - } + }.inWholeMilliseconds sendToListeners { finishIteration(this@TestRunner, iteration, iterationTime) } } @@ -280,13 +281,13 @@ internal class TestRunner(val suites: List, args: Array) { if (!runTests) return 0 sendToListeners { startTesting(this@TestRunner) } - val totalTime = measureTimeMillis { + val totalTime = measureTime { var i = 1 while (i <= iterations || iterations < 0) { runIteration(i) i++ } - } + }.inWholeMilliseconds sendToListeners { finishTesting(this@TestRunner, totalTime) } return exitCode } diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/random/Random.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/random/Random.kt index 9d1942152f1..c2720545b88 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/random/Random.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/random/Random.kt @@ -12,6 +12,7 @@ import kotlin.system.getTimeNanos */ internal object NativeRandom : Random() { private const val MULTIPLIER = 0x5deece66dL + @Suppress("DEPRECATION") private val _seed = AtomicLong(mult(getTimeNanos())) /** diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/system/Timing.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/system/Timing.kt index f25bc20abaa..441934e2c96 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/system/Timing.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/system/Timing.kt @@ -6,40 +6,91 @@ package kotlin.system import kotlin.native.internal.GCUnsafeCall +import kotlin.time.* /** * Gets current system time in milliseconds since certain moment in the past, * only delta between two subsequent calls makes sense. + * + * This function is deprecated. + * To measure the duration of execution of a block of code, + * use [measureTime] or [measureTimedValue] instead. + * To mark a point in time for querying the duration of time interval [elapsed][TimeMark.elapsedNow] + * from that point, use [TimeSource.Monotonic.markNow] instead. + * The resulting [Duration] then can be expressed as a [Long] number of milliseconds + * using [Duration.inWholeMilliseconds]. */ +@Deprecated("Use measureTime() or TimeSource.Monotonic.markNow() instead.") +@DeprecatedSinceKotlin(warningSince = "1.9") @GCUnsafeCall("Kotlin_system_getTimeMillis") public external fun getTimeMillis() : Long /** * Gets current system time in nanoseconds since certain moment in the past, * only delta between two subsequent calls makes sense. + * + * This function is deprecated. + * To measure the duration of execution of a block of code, + * use [measureTime] or [measureTimedValue] instead. + * To mark a point in time for querying the duration of time interval [elapsed][TimeMark.elapsedNow] + * from that point, use [TimeSource.Monotonic.markNow] instead. + * The resulting [Duration] then can be expressed as a [Long] number of nanoseconds + * using [Duration.inWholeNanoseconds]. */ +@Deprecated("Use measureTime() or TimeSource.Monotonic.markNow() instead.") +@DeprecatedSinceKotlin(warningSince = "1.9") @GCUnsafeCall("Kotlin_system_getTimeNanos") public external fun getTimeNanos() : Long /** * Gets current system time in microseconds since certain moment in the past, * only delta between two subsequent calls makes sense. + * + * This function is deprecated. + * To measure the duration of execution of a block of code, + * use [measureTime] or [measureTimedValue] instead. + * To mark a point in time for querying the duration of time interval [elapsed][TimeMark.elapsedNow] + * from that point, use [TimeSource.Monotonic.markNow] instead. + * The resulting [Duration] then can be expressed as a [Long] number of microseconds + * using [Duration.inWholeMicroseconds]. */ +@Deprecated("Use measureTime() or TimeSource.Monotonic.markNow() instead.") +@DeprecatedSinceKotlin(warningSince = "1.9") @GCUnsafeCall("Kotlin_system_getTimeMicros") public external fun getTimeMicros() : Long /** * Executes the given [block] and returns elapsed time in milliseconds. * + * This function is deprecated. + * To measure the duration of execution of a block of code, + * use [measureTime] or [measureTimedValue] instead. + * The resulting [Duration] then can be expressed as a [Long] number of milliseconds + * using [Duration.inWholeMilliseconds]. + * * @sample samples.system.Timing.measureBlockTimeMillis */ +@Deprecated("Use measureTime() instead.", ReplaceWith("measureTime(block).inWholeMilliseconds")) +@DeprecatedSinceKotlin(warningSince = "1.9") +@Suppress("DEPRECATION") public inline fun measureTimeMillis(block: () -> Unit) : Long { val start = getTimeMillis() block() return getTimeMillis() - start } -/** Executes the given [block] and returns elapsed time in microseconds (Kotlin/Native only). */ +/** + * Executes the given [block] and returns elapsed time in microseconds (Kotlin/Native only). + * + * This function is deprecated. + * To measure the duration of execution of a block of code, + * use [measureTime] or [measureTimedValue] instead. + * The resulting [Duration] then can be expressed as a [Long] number of microseconds + * using [Duration.inWholeMicroseconds]. + */ +@Deprecated("Use measureTime() instead.", ReplaceWith("measureTime(block).inWholeMicroseconds", "kotlin.time.measureTime")) +@DeprecatedSinceKotlin(warningSince = "1.9") +@Suppress("DEPRECATION") public inline fun measureTimeMicros(block: () -> Unit) : Long { val start = getTimeMicros() block() @@ -49,8 +100,17 @@ public inline fun measureTimeMicros(block: () -> Unit) : Long { /** * Executes the given [block] and returns elapsed time in nanoseconds. * + * This function is deprecated. + * To measure the duration of execution of a block of code, + * use [measureTime] or [measureTimedValue] instead. + * The resulting [Duration] then can be expressed as a [Long] number of nanoseconds + * using [Duration.inWholeNanoseconds]. + * * @sample samples.system.Timing.measureBlockNanoTime */ +@Deprecated("Use measureTime() instead.", ReplaceWith("measureTime(block).inWholeNanoseconds", "kotlin.time.measureTime")) +@DeprecatedSinceKotlin(warningSince = "1.9") +@Suppress("DEPRECATION") public inline fun measureNanoTime(block: () -> Unit) : Long { val start = getTimeNanos() block() diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/time/MonotonicTimeSource.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/time/MonotonicTimeSource.kt index 4ddaa8c1d00..38cfa7f90e1 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/time/MonotonicTimeSource.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/time/MonotonicTimeSource.kt @@ -11,7 +11,9 @@ import kotlin.time.TimeSource.Monotonic.ValueTimeMark @SinceKotlin("1.3") @ExperimentalTime internal actual object MonotonicTimeSource : TimeSource.WithComparableMarks { + @Suppress("DEPRECATION") private val zero: Long = getTimeNanos() + @Suppress("DEPRECATION") private fun read(): Long = getTimeNanos() - zero override fun toString(): String = "TimeSource(System.nanoTime())"