Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace custom time handling with kotlin.time.TimeSource #260

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 3 additions & 19 deletions api/kotlinx-html.api
Original file line number Diff line number Diff line change
Expand Up @@ -5177,10 +5177,6 @@ public final class kotlinx/html/UtilKt {
public static final fun styleLink (Lkotlinx/html/HEAD;Ljava/lang/String;)V
}

public final class kotlinx/html/UtilsImpl_jvmKt {
public static final fun currentTimeMillis ()J
}

public class kotlinx/html/VAR : kotlinx/html/HTMLTag, kotlinx/html/HtmlBlockInlineTag {
public fun <init> (Ljava/util/Map;Lkotlinx/html/TagConsumer;)V
public fun getConsumer ()Lkotlinx/html/TagConsumer;
Expand Down Expand Up @@ -5360,8 +5356,9 @@ public final class kotlinx/html/consumers/Finalize_consumerKt {
}

public final class kotlinx/html/consumers/Measure_consumerKt {
public static final fun getOut (Lkotlinx/html/consumers/TimedResult;)Ljava/lang/Appendable;
public static final fun measureTime (Lkotlinx/html/TagConsumer;)Lkotlinx/html/TagConsumer;
public static final fun getOut (Lkotlin/time/TimedValue;)Ljava/lang/Appendable;
public static final fun measureTime (Lkotlinx/html/TagConsumer;Lkotlin/time/TimeSource;)Lkotlinx/html/TagConsumer;
public static synthetic fun measureTime$default (Lkotlinx/html/TagConsumer;Lkotlin/time/TimeSource;ILjava/lang/Object;)Lkotlinx/html/TagConsumer;
}

public final class kotlinx/html/consumers/PredicateResult : java/lang/Enum {
Expand All @@ -5380,19 +5377,6 @@ public final class kotlinx/html/consumers/PredicateResults {
public final fun getSKIP ()Lkotlinx/html/consumers/PredicateResult;
}

public final class kotlinx/html/consumers/TimedResult {
public fun <init> (Ljava/lang/Object;J)V
public final fun component1 ()Ljava/lang/Object;
public final fun component2 ()J
public final fun copy (Ljava/lang/Object;J)Lkotlinx/html/consumers/TimedResult;
public static synthetic fun copy$default (Lkotlinx/html/consumers/TimedResult;Ljava/lang/Object;JILjava/lang/Object;)Lkotlinx/html/consumers/TimedResult;
public fun equals (Ljava/lang/Object;)Z
public final fun getResult ()Ljava/lang/Object;
public final fun getTime ()J
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class kotlinx/html/consumers/TraceConsumer : kotlinx/html/TagConsumer {
public fun <init> (Lkotlinx/html/TagConsumer;Lkotlin/jvm/functions/Function1;)V
public fun finalize ()Ljava/lang/Object;
Expand Down
15 changes: 6 additions & 9 deletions src/commonMain/kotlin/measure-consumer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package kotlinx.html.consumers

import kotlinx.html.*
import kotlinx.html.org.w3c.dom.events.Event
import kotlin.time.*

data class TimedResult<T>(val result: T, val time: Long)

val <O : Appendable> TimedResult<O>.out: O
get() = result

private class TimeMeasureConsumer<R>(val downstream: TagConsumer<R>) : TagConsumer<TimedResult<R>> {
private val start = currentTimeMillis()
val <O : Appendable> TimedValue<O>.out: O
Copy link
Contributor Author

@hfhbd hfhbd Feb 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for this extension? 🤔 You can call result/value directly.

get() = value

private class TimeMeasureConsumer<R>(val downstream: TagConsumer<R>, val start: TimeMark) : TagConsumer<TimedValue<R>> {
override fun onTagStart(tag: Tag) {
downstream.onTagStart(tag)
}
Expand Down Expand Up @@ -43,7 +40,7 @@ private class TimeMeasureConsumer<R>(val downstream: TagConsumer<R>) : TagConsum
downstream.onTagComment(content)
}

override fun finalize(): TimedResult<R> = TimedResult(downstream.finalize(), currentTimeMillis() - start)
override fun finalize(): TimedValue<R> = TimedValue(downstream.finalize(), start.elapsedNow())
}

fun <R> TagConsumer<R>.measureTime(): TagConsumer<TimedResult<R>> = TimeMeasureConsumer(this)
fun <R> TagConsumer<R>.measureTime(timeSource: TimeSource = TimeSource.Monotonic): TagConsumer<TimedValue<R>> = TimeMeasureConsumer(this, timeSource.markNow())
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/trace-consumer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package kotlinx.html.consumers
import kotlinx.html.*

class TraceConsumer<R>(val downstream: TagConsumer<R>, val println: (String) -> Unit) : TagConsumer<R> by downstream {
private val id = "ID-${currentTimeMillis() % 16384}"
private val id = "ID-${hashCode()}"
private val path = ArrayList<String>(1024)

override fun onTagStart(tag: Tag) {
Expand Down
2 changes: 0 additions & 2 deletions src/commonMain/kotlin/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ val Tag.br: Unit
consumer.onTagStart(tag)
consumer.onTagEnd(tag)
}

expect fun currentTimeMillis(): Long
5 changes: 0 additions & 5 deletions src/jsMain/kotlin/utilsImpl-js.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/jvmMain/kotlin/utilsImpl-jvm.kt

This file was deleted.

27 changes: 11 additions & 16 deletions src/jvmTest/kotlin/streaming.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.html.consumers.*
import kotlinx.html.stream.*
import java.io.*
import kotlin.test.*
import kotlin.time.*

class TestStreaming {
@Test fun `we should be able to construct at least simple things`() {
Expand Down Expand Up @@ -233,29 +234,23 @@ class TestStreaming {
val count = 1000
val builder = StringBuilder(26 * (count + 1)).appendHTML(false)

var minStart: Long
var maxStart = 0L
var minEnd = 0L
var maxEnd: Long
val timeSource = TimeSource.Monotonic
val now = timeSource.markNow()
var measuredDuration = Duration.INFINITE

minStart = currentTimeMillis()
val rs = builder.measureTime().div {
maxStart = currentTimeMillis()
val rs = builder.measureTime(timeSource = timeSource).div {
val measuredStart = timeSource.markNow()
for (i in 1..count) {
div {
p { +"node$i" }
}
}
minEnd = currentTimeMillis()
measuredDuration = measuredStart.elapsedNow()
}
maxEnd = currentTimeMillis()
val realDuration = now.elapsedNow()

val maxTime = maxEnd - minStart
val minTime = minEnd - maxStart

val errorMessage = "Expected time should be between $minTime and $maxTime ms, but actual is ${rs.time} ms"
assertTrue(errorMessage) { rs.time >= minTime }
assertTrue(errorMessage) { rs.time <= maxTime }
val errorMessage = "Expected time should be between $measuredDuration and $realDuration, but actual is ${rs.duration}"
assertTrue(errorMessage) { rs.duration in measuredDuration..realDuration }

val expected = StringBuilder().apply {
append("<div>")
Expand All @@ -268,7 +263,7 @@ class TestStreaming {
append("</div>")
}

assertEquals(expected.toString(), rs.result.toString())
assertEquals(expected.toString(), rs.value.toString())
}

@Test fun `escape bad chars`() {
Expand Down
13 changes: 0 additions & 13 deletions src/nativeMain/kotlin/utilNative.kt

This file was deleted.

8 changes: 0 additions & 8 deletions src/wasmJsMain/kotlin/utilsImpl-js.kt

This file was deleted.