Instruments概述

本文内容来自于学习《instrument user guide》

一、启动方式

  1. xcode:Xcode > Open Developer Tool > Instruments

  2. docker

  3. build时执行instrument

二、使用步骤

  1. 打开instruments,选择target

  2. 选择跟踪模板

  3. 采集数据

  4. 分析、检查数据

三、收集数据

  1. target可以选择系统所有进程或者单独app

这里需要注意的是无线连接iOS设备的方法:

1)通过USB将iOS设备连接到PC;

2)按住Option键,点击target -> 选择iOS设备,enable wireless;

3)target -> 选择iOS设备的无线版本;

4)断开iOS和PC的USB连接;

注意设备关机后,数据收集会停止,需要重新连接后重新收集数据。

  1. Dock中开启instruments

该方法支持后台收集、采样数据,支持的instruments模板有:

1)System Time Profile.

2)Time Profile Specific Process.

3)Automatically Time Profile Spinning Applications.

4)Allow Tracing of Any Process (10 hours).

  1. 使用iprofile收集数据

iprofiler是一个命令行工具,不需要打开instruments就可以监控 app的性能,数据保存在.dtps文件中。数据收集结束之后可以导入到instruments模板中,图形化展示。支持的模板有:1)Activity Monitor. 2)Allocations. 3)Counters. 4)Event Profiler. 5)Leaks. 6)System Trace. 7)Time Profiler.

iprofiler使用样例:

1)使用time profiler和activity monitor instrument收集所有进程的数据,最终保存为allprocs.dtps.

iprofiler -timeprofiler -activitymonitor

2)使用Time Profiler instrument收集app数据,数据采集时间为8s,数据保存为/temp/YourApp_perf.dtps.

iprofiler -T 8s -d /temp -o YourApp_perf -timeprofiler -a YourApp

3)使用leaks和activity monitor采集PID=823进程的数据,采样时间为2.5s,最终保存为YourApp_perf.dtps.

iprofiler -T 2500ms -o YourApp_perf -leaks -activitymonitor -a 823

4)使用Time Profiler 和 Allocations instruments采集app数据,采集时间为默认时间(默认时间为10s),数据保存为/tmp/allprocs.dtps.

iprofiler -d /tmp -timeprofiler -allocations -a YourApp.app

5)app的地址为/path/to,参量为arg1,使用Time Profiler 和System Trace instruments采集数据,采集时间为15s,但是只保存最后2s的数据,文件保存为YourApp_perf.dtps.

iprofiler -T 15 -I 1000ms -window 2s -o YourApp_perf -timeprofiler -systemtrace /path/to/Your.app arg1

  1. 将instrument工具对数据采集的影响最小化

1)降低采样间隔可以收集更多的数据,但是高采样率也可能会导致一些问题:每个采样都需要处理,高采样率消耗处理时间;采样间隔可能不一致; 采样数据过多会消耗内存;采样间隔的设置:打开Record Settings inspector

2)延迟模式:一般instruments都是在app执行的时候收集并且分析、展示数据。延迟模式下运行instruments是指等数据收集结束之后再分析数据,这种方式可以提高性能数据的准确性。设置方法:

为整个instruments设置deferred mode:instruments -> preferences > 勾选“always use deferred mode”;

为某一个trace设置derferred mode:file -> record options -> 勾选“deferred mode”;

四、检查数据

  1. 符号化

当跟踪数据里面显示的是地址而不是可读性较强的符号,因此需要将地址转换为符号。地址和 符号的映射保存在dSYM文件中。instruments工具一般会自动找到dSYM文件,然后将地址转换为符号,如果instruments工具没有成 功找到dSYM文件,可以手动设置:files -> symbols > 选择需要符号化的可执行文件或者framework > “select dSYM or containing folder” > 找到dSYM文件(dSYM文件默认会保存在build文件夹下)。

  1. 跟踪面板

1)在某一个时间点上添加标签flag:edit -> add flag;

2)放大、缩小:放大:按住shift键,拖拽选择要放大的数据;缩小:按住control键,拖拽选择要缩小的数据;

3)查看特定时间段内的数据:选择起点/终点 -> view -> set inspection range start/end

