Instruments User Guide编程指南4-Profile You App's Performance

来源:互联网 发布:老外看网络玄幻小说 编辑:程序博客网 时间:2024/05/16 17:14

Measure CPU Use 测量CPU的使用

有效使用的CPU允许你的应用程序(app)运行得更快和更有效。虽然你正在为特定平台编写一个应用程序,但是记住,即使是相同的通用平台都会有着不同的CPU性能。Instrument 帮助你管理处于各种情况的测试,允许你确认怎样更好的使用APP的多核,线程,和其他资源。

Track Core and Thread Use  跟踪多核和线程使用

时间分析器(Time Profiler)性能分析模板使用Time Profiler工具来执行系统cpu上低开销,基于时间的采样运行进程。随着时间的推移,有效地使用多核和线程,将有更好的APP性能。Time Profiler工具中的CPU和线程策略显示了APP怎样使用多核和线程。

To view core or thread usage  观察多核和线程的使用情况

1:Launch Instruments. 启动instrument
2:In the profiling template selection dialog that appears, click Time Profiler. 在弹出的性能分析模板框中选择Time Profiler
3:Choose your device and app from the target device and process lists. 在目标设备列表和进程列表中分别选择对应的设备和APP
4:Click Choose to create a trace document 点击Choose创建跟踪文件
5:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording. 点击记录按钮开始记录
6:Use your app or the system normally.  正常使用APP
7:Click the Stop button (image: ../Art/inline_stop_button_2x.png), or press Command-R again, when complete 当完成,点击停止按钮
8:Click the CPU or threads strategy button in the trace document toolbar  点击轨迹文档工具栏中的CPU和线程策略按钮
9:Examine the collected data.  检查收集的数据

使用CPU策略用法视图来比较给定周期内多核的使用情况。有效的使用多核并发将提高APP的性能。寻找不平衡的core的使用,如果一个core有着严重的使用区域,而其他core保持平静,说明该core所在的区域需要被较大的进行优化。当缩小的时候,activity可能显示多线程的发生情况,因此,当检测core的使用情况需要确保放大轨迹面板。

当APP处于工作当中,可以使用线程策略视图检测APP的线程使用情况。如果有大量的工作值主线程进行,会使我们的APP用户交互无反应或者响应过慢。所有无论什么时候,尽可能减少主线程的工作量。

Look for Performance Bottlenecks 寻找性能瓶颈

性能监控计数器(PMCs)是硬件寄存器,用于测量处理程序过程事件的发生。通过识别大量特定类型的事件,它们能够帮助我们发现APP中的性能瓶颈。例如:大量的条件分支指令可能表明一段逻辑,如果重新安排,可能会降低所需数量的分支。PMC事件能够发现这些问题,但是取决于我们怎样匹配它们到我们的代码并决定它们应该怎样帮助我们提高我们APP的性能。计数性能分析模板使用Couners工具来跟踪PMC事件。

To track PMC events

1:Launch Instruments 启动Instruments
2:In the profiling template selection dialog that appears, click Counters  选择点击Counters
3:Choose your device and app from the target device and process lists  在目标设备列表和进程列表中分别选择对应的设备和APP
4:Click Choose to create a trace document  点击Choose创建跟踪文件
5:Click the Counters instrument in the timeline pane. 在timeline面板中点击Counters工具
6:Press Command-2 to show record settings in the inspector pane. 使用instrument顶部导航条控制即可
7:In the record settings area of the inspector pane, click the Add button (+) in the Events and Formulas table 这里需要注意,需要使用真机,不然不能够添加对应的事件和公式
8:Select the event you’d like to count.  选择想要计数的事件
9:Repeat steps 7 and 8 to add more events, if desired. 重复7-8步骤
10:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording 开始记录
11:Use your app or the system normally  正常使用APP
12:Click the Stop button (image: ../Art/inline_stop_button_2x.png), or press Command-R again, when complete. 停止录制
13:Examine the collected data  检测APP

