xcode - 如何以编程方式启动/停止仪器(时间分析器)?

标签 xcode macos profiling instruments xcode-instruments

有什么方法可以以编程方式启动/停止仪器分析吗?我需要以可靠的方式分析 OS X 代码的特定部分,但我似乎找不到任何 Instruments 文档来告诉我如何做到这一点。对于 CHUD/Shark,有一个编程 API 和一个命令行工具来支持这一点,但我在任何地方都没有看到仪器的等效工具? FWIW 我发现了 2009 年左右的一些旧论坛帖子,哀叹该领域缺乏 Instruments 功能,但没有更新的内容。

最佳答案

是的。查找 DTPerformanceSession。这是introduced with Instruments 4.0 。这是enhanced in Instruments 4.1 .

这些文档提供了示例代码:

CFStringRef process = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%d"), getpid());
CFErrorRef error = NULL;
DTPerformanceSessionRef session = DTPerformanceSessionCreate(NULL, process, NULL, &error);
DTPerformanceSessionAddInstrument(session, (CFStringRef)@DTPerformanceSession_TimeProfiler, NULL, NULL, &error);
CFMutableArrayRef instrumentIDs = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
CFArrayAppendValue(instrumentIDs, @DTPerformanceSession_TimeProfiler);
DTPerformanceSessionStart(session, instrumentIDs, &error);

// do something in your app

DTPerformanceSessionStop(session, instrumentIDs, &error);
DTPerformanceSessionSave(session, (CFStringRef)@"/tmp/myAppProfile", &error);
DTPerformanceSessionDispose(session, &error);

关于xcode - 如何以编程方式启动/停止仪器(时间分析器)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17292094/

相关文章:

iphone - 用 Shark 分析 iphone : Nothing happens after pressing start

python - 元组理解时间分析

ios - Xcode 中的栏按钮始终位于底部 Xcode 6.3 beta

swift - 使用已发布的结果为 ObservableObject ViewModel 编写单元测试

ios - 在 Xcode 6 spritekit 级别编辑器中使用 pdf 作为纹理源

ios - 将标签放置在 tableview 之上

macos - 在当前窗口/ View Controller 的不同窗口/ View Controller 中执行代码

ruby - 如何在 OSX 10.11 上安装 Jekyll?

python - Pyenv、Python安装失败: OpenSSL development header is not installed

c++ - reshape 微软的 Concurrency::diagnostic::span 也可以检测外部跨度