@@ -3,11 +3,15 @@ package ac.at.uibk.dps.dapr.big.big
33// import io.dropwizard.metrics5.CsvReporter
44// import io.dropwizard.metrics5.MetricName
55// import io.dropwizard.metrics5.MetricRegistry
6+ import ac.at.uibk.dps.dapr.big.Big
67import io.dapr.actors.ActorId
78import io.dapr.actors.runtime.AbstractActor
89import io.dapr.actors.runtime.ActorRuntimeContext
910import io.dapr.client.DaprClientBuilder
11+ import java.util.concurrent.TimeUnit
12+ import kotlin.time.Clock
1013import kotlin.time.measureTime
14+ import kotlin.time.toJavaDuration
1115
1216class BigActorImpl (runtimeContext : ActorRuntimeContext <BigActorImpl >, val actorId : ActorId ) :
1317 AbstractActor (runtimeContext, actorId), BigActor {
@@ -17,25 +21,12 @@ class BigActorImpl(runtimeContext: ActorRuntimeContext<BigActorImpl>, val actorI
1721 var target: String = " "
1822 var count = 0
1923
20- /* var metricRegistry: MetricRegistry =
21- MetricRegistry().apply {
22- CsvReporter.forRegistry(this)
23- .build(Paths.get("/metrics").toAbsolutePath().toFile())
24- .start(1L, TimeUnit.SECONDS)
25- }
24+ var metricRegistry = Big .provideMetricRegistry()
2625
27- var counter =
28- metricRegistry.counter(
29- MetricName.build("big.pings").tagged(mapOf("id" to actorId.toString()))
30- )
31- var eventTimer =
32- metricRegistry.timer(
33- MetricName.build("event.latency").tagged(mapOf("id" to actorId.toString()))
34- )
35- var pingTimer =
36- metricRegistry.timer(MetricName.build("ping.duration").tagged("id", actorId.toString()))
37- var pongTimer =
38- metricRegistry.timer(MetricName.build("pong.duration").tagged("id", actorId.toString()))*/
26+ var counter = metricRegistry.counter(" big.pings" )
27+ var eventTimer = metricRegistry.timer(" event.latency" )
28+ var pingTimer = metricRegistry.timer(" ping.duration" )
29+ var pongTimer = metricRegistry.timer(" pong.duration" )
3930
4031 override fun register () {
4132 client.publishEvent(" pubsub" , " register" , actorId.toString()).subscribe()
@@ -49,45 +40,58 @@ class BigActorImpl(runtimeContext: ActorRuntimeContext<BigActorImpl>, val actorI
4940 override fun receivePong (data : Map <String , Any >) {
5041 val delta = measureTime {
5142 val time = data[" time" ] as Long
52- // eventTimer.update((System.nanoTime() - time) / 1_000, TimeUnit.MICROSECONDS)
43+
44+ val now = Clock .System .now()
45+ val nowNanos = (now.epochSeconds * 1_000_000_000L ) + now.nanosecondsOfSecond
46+ val deltaNanos = (nowNanos - time).coerceAtLeast(0L )
47+
48+ eventTimer.update(deltaNanos, TimeUnit .NANOSECONDS )
5349
5450 val sender = data[" sender" ] as String
5551
5652 if (sender == target) {
5753 sendPing()
5854 ++ count
5955
60- // counter.inc()
56+ counter.inc()
6157 }
6258 }
63- // pingTimer.update(delta.toJavaDuration())
59+ pingTimer.update(delta.toJavaDuration())
6460 }
6561
6662 override fun sendPong (data : Map <String , Any >) {
6763 val delta = measureTime {
6864 val time = data[" time" ] as Long
69- // eventTimer.update((System.nanoTime() - time) / 1_000, TimeUnit.MICROSECONDS)
65+
66+ val now = Clock .System .now()
67+ val nowNanos = (now.epochSeconds * 1_000_000_000L ) + now.nanosecondsOfSecond
68+ val deltaNanos = (nowNanos - time).coerceAtLeast(0L )
69+
70+ eventTimer.update(deltaNanos, TimeUnit .NANOSECONDS )
7071
7172 val sender = data[" sender" ] as String
7273
7374 client
7475 .publishEvent(
7576 " pubsub" ,
7677 " pong" ,
77- mapOf (" sender" to actorId.toString(), " receiver" to sender, " time" to System .nanoTime() ),
78+ mapOf (" sender" to actorId.toString(), " receiver" to sender, " time" to nowNanos ),
7879 )
7980 .subscribe()
8081 }
81- // pongTimer.update(delta.toJavaDuration())
82+ pongTimer.update(delta.toJavaDuration())
8283 }
8384
8485 private fun sendPing () {
8586 target = neighbors.random()
87+ val now = Clock .System .now()
88+ val nowNanos = (now.epochSeconds * 1_000_000_000L ) + now.nanosecondsOfSecond
89+
8690 client
8791 .publishEvent(
8892 " pubsub" ,
8993 " ping" ,
90- mapOf (" sender" to actorId.toString(), " receiver" to target, " time" to System .nanoTime() ),
94+ mapOf (" sender" to actorId.toString(), " receiver" to target, " time" to nowNanos ),
9195 )
9296 .subscribe()
9397 }
0 commit comments