重要:能够被PMC事件跟踪的数量是由硬件决定。如果打算频繁记录相同的PMC事件,保存它们作为性能分析模板。否则,当我们关闭的时候原有配置和数据将被丢弃。对于更多有关保存性能分析模板, Save a Trace Document as a Profiling Template.

Other Ways to Measure CPU Use

Instrument提供了大量的其它性能分析模板以至于我们能够使用它们来检测我们APP CPU的使用情况。

1:Use the Activity Monitor profiling template to gauge overall CPU use in comparison to other apps.  使用Activity Monitor性能分析模板来测量CPU对比其它APP的整体使用情况
2:Use the Dispatch profiling template to see when your dispatch queues are executed. You can see how long the dispatched thread lasts and how many blocks are used.   使用Dispatch性能分析模板来观察我们的派发列队什么时候被执行。也可以看上一次线程派发执行了多长时间有多少block被使用
3:Use the System Trace profiling template to see when threads are scheduled, time active, core used, and more.  使用System Trace性能分析模板来查看线程安排,时间,多核等。

Measure Graphics Performance

在iOS APP中大量使用绘图将使我们的APP对比其它APP更具突出性。但是要合理的使用绘图资源,否则我们的APP运行将会变得非常慢,降低电量的生命,而且无论我们尝试渲染多好的内容都是徒劳无功,最理想的绘图性能表现:

1:Ensure that the frame rate of animations is sufficient for providing a fluid visual experience without negatively impacting app performance. 确保动画的帧率是足够提供流畅的视觉体验而并不会影响APP性能。
2:Reduce the number of views your app uses 减少APP中视图的使用数量
3:Reduce the use of opacity, such as in views that exhibit a translucent blur. If you need to use opacity, avoid using it over views that are updated frequently. Otherwise, energy cost is magnified, as both the background view and the translucent view must be updated whenever content changes.
减少透明度(opacity)的使用,比如:view显示半透明模糊效果。如果我们确实需要使用opacity,避免使用在更新过频繁的view上。否则,性能开销会增加,因为无论什么时候内容改变,背景视图和半透明视图都必须被更新。
4:Draw to smaller portions of the screen—only the portions that are changing. To do this, use needsToDrawRect: or getRectsBeingDrawn:count: to identify the specific area to update, and pass the result to drawRect:.  绘制屏幕上的小部分,可以使用needsToDrawRect: or getRectsBeingDrawn:count:方法来识别特定区域的跟新,然后传递给drawRect:方法
5:Eliminate drawing when your app or its content is not visible; for example, when your app is in the background, or when its content is obscured by other views, clipped, or offscreen. 当APP绘制它的内容是不可见的时候,减少绘制,例如:当你的APP在后天,或者内容被其它视图覆盖,剪切或者离屏。
6:Eliminate drawing during window resizing。 当window在改变大小期间减少绘制

Measure Core Animation Graphics Performance in iOS

核心动画(Core Animation)模板使用Core Animation和Time Profiler 工具测量APP绘图和CPU的性能。这个模板提供了迅速和轻量开始点进行测量内容渲染的帧率。它允许我们迅速查看哪里的渲染帧率比我们预期的要低。通过关联正在做的采样情况,我们能够确认我们的那部分代码需要被优化。

注意:使用Core Animation模板进行性能分析APP,需要使用真机。如果使用模拟器不能够得到真正有效的结果。不要使用Core Animation工具来测量OpenGL ES性能

To measure frame rate

1:Connect your iOS device to your Mac. 将iOS设备与MAC相连接
2:Launch Instruments 启动Instruments
3:In the profiling template selection dialog that appears, click Core Animation 在模板框中选择 Core Animation
4:Choose your iOS device and app from the target device and process lists
5:Click Choose to create a trace document.
6:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.
7:Use your app normally.
8:Click the Stop button (image: ../Art/inline_stop_button_2x.png), or press Command-R again, when complete
9:Examine the collected data.

