Added methods for tracking call durations.

This commit is contained in:
Stefan Ceriu
2022-04-05 10:01:16 +03:00
parent 711857ac9c
commit 88a0654d23
7 changed files with 94 additions and 2 deletions
+64
View File
@@ -0,0 +1,64 @@
//
// Benchmark.swift
// ElementX
//
// Created by Stefan Ceriu on 04/04/2022.
// Copyright © 2022 Element. All rights reserved.
//
import Foundation
struct Benchmark {
static private var trackingIdentifiers = [String: CFAbsoluteTime]()
static public var trackingEnabled = false
static func startTrackingForIdentifier(_ identifier: String, message: String? = nil) {
guard trackingEnabled else {
return
}
let startTime = CFAbsoluteTimeGetCurrent()
trackingIdentifiers[identifier] = startTime
if let message = message {
MXLog.verbose("⏰ Tracking \(identifier). \(message).")
} else {
MXLog.verbose("⏰ Tracking \(identifier).")
}
}
static func logElapsedDurationForIdentifier(_ identifier: String, message: String? = nil) {
guard trackingEnabled else {
return
}
guard let start = trackingIdentifiers[identifier] else {
assertionFailure("⏰ Invalid tracking identifier")
return
}
let elapsedTime = CFAbsoluteTimeGetCurrent() - start
if let message = message {
MXLog.verbose("⏰ Tracking \(identifier). \(message). Elapsed time: \(elapsedTime.round(to: 4)) seconds.")
} else {
MXLog.verbose("⏰ Tracking \(identifier). Elapsed time: \(elapsedTime.round(to: 4)) seconds.")
}
}
static func endTrackingForIdentifier(_ identifier: String, message: String? = nil) {
guard trackingEnabled else {
return
}
logElapsedDurationForIdentifier(identifier, message: message)
trackingIdentifiers[identifier] = nil
}
}
private extension Double {
func round(to places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}
}
+1 -1
View File
@@ -151,7 +151,7 @@ private var logger: SwiftyBeaver.Type = {
let consoleDestination = ConsoleDestination()
consoleDestination.useNSLog = true
consoleDestination.asynchronously = false
consoleDestination.format = "$C $N.$F():$l $M"
consoleDestination.format = "$N.$F:$l $M"
consoleDestination.levelColor.verbose = ""
consoleDestination.levelColor.debug = ""
consoleDestination.levelColor.info = ""