forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChronometer.scala
More file actions
144 lines (111 loc) · 4.34 KB
/
Chronometer.scala
File metadata and controls
144 lines (111 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package lila.common
object Chronometer:
object futureExtension:
import scala.concurrent.Await
extension [A](fua: Future[A])
def await(duration: FiniteDuration, name: String): A =
Chronometer.syncMon(_.blocking.time(name)):
try Await.result(fua, duration)
catch
case e: Exception =>
lila.mon.blocking.timeout(name).increment()
throw e
def awaitOrElse(duration: FiniteDuration, name: String, default: => A): A =
try await(duration, name)
catch case _: Exception => default
def chronometer = Chronometer(fua)
def chronometerTry = Chronometer.lapTry(fua)
def mon(path: lila.mon.TimerPath): Fu[A] = chronometer.mon(path).result
def monTry(path: scala.util.Try[A] => lila.mon.TimerPath): Fu[A] =
chronometerTry.mon(r => path(r)(lila.mon)).result
def monSuccess(path: lila.mon.type => Boolean => kamon.metric.Timer): Fu[A] =
chronometerTry
.mon: r =>
path(lila.mon)(r.isSuccess)
.result
def monValue(path: A => lila.mon.TimerPath): Fu[A] = chronometer.monValue(path).result
def logTime(name: String): Fu[A] = chronometer.pp(name)
def logTimeIfGt(name: String, duration: FiniteDuration): Fu[A] = chronometer.ppIfGt(name, duration)
end futureExtension
case class Lap[A](result: A, nanos: Long):
def millis = (nanos / 1000000).toInt
def micros = (nanos / 1000).toInt
def seconds = (millis / 1000).toInt
def logIfSlow(threshold: Int, logger: lila.log.Logger)(msg: A => String) =
if millis >= threshold then log(logger)(msg)
else this
def log(logger: lila.log.Logger)(msg: A => String) =
logger.info(s"<${millis}ms> ${msg(result)}")
this
def mon(path: lila.mon.TimerPath) =
path(lila.mon).record(nanos)
this
def monValue(path: A => lila.mon.TimerPath) =
path(result)(lila.mon).record(nanos)
this
def pp: A =
println(s"chrono $showDuration")
result
def pp(msg: String): A =
println(s"chrono $msg - $showDuration")
result
def ppIfGt(msg: String, duration: FiniteDuration): A =
if nanos > duration.toNanos then pp(msg)
else result
def showDuration: String = if millis >= 1 then s"$millis ms" else s"$micros micros"
case class LapTry[A](result: scala.util.Try[A], nanos: Long):
def millis = (nanos / 1000000).toInt
case class FuLap[A](lap: Fu[Lap[A]]) extends AnyVal:
def logIfSlow(threshold: Int, logger: lila.log.Logger)(msg: A => String) =
lap.dforeach(_.logIfSlow(threshold, logger)(msg))
this
def mon(path: lila.mon.TimerPath) =
lap.dforeach(_.mon(path))
this
def monValue(path: A => lila.mon.TimerPath) =
lap.dforeach(_.monValue(path))
this
def log(logger: lila.log.Logger)(msg: A => String) =
lap.dforeach(_.log(logger)(msg))
this
def pp: Fu[A] = lap.dmap(_.pp)
def pp(msg: String): Fu[A] = lap.dmap(_.pp(msg))
def ppIfGt(msg: String, duration: FiniteDuration): Fu[A] = lap.dmap(_.ppIfGt(msg, duration))
def tap(f: Lap[A] => Unit) =
lap.dforeach(f)
this
def result = lap.dmap(_.result)
case class FuLapTry[A](lap: Fu[LapTry[A]]) extends AnyVal:
def mon(path: scala.util.Try[A] => kamon.metric.Timer) =
lap.dforeach: l =>
path(l.result).record(l.nanos)
this
def result =
lap.flatMap { l =>
Future.fromTry(l.result)
}(scala.concurrent.ExecutionContext.parasitic)
def apply[A](f: Fu[A]): FuLap[A] =
val start = nowNanosRel
FuLap(f.dmap { Lap(_, nowNanosRel - start) })
def lapTry[A](f: Fu[A]): FuLapTry[A] =
val start = nowNanosRel
FuLapTry:
f.transformWith { r =>
fuccess(LapTry(r, nowNanosRel - start))
}(scala.concurrent.ExecutionContext.parasitic)
def sync[A](f: => A): Lap[A] =
val start = nowNanosRel
val res = f
Lap(res, nowNanosRel - start)
def syncEffect[A](f: => A)(effect: Lap[A] => Unit): A =
val lap = sync(f)
effect(lap)
lap.result
def syncMon[A](path: lila.mon.TimerPath)(f: => A): A =
val timer = path(lila.mon).start()
val res = f
timer.stop()
res
def start =
val at = nowNanosRel
() => Lap((), nowNanosRel - at)