Measure Request Latency With ContinuousClock
A request-duration dashboard looks excellent because the slow requests all throw before the timing code runs. The clock might be accurate; the measurement boundary is still wrong.
Kyle Browning’s ContinuousClock article is a useful prompt to audit elapsed-time helpers. Let’s take that idea one step further: preserve the operation’s result, include failures, and make the measurement’s meaning explicit.
Pick a clock for the question
Date represents an absolute point in time and is useful for a timestamp such as “last refreshed at.” Elapsed execution time is a different question. Swift’s Clock, Instant, and Duration proposal introduced clocks for that purpose in Swift 5.7.
ContinuousClock advances while the system is asleep. SuspendingClock excludes system sleep. Neither measures how much CPU time your particular task consumed. Waiting for a network response while the system is awake can contribute to both.
Also keep wall-clock adjustments separate from display settings: changing a time zone or crossing a daylight-saving boundary does not itself change the absolute instant represented by a Date. The reason to use a monotonic clock is not that date formatting has a different offset.
Record on every normal exit
This helper returns the operation’s value and propagates its error. Its defer records elapsed time in both cases:
import Foundation
func measured<Value: Sendable>(
record: @Sendable (Duration) -> Void,
operation: @Sendable () async throws -> Value
) async rethrows -> Value {
let clock = ContinuousClock()
let start = clock.now
defer { record(start.duration(to: clock.now)) }
return try await operation()
}
This version requires a Sendable operation and result as its concurrency contract. The recording closure is synchronous and should do very little work: update a small accumulator or enqueue a metric. Do not make every request wait for an analytics upload before the helper returns.
The timestamp is taken before calling the recorder, so the recorded duration excludes the recorder’s own work. A process killed mid-operation cannot execute this defer; the metric is not a durable audit trail.
Separate attempts from successful requests
The helper intentionally reports only a Duration. At its call site, record whether the attempt succeeded, failed, or was cancelled using a small bounded outcome vocabulary. Avoid attaching raw URLs, account IDs, or error descriptions as metric dimensions.
For a retried operation, choose whether you want one measurement per attempt, one for the entire user operation including backoff, or both. Give those metrics different names. Averaging an attempt duration and calling it “time until the user gets content” hides the waiting between attempts.
Keep cancelled operations in a separate category when analyzing latency. They can tell you that a screen repeatedly starts work the user never sees, but mixing them into successful-request percentiles can make an apparent improvement meaningless.
Keep Duration until you cross a boundary
Use Duration for arithmetic and comparisons within Swift. Convert units only when a logging or metrics interface requires a specific numeric representation, and name that unit at the boundary.
Do not store a ContinuousClock.Instant as a server timestamp or subtract an instant collected on another device. For cross-process or epoch work, consult the specific clock’s documented guarantees; a local request timer needs none of that machinery.
Similarly, timing an operation does not impose a timeout. A slow dependency will keep running unless a separate cancellation or deadline policy ends it. Cancellation remains cooperative.
Test behavior without asserting scheduler precision
The companion runs one successful operation and one that throws cancellation, then checks that both produced nonnegative measurements. It separately advances an instant by 125 milliseconds and verifies the resulting Duration exactly.
Those tests validate the helper’s bookkeeping. They do not assert that sleeping for 10 milliseconds resumes at precisely 10 milliseconds, or that a particular machine should finish work below an arbitrary threshold. Use a representative performance test for those product requirements.
September 2026 review: the helper passed its executable checks and type-checked for iOS 17 using Swift 6.4. It uses established clock APIs, and the fixture performs no real network benchmark.