Skip to content

Commit 6779e39

Browse files
author
Mazvydas Bartasius
committed
Adds an extension that appends the duration of the run to the output.
1 parent 41d305e commit 6779e39

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Sources/Future/Future+Measure.swift

+15
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ public enum MeasureTimeScale {
3939

4040
extension Future {
4141

42+
/// After the future is run, appends the duration of the run to the output.
43+
public func measure() -> Future<(Response, TimeInterval)> {
44+
return Future<(Response, TimeInterval)> { callback in
45+
46+
let start = CFAbsoluteTimeGetCurrent()
47+
48+
self.run {
49+
let end = CFAbsoluteTimeGetCurrent()
50+
let duration = (end - start)
51+
52+
callback(($0, duration))
53+
}
54+
}
55+
}
56+
4257
/// After the future is run, it calls the `durationCallback` on the main queue, passing how long it took to execute the current future.
4358
public func measure(_ durationCallback: @escaping (TimeInterval) -> Void) -> Future {
4459
return Future { callback in

0 commit comments

Comments
 (0)