Skip to content

Commit

Permalink
Improved delay measure test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaron committed Jan 19, 2024
1 parent d7665d4 commit be9ed8b
Showing 1 changed file with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,27 @@ internal class TiingoSamples {
}


private inline fun <reified T : Action> Feed.apply2(
timeframe: Timeframe = Timeframe.INFINITE,
crossinline block: (T, Instant) -> Unit
private fun Feed.measure(
timeframe: Timeframe,
) = runBlocking {

// We need a channel with enough capacity
val channel = EventChannel(10_000, timeframe = timeframe)

val job = launch {
play(channel)
channel.close()
}
var sum = 0L
var n = 0L

try {
while (true) {
val o = channel.receive()
o.actions.filterIsInstance<T>().forEach { block(it, o.time) }
if (o.actions.isNotEmpty()) {
val now = Instant.now()
sum += now.toEpochMilli() - o.time.toEpochMilli()
n++
}
}

} catch (_: ClosedReceiveChannelException) {
Expand All @@ -121,23 +126,18 @@ internal class TiingoSamples {
if (job.isActive) job.cancel()
}

println("average delay=${sum/n}ms events=$n")

}


@Test
@Ignore
internal fun testLiveFeedMeasureKeepUp() {
internal fun testLiveFeedMeasure() {
val feed = TiingoLiveFeed.iex(0)
feed.subscribe() // subscribe to all iex stocks
var n = 0
var sum = 0L
feed.apply2<PriceAction>(Timeframe.next(1.minutes)) { _, time ->
val now = Instant.now()
sum += now.toEpochMilli() - time.toEpochMilli()
n++
}
feed.subscribe() // subscribe to all IEX quotes and trades
feed.measure(Timeframe.next(1.minutes))
feed.close()
println("average delay is ${sum/n}ms events=$n")
}

@Test
Expand Down

0 comments on commit be9ed8b

Please sign in to comment.