4)隔离观察一段数据:按住option键,选择一段数据,detail panel中显示这段时间内的详细信息。

  1. 详细信息面板

不同的instruments,详细面板中显示不同的信息。

1)extend detail inspector: view -> inspectors > show extend detail;

2)显示代码;

3)隐藏/显示系统调用堆栈;

五、保存、导出

  1. 保存单个跟踪文档;

  2. 保存instruments跟踪模板;

  3. 导出跟踪数据,导出格式为CSV文件(注意:不是所有的instruments都支持导出为csv文件);

  4. 导入数据:使用命令行工具采样数据之后,可以导入到instruments中进行查看;

  5. 自定义的instruments可以导出为脚本,然后用dtrace命令行工具执行该脚本。采样数据之后,可以导入到instruments进行查看。

六、定位内存问题

  1. 使用activity monitor检查内存使用情况

activity monitor跟踪cpu、内存、网络,可以跟踪所有进程或者单个进程。它有一系列的系统统计信息可供选择:Physical Memory Wired,Physical Memory Active,Physical Memory Inactive,Physical Memory Used,Physical Memory Free,Total VM Size,VM Page In Bytes,VM Page Out Bytes,VM Swap Used

  1. 内存不合理运用

Abandoned Memory,也就是存在已分配内存的引用,但实际上程序中不会使用。检测方法是重复进行一些操作heap不会持续增长。每次重复这些操作后,点击mark Generations button,会设置一个flag,然后查看每个迭代的详细数据。

  1. 内存泄漏

内存泄露即内存被分配了,但程序中已经没有指向该内存的指针,导致该内存无法被释放。使用leak instruments检测。倘若对象发生内存泄露,detail panel中会看到对象的retain release历史记录。倘若是非对象发生内存泄露,会看到malloc和free的调用历史

  1. Zombies

Zombie 问题即因程序员在代码中引用了“Zombie 对象”而导致应用程序崩溃。所谓 Zombie 对象,即已经deallocated 的对象,这些对象的 retainCount 都已经为 0,通过正常的手段我们无法在 debug 中跟踪和观察它们。zombies instruments可以用来跟踪这类问题。注意zombies instrumens使用debug模式,并且将环境变量NSZombieEnabled设为true。

七、检测I/O活动

  1. 网络使用

activity monitor instruments可以跟踪网络,网络相关的统计信息有:1)Net Packets In;2)Net Bytes In;3)Net Packets Out;4)Net Bytes Out;5)Net Packets In Per Second;6)Net Packets Out Per Second;7)Net Bytes In Per Second;8)Net Bytes Out Per Second

  1. 网络连接

检测iOS app如何使用TCP/IP 和 UDP/IP。与connections instruments一起使用,可以检测app发送和接收的包数目。

八、图像性能检测

  1. core animation graphics:用来检测帧频率

  2. OpenGL activity

  3. GPU Driver

九、CPU使用

  1. Performance Monitor Counters

  2. 电量:Energy Diagnostics Trace Template

跟踪电量、CPU、网络、显示亮度、睡眠/唤醒、蓝牙、wifi、GPS。

可以全天开启 Energy Diagnostics Log模式(在开发手机设备中,设置->开发者选项,该设置重启设备后会消失)。注意如果设备电量低、关机了,log数据会丢失。数据收集结束后, 将log数据传到PC上,使用energy diagnostics instruments分析该数据。

  1. 线程使用Multicore Trace Template

分析多核性能,比如线程状态、调度队列、块使用情况。Multicore Trace Template包含thread states和dispatch instruments。

  1. Time Profiler Trace Template检测内核使用情况

十、UI automation

这一部分大家应该用的比较多,这里放一张UI元素的层次结构图。

文章链接:http://www.jianshu.com/p/2f850a774fca
其他的一些好的资源:
http://www.jianshu.com/p/88634cf65e27
http://www.jianshu.com/p/39632cc74ce5
http://www.jianshu.com/p/ca1a3e91af6b
http://www.jianshu.com/p/be59588327d5
http://cdn.cocimg.com/bbs/attachment/Fid_6/6_24457_90eabb4ed5b3863.pdf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值