Skip to content

Commit dfe6b2f

Browse files
committed
refactor: consolidate perf logging to single fun
1 parent ba7b220 commit dfe6b2f

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

app/src/main/java/to/bitkit/fcm/WakeNodeWorker.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import to.bitkit.repositories.LightningRepo
3737
import to.bitkit.services.CoreService
3838
import to.bitkit.ui.pushNotification
3939
import to.bitkit.utils.Logger
40-
import to.bitkit.utils.withPerformanceLogging
40+
import to.bitkit.utils.measured
4141
import kotlin.time.Duration.Companion.minutes
4242

4343
@Suppress("LongParameterList")
@@ -74,7 +74,7 @@ class WakeNodeWorker @AssistedInject constructor(
7474
Logger.debug("${this::class.simpleName} notification payload: $notificationPayload")
7575

7676
try {
77-
withPerformanceLogging {
77+
measured(TAG) {
7878
lightningRepo.start(
7979
walletIndex = 0,
8080
timeout = timeout,
@@ -252,4 +252,8 @@ class WakeNodeWorker @AssistedInject constructor(
252252

253253
deliverSignal.complete(Unit)
254254
}
255+
256+
companion object {
257+
private const val TAG = "WakeNodeWorker"
258+
}
255259
}

app/src/main/java/to/bitkit/utils/Logger.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ class AppLogger(
102102
delegate?.verbose(msg, e, context, file, line)
103103
}
104104

105-
fun performance(
105+
fun perf(
106106
msg: String?,
107107
context: String = "",
108108
file: String = getCallerPath(),
109109
line: Int = getCallerLine(),
110110
) {
111-
delegate?.performance(msg, context, file, line)
111+
delegate?.perf(msg, context, file, line)
112112
}
113113
}
114114

@@ -179,7 +179,7 @@ class LoggerImpl(
179179
saver.save(message)
180180
}
181181

182-
fun performance(
182+
fun perf(
183183
msg: String?,
184184
context: String = "",
185185
path: String = getCallerPath(),
Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
package to.bitkit.utils
22

3-
import java.time.Instant
4-
import kotlin.system.measureTimeMillis
3+
import kotlin.time.Duration
4+
import kotlin.time.measureTime
5+
6+
fun Duration.formatted(): String = toComponents { hours, minutes, seconds, nanoseconds ->
7+
val ms = nanoseconds / 1_000_000
8+
buildString {
9+
if (hours > 0) append("${hours}h ")
10+
if (minutes > 0) append("${minutes}m ")
11+
if (seconds > 0) append("${seconds}s ")
12+
if (ms > 0 || isEmpty()) append("${ms}ms")
13+
}.trim()
14+
}
515

616
internal inline fun <T> measured(
717
label: String,
818
block: () -> T,
919
): T {
1020
var result: T
11-
12-
val elapsedMs = measureTimeMillis {
21+
val elapsed = measureTime {
1322
result = block()
1423
}
15-
16-
Logger.debug("$label took ${elapsedMs}ms")
17-
18-
return result
19-
}
20-
21-
internal inline fun <T> withPerformanceLogging(block: () -> T): T {
22-
val startTime = System.currentTimeMillis()
23-
val startTimestamp = Instant.ofEpochMilli(startTime)
24-
Logger.performance("Start Time: $startTimestamp")
25-
26-
val result: T = block()
27-
28-
val endTime = System.currentTimeMillis()
29-
val endTimestamp = Instant.ofEpochMilli(endTime)
30-
val duration = (endTime - startTime) / 1000.0
31-
Logger.performance("End Time: $endTimestamp, Duration: $duration seconds")
32-
24+
Logger.perf("$label took ${elapsed.formatted()}")
3325
return result
3426
}

0 commit comments

Comments
 (0)