@@ -13,6 +13,8 @@ import dagger.assisted.AssistedInject
1313import java.util.concurrent.TimeUnit
1414import kotlin.properties.Delegates
1515import kotlin.time.Clock
16+ import kotlin.time.measureTime
17+ import kotlin.time.toJavaDuration
1618import kotlinx.coroutines.*
1719import kotlinx.coroutines.channels.Channel
1820import kotlinx.coroutines.channels.onFailure
@@ -100,6 +102,8 @@ internal constructor(
100102
101103 private val eventTimer: Timer = runtime.metricRegistry.timer(" event.latency" )
102104
105+ private val processEventTimer: Timer = runtime.metricRegistry.timer(" processEvent.time" )
106+
103107 init {
104108 val instanceData = Context .from(instance.data).getAll()
105109 val transientContext = Context .from(specification.transient)
@@ -154,19 +158,26 @@ internal constructor(
154158 }
155159
156160 private fun processEvent (event : Event ) {
157- if (isTerminated()) return
161+ val delta = measureTime {
162+ if (isTerminated()) return
163+
164+ if (
165+ (event.channel == EventChannel .EXTERNAL || event.channel == EventChannel .PERIPHERAL ) &&
166+ event.source != name
167+ ) {
168+ val now = Clock .System .now()
169+ val nowNanos = (now.epochSeconds * 1_000_000_000L ) + now.nanosecondsOfSecond
170+ val deltaNanos = (nowNanos - event.emittedTime).coerceAtLeast(0L )
171+
172+ eventTimer.update(deltaNanos, TimeUnit .NANOSECONDS )
173+ }
158174
159- if (event.channel == EventChannel .EXTERNAL && event.source != name) {
160- val now = Clock .System .now()
161- val nowNanos = (now.epochSeconds * 1_000_000_000L ) + now.nanosecondsOfSecond
162- val deltaNanos = (nowNanos - event.emittedTime).coerceAtLeast(0L )
175+ handleEvent(event)?.let { transition -> step(transition) }
163176
164- eventTimer.update(deltaNanos, TimeUnit . NANOSECONDS )
177+ if (event.channel == EventChannel . INTERNAL ) stateMachineEventHandler.propagateToNested(event )
165178 }
166179
167- handleEvent(event)?.let { transition -> step(transition) }
168-
169- if (event.channel == EventChannel .INTERNAL ) stateMachineEventHandler.propagateToNested(event)
180+ processEventTimer.update(delta.toJavaDuration())
170181 }
171182
172183 private fun handleEvent (event : Event ): ActiveTransition ? {
0 commit comments