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
+6
View File
@@ -43,6 +43,12 @@ class AppCoordinator: AuthenticationCoordinatorDelegate, Coordinator {
authenticationCoordinator = AuthenticationCoordinator(keychainController: keychainController,
navigationRouter: navigationRouter)
authenticationCoordinator.delegate = self
let loggerConfiguration = MXLogConfiguration()
loggerConfiguration.logLevel = .verbose
MXLog.configure(loggerConfiguration)
// Benchmark.trackingEnabled = true
}
func start() {
+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 = ""
@@ -123,6 +123,7 @@ class AuthenticationCoordinator: Coordinator {
}
private func login(username: String, password: String, completion: @escaping (Result<Void, AuthenticationCoordinatorError>) -> Void) {
Benchmark.startTrackingForIdentifier("Login", message: "Started new login")
delegate?.authenticationCoordinatorDidStartLoading(self)
@@ -148,6 +149,7 @@ class AuthenticationCoordinator: Coordinator {
}
private func restorePreviousLogin(_ usernameTokenTuple: (username: String, token: String), completion: @escaping (Result<Void, AuthenticationCoordinatorError>) -> Void) {
Benchmark.startTrackingForIdentifier("Login", message: "Started restoring previous login")
delegate?.authenticationCoordinatorDidStartLoading(self)
@@ -172,6 +174,7 @@ class AuthenticationCoordinator: Coordinator {
}
private func setupUserSessionForClient(_ client: Client) {
Benchmark.endTrackingForIdentifier("Login", message: "Finished login")
do {
let restoreToken = try client.restoreToken()
@@ -54,6 +54,8 @@ class UserSession: ClientDelegate {
self.mediaProvider = MediaProvider(client: client, imageCache: ImageCache.default)
client.setDelegate(delegate: WeakUserSessionWrapper(userSession: self))
Benchmark.startTrackingForIdentifier("ClientSync", message: "Started sync.")
client.startSync()
updateRooms()
@@ -89,6 +91,7 @@ class UserSession: ClientDelegate {
// MARK: ClientDelegate
func didReceiveSyncUpdate() {
Benchmark.logElapsedDurationForIdentifier("ClientSync", message: "Received sync update")
updateRooms()
}
@@ -101,7 +104,11 @@ class UserSession: ClientDelegate {
return
}
Benchmark.startTrackingForIdentifier("ClientRooms", message: "Fetching available rooms")
let sdkRooms = self.client.rooms()
Benchmark.endTrackingForIdentifier("ClientRooms", message: "Retrieved \(sdkRooms.count) rooms")
Benchmark.startTrackingForIdentifier("ProcessingRooms", message: "Started processing \(sdkRooms.count) rooms")
let diff = sdkRooms.map({ $0.id()}).difference(from: currentRooms.map({ $0.id }))
for change in diff {
@@ -117,6 +124,8 @@ class UserSession: ClientDelegate {
}
}
Benchmark.endTrackingForIdentifier("ProcessingRooms", message: "Finished processing \(sdkRooms.count) rooms")
DispatchQueue.main.async {
self.rooms = currentRooms
}
@@ -150,7 +150,11 @@ class RoomProxy: RoomProxyProtocol {
return
}
let messages = backwardStream.paginateBackwards(count: UInt64(count)).map { message in
Benchmark.startTrackingForIdentifier("BackPagination \(self.id)", message: "Backpaginating \(count) message(s) in room \(self.id)")
let sdkMessages = backwardStream.paginateBackwards(count: UInt64(count))
Benchmark.endTrackingForIdentifier("BackPagination \(self.id)", message: "Finished backpaginating \(count) message(s) in room \(self.id)")
let messages = sdkMessages.map { message in
self.messageFactory.buildRoomMessageFrom(message)
}.reversed()