Skip to content

Commit 7c67d4a

Browse files
authored
feat: adding invoke and event processing timers, fixing event latency… (#126)
1 parent 65cafb0 commit 7c67d4a

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/ActionCommand.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package at.ac.uibk.dps.cirrina.execution.`object`
33
import at.ac.uibk.dps.cirrina.csm.Csml.EventChannel
44
import at.ac.uibk.dps.cirrina.execution.service.ServiceImplementationSelector
55
import com.codahale.metrics.MetricRegistry
6+
import kotlin.time.measureTime
7+
import kotlin.time.toJavaDuration
68
import kotlinx.coroutines.CoroutineScope
79
import kotlinx.coroutines.launch
810
import mu.KotlinLogging
@@ -45,18 +47,21 @@ class ActionExecutor(
4547
val input = action.input.map { it.evaluate(scope.extent) }
4648

4749
coroutineScope.launch {
48-
runCatching { service.invoke(input) }
49-
.onSuccess { output ->
50-
action.emits.forEach { eventTemplate ->
51-
val emittedEvent = eventTemplate.copy(data = output)
52-
if (emittedEvent.channel == EventChannel.INTERNAL) {
53-
eventHandler.propagateToParent(emittedEvent)
54-
} else {
55-
eventHandler.emit(emittedEvent)
50+
val delta = measureTime {
51+
runCatching { service.invoke(input) }
52+
.onSuccess { output ->
53+
action.emits.forEach { eventTemplate ->
54+
val emittedEvent = eventTemplate.copy(data = output)
55+
if (emittedEvent.channel == EventChannel.INTERNAL) {
56+
eventHandler.propagateToParent(emittedEvent)
57+
} else {
58+
eventHandler.emit(emittedEvent)
59+
}
5660
}
5761
}
58-
}
59-
.onFailure { logger.error(it) { "service invocation failed" } }
62+
.onFailure { logger.error(it) { "service invocation failed" } }
63+
}
64+
metricRegistry.timer("invoke.time").update(delta.toJavaDuration())
6065
}
6166

6267
return emptyList()

src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import dagger.assisted.AssistedInject
1313
import java.util.concurrent.TimeUnit
1414
import kotlin.properties.Delegates
1515
import kotlin.time.Clock
16+
import kotlin.time.measureTime
17+
import kotlin.time.toJavaDuration
1618
import kotlinx.coroutines.*
1719
import kotlinx.coroutines.channels.Channel
1820
import 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

Comments
 (0)