在详情面板上将显示每秒采样的帧率

Correlate Interactions with Results

Core Animation工具捕获数据之后,我们可能看到在timeline面板上有钉子(spikes)一样的突出的部分,那里显示的是APP的帧率。在某些情况下,spikes能够被引起,由于改变设备的方向在水平和竖屏之间,如果我们并不知道这一点,我们可能会花费很多时间来寻找导致性能的问题。也可以在执行确定事件的时候,加入旗帜,See Set Flags.

Debugging Options

Core Animation在监控面板(inspector pane)中的显示设置区域(display settings)包含了大量的有用调试选项,你不需要运行跟踪你的iOS设备上使用这些选项。

Color Blended Layers:Shows blended view layers. Multiple view layers that are drawn on top of each other with blending enabled are highlighted in red. Reducing the amount of red in your app when this option is selected can dramatically improve your app’s performance. Blended view layers often cause slow table scrolling   显示混合视图图层,多个视图图层被绘制将显示高亮红色 。减少红色的数量能够极大的提高性能。混合视图图层经常导致列表滚动
Color Hits Green and Misses RedMarks views in green or red. A view that is able to use a cached rasterization is marked in green. 标记视图为绿色或红色,视图能够使用一个缓存的光栅化被标记为绿色
Color Copied ImagesShows images that are copied by Core Animation in blue. 
Color ImmediatelyRemoves the 10 ms delay when performing color-flush operations.  当执行color-flush操作去除10毫秒的延时
Color Misaligned Images Places a magenta overlay over images where the source pixels are not aligned to the destination pixels. 如果image上显示的是品红覆盖层那么源像素跟目标像素没有对齐
Color Offscreen-Rendered Yellow Places a yellow overlay over content that is rendered offscreen.  位于内容上的黄色覆盖层是离频渲染
Color OpenGL Fast Path Blue Places a blue overlay over content that is detached from the compositor.
Flash Updated RegionsColors regions on your iOS device in yellow when those regions are updated by the graphics processor. 当被图形处理器进行更新,对应区域将显示黄色

Measure OpenGL ES Activity in iOS 

在iOS APP中为了检测准确性和性能问题,OpenGL ES 分析模板是使用OpenGL ES Analyser和GPU Driver 工具来测量和分析 OpenGL ES activity ,同时也推荐怎样处理发现的相关问题。

To measure OpenGL Activity

1:Connect your iOS device to your Mac
2:Launch Instruments
3:In the profiling template selection dialog that appears, click OpenGL ES Analysis
4:Choose your iOS device and app from the target device and process lists.
5:Click Choose to create a trace document.
6:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.
7:Use your app normally, exercising your OpenGL ES graphics code
8:After a few seconds of measurement, click the Stop button (image: ../Art/inline_stop_button_2x.png), or press Command-R again
9:Examine the collected data

通过对严重性筛选,错误会被列举在详情面板,红色正方形表示是最严重的问题,橙色三角形是次要严重类型的问题。通过选中详情面板的问题,推荐和跟踪轨迹会被显示在监控面板的扩展面板区域。

Monitor Network and File I/O

Instrument提供了一些性能模板来监控APP的网络(network)和文件I/O(file I/O)活动。

Use Activity Monitor to Track Overall Network and Disk Use 使用活动监视器来跟踪网络和磁盘的使用

活动监视器性能分析模板使用Activity Monitor根据来跟踪整体的系统活动,包括CPU、网络、磁盘等。默认情况下,在时间轴面板(timeline pane)中Activity Monitor并不显示网络或磁盘活动。但是,我们可以手动允许显示这些统计数据。

To record network and disk use with the Activity Monitor instrument 使用Activity Monitor工具记录网络和磁盘的使用

