名称: instruments-profiling
描述: 使用 Instruments/xctrace 对原生 macOS 或 iOS 应用进行性能分析时使用。涵盖正确的二进制文件选择、CLI 参数、导出以及常见问题。
元数据:
short-description: 适用于 macOS/iOS 应用的 Instruments 性能分析
当用户希望对原生应用进行性能分析或堆栈分析时,使用此技能。
重点:时间分析器、xctrace CLI 以及选择正确的二进制文件/应用实例。
xcrun xctrace list templatesxcrun xctrace record --template 'Time Profiler' --time-limit 60s --output /tmp/App.trace --launch -- /path/To/App.appxcrun xctrace record --template 'Time Profiler' --time-limit 60s --output /tmp/App.trace --attach <pid>open -a Instruments /tmp/App.trace注意:xcrun xctrace --help 不是有效的子命令。请使用 xcrun xctrace help record。
常见问题:Instruments 可能分析错误的应用(例如,位于 /Applications 中的应用),如果 LaunchServices 解析到了不同的应用包。
请遵循以下规则:
xcrun xctrace record ... --launch -- /path/App.app/Contents/MacOS/App.app 包,请确保它是目标应用包:open -n /path/App.appps -p <pid> -o comm= -o command= 进行验证/Applications/App.app 和本地构建版本,请明确指定本地构建的路径。--template 'Time Profiler': 来自 xctrace list templates 的模板名称。--launch -- <cmd>: -- 之后的所有内容都是目标命令(二进制文件或应用包)。--attach <pid|name>: 附加到正在运行的进程。--output <path>: .trace 输出文件。如果省略,文件将保存在当前工作目录。--time-limit 60s|5m: 设置捕获时长。--device <name|UDID>: 在 iOS 设备上运行时必需。--target-stdout -: 将启动进程的标准输出流传输到终端(对 CLI 工具很有用)。xcrun xctrace export --input /tmp/App.trace --tocxcrun xctrace export --input /tmp/App.trace --xpath '/trace-toc/run[@number="1"]/data/table[@schema="time-profile"]' --output /tmp/time-profile.xml--attach。xctrace 可能需要开发者工具权限。time-profile 导出的文件可能非常庞大。xcrun xctrace list devices 和 --device <UDID>。xctrace --attach 附加。