|
1 | 1 | package ac.at.uibk.dps.dapr.chameneos.chameneos |
2 | 2 |
|
| 3 | +import ac.at.uibk.dps.dapr.chameneos.Chameneos |
3 | 4 | import io.dapr.actors.ActorId |
4 | 5 | import io.dapr.actors.runtime.AbstractActor |
5 | 6 | import io.dapr.actors.runtime.ActorRuntimeContext |
6 | 7 | import io.dapr.client.DaprClientBuilder |
| 8 | +import java.util.concurrent.TimeUnit |
7 | 9 | import kotlin.random.Random |
| 10 | +import kotlin.time.Clock |
| 11 | +import kotlin.time.measureTime |
| 12 | +import kotlin.time.toJavaDuration |
8 | 13 |
|
9 | 14 | class ChameneosActorImpl( |
10 | 15 | runtimeContext: ActorRuntimeContext<ChameneosActorImpl>, |
11 | 16 | val actorId: ActorId, |
12 | 17 | ) : AbstractActor(runtimeContext, actorId), ChameneosActor { |
13 | | - |
14 | 18 | val client = DaprClientBuilder().build() |
| 19 | + |
15 | 20 | var color = Random.nextInt(1, 4) |
16 | 21 |
|
| 22 | + var metricRegistry = Chameneos.provideMetricRegistry() |
| 23 | + |
| 24 | + var eventTimer = metricRegistry.timer("event.latency") |
| 25 | + var requestTimer = metricRegistry.timer("request.duration") |
| 26 | + var meetTimer = metricRegistry.timer("meet.duration") |
| 27 | + var changeTimer = metricRegistry.timer("change.duration") |
| 28 | + |
17 | 29 | override fun request() { |
18 | | - client |
19 | | - .publishEvent( |
20 | | - "pubsub", |
21 | | - "request", |
22 | | - mapOf<String, Any>("requestor" to actorId.toString(), "color" to color), |
23 | | - ) |
24 | | - .subscribe() |
| 30 | + val delta = measureTime { |
| 31 | + val now = Clock.System.now() |
| 32 | + val epochNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond |
| 33 | + |
| 34 | + client |
| 35 | + .publishEvent( |
| 36 | + "pubsub", |
| 37 | + "request", |
| 38 | + mapOf<String, Any>( |
| 39 | + "requestor" to actorId.toString(), |
| 40 | + "color" to color, |
| 41 | + "time" to epochNanos, |
| 42 | + ), |
| 43 | + ) |
| 44 | + .subscribe() |
| 45 | + } |
| 46 | + requestTimer.update(delta.toJavaDuration()) |
25 | 47 | } |
26 | 48 |
|
27 | | - override fun meet(request: MeetRequest) { |
28 | | - color = if (color == request.partnerColor) color else (color xor request.partnerColor) |
29 | | - client |
30 | | - .publishEvent( |
31 | | - "pubsub", |
32 | | - "change", |
33 | | - mapOf<String, Any>("partner" to request.partner, "color" to color), |
34 | | - ) |
35 | | - .subscribe() |
| 49 | + override fun meet(data: Map<String, Any>) { |
| 50 | + val delta = measureTime { |
| 51 | + val time = data["time"] as Long |
| 52 | + val partnerColor = data["color"] as Int |
| 53 | + val partner = data["partner"] as String |
| 54 | + |
| 55 | + var now = Clock.System.now() |
| 56 | + var nowNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond |
| 57 | + |
| 58 | + val deltaNanos = (nowNanos - time).coerceAtLeast(0L) |
36 | 59 |
|
| 60 | + eventTimer.update(deltaNanos, TimeUnit.NANOSECONDS) |
| 61 | + |
| 62 | + color = if (color == partnerColor) color else (color xor partnerColor) |
| 63 | + |
| 64 | + now = Clock.System.now() |
| 65 | + nowNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond |
| 66 | + client |
| 67 | + .publishEvent( |
| 68 | + "pubsub", |
| 69 | + "change", |
| 70 | + mapOf<String, Any>("partner" to partner, "color" to color, "time" to nowNanos), |
| 71 | + ) |
| 72 | + .subscribe() |
| 73 | + } |
| 74 | + meetTimer.update(delta.toJavaDuration()) |
37 | 75 | request() |
38 | 76 | } |
39 | 77 |
|
40 | | - override fun change(partnerColor: Int) { |
41 | | - color = partnerColor |
| 78 | + override fun change(data: Map<String, Any>) { |
| 79 | + val delta = measureTime { |
| 80 | + val time = data["time"] as Long |
| 81 | + |
| 82 | + val now = Clock.System.now() |
| 83 | + val nowNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond |
| 84 | + |
| 85 | + val deltaNanos = (nowNanos - time).coerceAtLeast(0L) |
| 86 | + |
| 87 | + eventTimer.update(deltaNanos, TimeUnit.NANOSECONDS) |
| 88 | + |
| 89 | + color = data["color"] as Int |
| 90 | + } |
| 91 | + changeTimer.update(delta.toJavaDuration()) |
| 92 | + |
42 | 93 | request() |
43 | 94 | } |
44 | 95 | } |
0 commit comments