Put Signposts Around The Work Users Actually Wait For
A refresh feels slow, but the network request finishes quickly. The missing time may be decoding, waiting for an actor, applying database changes, or publishing a new model. Measuring just the request answers a narrower question than the person waiting for the screen.
Choose the user-visible boundary first, then put a signpost around the work that actually belongs to it.
Own each interval in one scope
Apple’s OSSignposter API creates events and intervals that Instruments can display alongside other activity. Give an interval a stable name and a unique ID for each invocation.
import Foundation
import os
struct RefreshTrace: Sendable {
let signposter = OSSignposter(subsystem: "example.reader", category: "Refresh")
func measure<Value: Sendable>(
_ operation: @Sendable () async throws -> Value
) async rethrows -> Value {
let id = signposter.makeSignpostID()
let state = signposter.beginInterval("Refresh", id: id)
defer { signposter.endInterval("Refresh", state) }
return try await operation()
}
}
The state belongs to this invocation, so two overlapping refreshes do not overwrite a shared currentInterval property. defer pairs the end with the beginning when the operation returns or throws.
The wrapper preserves both the returned value and the thrown error. It does not turn cancellation into success or swallow a failure merely to finish the measurement.
Await everything the measurement promises
Put request, decoding, and model publication inside the measured operation if the interval is intended to represent all three. The publication must be awaited too. Starting an unstructured task that updates the screen later lets the interval end before its advertised work finishes.
There is another useful distinction: publishing model state is not the same as displaying a rendered frame. If your metric is “new content first appeared,” use a separate, carefully defined presentation marker. Do not rename a model-update interval to imply you measured rendering.
Write down the boundary in the code review. “Refresh finishes when the latest accepted model has been applied” is specific enough for another developer to interpret the trace.
Cancellation ends when the operation unwinds
Calling cancel() sets a task’s cancellation state; it does not immediately execute every defer in that task. A dependency that ignores cancellation can keep the interval open until it returns.
That can be useful evidence. A long interval after dismissal may reveal abandoned work still consuming resources. Do not forcibly end the interval at the cancel button and then conclude that the operation stopped there.
Normal error unwinding balances this wrapper. Process termination is a different boundary and cannot be repaired with a defer. Apple’s endInterval documentation also describes matching names and interval state; mismatched instrumentation can create misleading traces or runtime assertions.
Read elapsed time as elapsed time
An interval around async work includes suspension and waiting. A 400-millisecond interval does not establish 400 milliseconds of CPU execution.
Use the interval to locate the relevant span, then correlate it with the profiler and thread activity. If it contains a long idle gap, investigate the dependency or queue. If it contains sustained decoding work, inspect that work. The same duration can have very different causes.
Add nested intervals only when they answer a question: decoding, database commit, or a particular expensive transformation. Instrumenting every tiny helper creates a trace that is harder to read and can perturb the workload you wanted to observe.
Keep names stable and metadata small
Use a fixed name such as Refresh, not a document title, URL, or account identifier. Stable categories make repeated operations comparable and avoid turning user content into diagnostic labels.
Capture a representative workflow with overlapping requests, an error, and a cancellation. Verify that the trace contains the pairs you expect before using it to support a performance claim. Repeat on a release configuration and representative hardware when judging a change.
September 2026 review: the wrapper type-checked for iOS 17 and its executable preserved successful results and thrown cancellation. No Instruments recording was captured for this article, so it reports no measured app latency or device performance improvement.