How do you instrument code with the signpost API os_signpost so a custom interval shows in Instruments?
You want your JSON-import step to appear as a named interval in the Points of Interest track in Instruments, like the readout below, so you can see its duration on each run.
Explain which API produces this and write the begin/end calls that emit the interval.
Points of Interest
| Import JSON |==================| 412 ms
| Import JSON |=========| 230 ms
| Import JSON |====================| 498 ms
Show how you emit the interval.
Create an OSSignposter on the .pointsOfInterest category and wrap the work in a begin/end pair — beginInterval before it, endInterval with the returned state after. The named interval then shows in the Points of Interest track.
- ✗Timing with Date and print instead of a signpost API
- ✗Emitting a single log line and expecting it to become an interval
- ✗Assuming only Apple framework events can appear in Points of Interest
- →Why must the end signpost use the state returned by the begin call?
- →How does the Points of Interest track differ from the Time Profiler instrument?
Use OSSignposter (iOS 15+) on a log tied to the Points of Interest category. The begin call returns an OSSignpostIntervalState that you must hand to the matching end call — that pairing is what defines one interval:
import OSLog
let signposter = OSSignposter(subsystem: "com.myapp.import", category: .pointsOfInterest)
func importJSON(_ data: Data) throws {
let state = signposter.beginInterval("Import JSON")
defer { signposter.endInterval("Import JSON", state) }
try decodeAndStore(data) // the work being measured
}
Profiling with the os_signpost / Points of Interest instrument now draws one bar named Import JSON per call, with its measured duration. Pre-iOS-15 code does the same with os_signpost(.begin, log:, name:) and os_signpost(.end, …). Signposts cost almost nothing when Instruments is not attached, so they can stay in the code.