instruments 之 points of interest 使用

📅 2026/8/12 18:15:23
instruments 之 points of interest 使用
Points of Interest 是什么Instruments 中的Points of InterestPOI关注点可以理解为由你主动埋在代码里的“性能时间标记”。Instruments 本身能够告诉你 CPU、内存、线程在什么时候繁忙但它并不知道那一刻你的 App 正在执行什么业务操作。通过 POI你可以在时间轴上标记用户点击了“发送消息”首页开始加载图片解码开始和结束列表首屏渲染完成页面切换开始和结束这样就可以把业务行为和 CPU、卡顿、网络、线程活动对应起来。POI 并不是一种新的底层采集机制它本质上使用的是OS Signpost。当 Signpost 被写入.pointsOfInterest分类时Instruments 会把它突出显示在 Points of Interest 轨道中。Apple 目前推荐使用OSSignposterAPI。(Apple Developer)因些Points of Interest 本身不负责发现性能问题但它能显著提高分析效率。两种标记类型1. Event一个瞬间表示某件事情在某个时刻发生了没有持续时间。例如signposter.emitEvent(User Tapped Send)在 Instruments 时间轴上它表现为一个时间点。适合用户点击按钮页面出现收到推送开始一次刷新缓存命中网络请求失败emitEvent的含义就是“在当前时刻记录一个关注点”。(Apple Developer)2. Interval一段持续时间表示一项工作从什么时候开始到什么时候结束。图片解码开始 ├──────────────┤ 图片解码结束适合页面加载JSON 解析图片解码数据库查询网络请求列表布局动画执行App 启动后的初始化工作Apple 的OSSignposter通过beginInterval和endInterval创建区间Instruments 会把它显示成一段可选择的时间范围。(Apple Developer)iOS 15 以上使用 OSSignposter1. 创建统一的 Signposterimport os enum AppSignpost { static let signposter OSSignposter( subsystem: Bundle.main.bundleIdentifier ?? com.example.app, category: .pointsOfInterest ) }这里有两个关键参数subsystem通常使用 App 的 Bundle IDcategory使用.pointsOfInterest.pointsOfInterest是系统专门用于 Signpost 和 Instruments POI 轨道的分类。(Apple Developer)2. 标记一个瞬间事件例如记录用户点击发送按钮func sendButtonDidTap() { AppSignpost.signposter.emitEvent(Send Button Tapped) sendMessage() }或者加入一些附加信息func sendButtonDidTap(conversationID: Int) { AppSignpost.signposter.emitEvent( Send Button Tapped, conversationID: \(conversationID) ) sendMessage() }第一个字符串Send Button Tapped必须是静态字符串动态数据应该放在后面的 metadata 中。OSSignposter支持在 metadata 中使用字符串插值以及隐私选项。(Apple Developer)3. 测量一段同步代码例如测量 JSON 解析func parseResponse(_ data: Data) throws - Response { let state AppSignpost.signposter.beginInterval( Parse Response, dataSize: \(data.count) ) defer { AppSignpost.signposter.endInterval( Parse Response, state ) } return try JSONDecoder().decode(Response.self, from: data) }使用defer很重要因为无论方法正常返回还是抛出错误都能保证 interval 被结束。在 Instruments 中会看到Parse Response ├──────── 18 ms ────────┤4. 使用 withIntervalSignpost对于同步闭包写法可以更简单func parseResponse(_ data: Data) throws - Response { try AppSignpost.signposter.withIntervalSignpost( Parse Response, dataSize: \(data.count) ) { try JSONDecoder().decode(Response.self, from: data) } }withIntervalSignpost会自动包围闭包负责开始和结束 interval特别适合短小、同步的测量代码。(Apple Developer)5. 测量 async 任务func loadUserProfile(userID: Int) async throws - UserProfile { let signpostID AppSignpost.signposter.makeSignpostID() let state AppSignpost.signposter.beginInterval( Load User Profile, id: signpostID, userID: \(userID) ) defer { AppSignpost.signposter.endInterval( Load User Profile, state ) } let data try await apiClient.requestUser(userID: userID) return try JSONDecoder().decode(UserProfile.self, from: data) }这里显式创建了signpostID。当同名任务可能并发执行时例如同时下载多张图片、加载多个用户资料需要为每个任务生成不同 ID。否则 Instruments 无法正确区分每一对开始和结束标记。只有在同名 interval 绝不可能重叠时才适合使用默认的.exclusive。(Apple Developer)几个容易踩的坑1. begin 和 end 名称必须一致let state signposter.beginInterval(Load Messages) // 错误 signposter.endInterval(Load Message, state)在 Debug 构建中OSSignposter会检查名称、状态和对应的 Signposter 是否一致不匹配可能触发运行时断言。(Apple Developer)2. 同名并发任务不要都使用默认 ID多个并发图片下载都叫Download Image时应使用let id signposter.makeSignpostID()而不是全部依赖.exclusive。(Apple Developer)3. Signpost 名称不能使用普通动态字符串下面不能直接使用let name Load \(pageName) signposter.beginInterval(name)因为 name 要求是StaticString。应改成signposter.beginInterval( Load Page, pageName: \(pageName) )4. 可以在 Release 中关闭需要完全关闭时可以使用#if DEBUG static let signposter OSSignposter( subsystem: Bundle.main.bundleIdentifier ?? com.example.app, category: .pointsOfInterest ) #else static let signposter OSSignposter.disabled #endifApple 提供了OSSignposter.disabled用于在生产构建中关闭 Signpost而不必修改调用代码。(Apple Developer)最简单的心智模型Instruments 指标 告诉你系统什么时候忙。 Points of Interest 告诉 Instruments那时候你的 App 正在做什么。 Event 某件事发生了。 Interval 某件事执行了多长时间。