session-ios/SignalMessaging/utils/Bench.swift
2018-10-03 08:59:55 -04:00

31 lines
787 B
Swift

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
/// Benchmark async code by calling the passed in block parameter when the work
/// is done.
///
/// BenchAsync(title: "my benchmark") { completeBenchmark in
/// foo {
/// completeBenchmark()
/// fooCompletion()
/// }
/// }
public func BenchAsync(title: String, block: (@escaping () -> Void) -> Void) {
let startTime = CFAbsoluteTimeGetCurrent()
block {
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
Logger.debug("[Bench] title: \(title), duration: \(timeElapsed)")
}
}
public func Bench(title: String, block: () -> Void) {
BenchAsync(title: title) { finish in
block()
finish()
}
}