跳到正文
Skillpedia
通用编程7.4k · #13233

instruments-profiling

它能做什么

这个技能展示如何为原生 Apple 应用录制 Time Profiler 跟踪,并在 Instruments 中打开。适合调查运行缓慢或占用资源较多的原生应用的开发者。

适用场景

  • ✓macOS 或 iOS 应用运行缓慢,你需要查看时间都花在哪里的时候。
  • ✓你需要某个特定应用版本的跟踪,而不是另一个副本的跟踪的时候。
  • ✓你想用命令行录制应用性能数据的时候。

技能链接

https://github.com/steipete/agent-scripts/blob/main/skills/instruments-profiling/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/steipete/agent-scripts/blob/main/skills/instruments-profiling/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我的 Mac 应用打开项目时感觉很慢。请录制一份性能跟踪,并告诉我时间都花在哪里了。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体使用 xctrace 命令行工具录制跟踪,并附加到正确的应用二进制文件上,然后在 Instruments 中打开结果,进行调用栈分析。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Instruments Profiling (macOS/iOS)

Use this skill when the user wants performance profiling or stack analysis for native apps. Focus: Time Profiler, xctrace CLI, and picking the correct binary/app instance.

Quick Start (CLI)

  • List templates: xcrun xctrace list templates
  • Record Time Profiler (launch):
    • xcrun xctrace record --template 'Time Profiler' --time-limit 60s --output /tmp/App.trace --launch -- /path/To/App.app
  • Record Time Profiler (attach):
    • Launch app yourself, get PID, then:
    • xcrun xctrace record --template 'Time Profiler' --time-limit 60s --output /tmp/App.trace --attach <pid>
  • Open trace in Instruments:
    • open -a Instruments /tmp/App.trace

Note: xcrun xctrace --help is not a valid subcommand. Use xcrun xctrace help record.

Picking the Correct Binary (Critical)

Gotcha: Instruments may profile the wrong app (e.g., one in /Applications) if LaunchServices resolves a different bundle. Use these rules:

  • Prefer direct binary path for deterministic launch:
    • xcrun xctrace record ... --launch -- /path/App.app/Contents/MacOS/App
  • If launching .app, ensure it’s the intended bundle:
    • open -n /path/App.app
    • Verify with ps -p <pid> -o comm= -o command=
  • If both /Applications/App.app and a local build exist, explicitly target the local build path.
  • After launch, confirm the process path before trusting the trace.

Command Arguments (xctrace)

  • --template 'Time Profiler': template name from xctrace list templates.
  • --launch -- <cmd>: everything after -- is the target command (binary or app bundle).
  • --attach <pid|name>: attach to running process.
  • --output <path>: .trace output. If omitted, file saved in CWD.
  • --time-limit 60s|5m: set capture duration.
  • --device <name|UDID>: required for iOS device runs.
  • --target-stdout -: stream launched process stdout to terminal (useful for CLI tools).

Exporting Stacks (CLI)

  • Inspect trace tables:
    • xcrun xctrace export --input /tmp/App.trace --toc
  • Export raw time-profile samples:
    • xcrun xctrace export --input /tmp/App.trace --xpath '/trace-toc/run[@number="1"]/data/table[@schema="time-profile"]' --output /tmp/time-profile.xml
  • Post-process in a script (Python/Rust) to aggregate stacks.

Instruments UI Workflow

  • Template: Time Profiler
  • Use “Record” and capture the slow path (startup vs steady-state)
  • Call Tree tips:
    • Hide System Libraries
    • Invert Call Tree
    • Separate by Thread
    • Focus on hot frames and call counts

Gotchas & Fixes

  • Wrong app profiled: LaunchServices resolves installed app instead of local build.
    • Fix: use direct binary path or --attach with known PID.
  • No samples / empty trace: App exits quickly or never hits work.
    • Fix: longer capture, trigger workload during recording.
  • Privacy prompts: xctrace may need Developer Tools permission.
    • Fix: System Settings → Privacy & Security → Developer Tools → allow Terminal/Xcode.
  • Large XML exports: time-profile exports are huge.
    • Fix: filter with XPath and aggregate offline; don’t print to terminal.

iOS Specific Notes

  • Device: use xcrun xctrace list devices and --device <UDID>.
  • Launch via Xcode if needed; attach with xctrace --attach.
  • Ensure debug symbols for meaningful stacks.

Verification Checklist

  • Confirm trace process path matches target build.
  • Confirm stacks show expected app frames.
  • Capture covers the slow operation (startup/refresh).
  • Export stacks for automated diffing if optimizing.
常见问题

为什么可能分析到错误的应用?

技能警告,macOS 可能会解析到另一个应用副本,因此建议指定确切的二进制路径。

一次录制会持续多久?

示例使用时间限制,例如 60 秒。

它能附加到已经在运行的应用上吗?

可以。技能说明,在应用启动后,可以附加到进程 ID 上。

详情

作者的原始描述

Instruments/xctrace profiling: macOS/iOS traces, binaries, args, exports.

repo: steipete/agent-scripts

热度

GitHub 星标
7.4k
排名
#13233
许可证
MIT
更新时间
2026/10/30

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能