Как инструментировать код через signpost-API os_signpost, чтобы свой интервал появился в Instruments?
Вы хотите, чтобы шаг импорта JSON появился как именованный интервал в треке Points of Interest в Instruments, как в выводе ниже, чтобы видеть его длительность на каждом прогоне.
Объясните, какой API это даёт, и напишите вызовы begin/end, порождающие интервал.
Points of Interest
| Import JSON |==================| 412 ms
| Import JSON |=========| 230 ms
| Import JSON |====================| 498 ms
Покажите, как вы порождаете интервал.
Создайте OSSignposter на категории .pointsOfInterest и оберните работу в пару begin/end — beginInterval до неё, endInterval с возвращённым state после. Именованный интервал тогда виден в треке Points of Interest.
- ✗Меряют через Date и print вместо signpost-API
- ✗Пишут одну строку лога и ждут, что она станет интервалом
- ✗Считают, что в Points of Interest попадают лишь события фреймворков Apple
- →Почему конечный signpost должен использовать state, возвращённый вызовом begin?
- →Чем трек Points of Interest отличается от инструмента Time Profiler?
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.