1:Launch Instruments
2:In the profiling template selection dialog that appears, click Activity Monitor
3:Choose your device and app from the target device and process lists
4:Click Choose to create a trace document
5:Click the Activity Monitor instrument in the timeline pane
6:Press Command-2 to show record settings in the inspector pane
7:In the “Statistics to list” area, select the statistics you want to see. Then, select them again in the System Statistics area to enable them. 在Statistics to list区域选中想要显示的选项,然后再System Statistics再一次选中,即会显示在时间轴面板上。因为在Statistics to list区域选中的内容会出现在System Statistics区域,反选就会消失。如下图:

默认情况下在Statistics to list选中了以下四个部分,由于内容比较靠下,所以截图上看不到,然后再在System Statistics选中了,所以显示了监控的情况:





当我们自己手动在Statistics to list选中其他项内容,选中的内容自动会显示在System Statistics区域:



当在System Statistics区域区域选中就会显示在timeline 面板上:



下列选项用于统计文件I/O:

1:Disk read ops (operations)
2:Disk write ops
3:Disk bytes read
4:Disk bytes written
5:Disk read ops per second
6:Disk write ops per second
7:Disk bytes read per second
8:Disk bytes written per second

下列选项用于统计网络I/O:

1:Net packets in (received)
2:Net packets out (sent)
3:Net bytes in
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

8:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.
9:Use your app normally.
10:Click the Stop button (image: ../Art/inline_stop_button_2x.png), or press Command-R again, when complete
11:Examine the collected data.

时间面板显示视觉图表来指示活动。综合的活动信息可以在详情面板发现。在详情面板的导航条上可以看到采样的频率,所有被获取的数据都是以该采样频率进行的。

Monitor Network Connections of an iOS App

网络性能分析模板使用Connections工具分析iOS APP中的TCP/IP 和 UDP/IP的连接.

To record network connections used by your iOS app

1:Connect your iOS device to your Mac. You can use a physical or wireless connection. See Target an iOS Device Wirelessly. 将设备与Mac相连,可以使用真机也可以使用无线进行连接
2:Launch Instruments  启动Instruments
3:In the profiling template selection dialog that appears, click Network。注意:不要找Connections,是Network工具。
4:Choose your iOS device and app from the target device and process lists 选择设备和APP
5:Click Choose to create a trace document. 创建跟踪文件
6:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording. 开始记录
7:Use your app normally. 正常使用APP
8:Click the Stop button (image: ../Art/inline_stop_button_2x.png), or press Command-R again, when complete. 停止记录
9:Examine the collected data. 检测数据

Monitor Disk Use in OS X

File Activity性能模板使用File Activity, Reads/Writes, File Attributes, and Directory I/O instruments来观察OS X APP磁盘使用

To record the file activity of your OS X app

1:Launch Instruments.
2:In the profiling template selection dialog that appears, click File Activity.
3:Choose your Mac and OS X app from the target device and process lists.
4:Click Choose to create a trace document.
5:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.
6:Use your app normally.
7:Click the Stop button (image: ../Art/inline_stop_button_2x.png), or press Command-R again, when complete.
8:Click individual instruments in the timeline pane to view their activity in the detail pane.Each instrument provides an event list and a call tree, which you can use to analyze specific instances of file activity.
9:Select an event you want to inspect in the detail pane.
10:Press Command-3 to display a stack trace for the selected event in the extended detail area of the inspector pane.
11:Click the Collapse button (image: ../Art/inline_hidesystemcalls_button_2x.png) in the extended detail area to hide system calls in the stack trace. This makes it easier to locate your app’s methods.Calls made by your app are colored black and preceded by a user code icon (image: ../Art/inline_usercode_icon_2x.png).
12:Double-click a method in the stack trace to display its code in Instruments.
13:Click the Xcode button (image: ../Art/inline_xcode_2x.png) at the top of the detail pane to open the code in Xcode for editing.



0 0