mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 10:33:45 +00:00
Added methods for tracking call durations.
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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 = ""
|
||||
|
||||
Reference in New Issue
Block a user