About Data Analysis
Instrument并不帮助我们解决代码问题。它帮助我们更好的理解我们的代码做了什么,当APP运行的时候,通过捕捉,统计详细的数据并呈现数据给我们进行分析。由于每个APP是不一样的,所以实际发现和处理问题的步骤也是不一样的。因此,我们必须学习怎样去理解Instrument为我们收集的数据,过滤不需要的数据,研究与APP相关的数据。我们必须执行一些检测工作,对于任意相关数据识别APP程序中的代码,以至于能够对代码进行改善。
在执行trace之后,我们可以检查在时间轴(timeline)面板,详情面板(detail pane),监督面板中扩展详情区域(extended detail area () of the inspector pane),中收集的数据,如Figure 10-1:
Figure 10-1A trace document containing collected data
建议:许多的instrument,当它们监控APP的时候,会周期性的捕捉堆栈踪迹。当我们确定了具体的数据点想要进一步研究,我们可以浏览堆栈踪迹来确定对应的代码部分。如果我们正在对Xcode打开的APP进行性能分析,我们经常可以在Instrument的右边查看源码或者调到Xcode中进一步检查。
Navigate the Timeline Pane
跟踪文件(trace document)中最突出的部分就是时间轴面板(timeline pane),它显示每个instrument基于时间收集的可视数据(Figure 11-1)。该面板非常容易识别APP中动态趋势和潜在的问题。例如:在内存使用图表中像钉子一样突出的部分表明了在该一段时间内你的APP比正常情况下分配了更多的内存。突出的部分可能正常,或者也表明了我们的代码创建了更多的对象,或者内存缓冲区超过了我们的预期。对于工具(Instrument)像Leaks能确定我们APP中哪个地方并没有合理的处理内存。如果Leaks的时间轴面板是稀疏构成,我们就明白我们APP的行为是合理的。相反,如果track不为空,而是变化很大,我们可能想要查找原因了。
The timeline pane of a trace document
Zoom In and Out
如果我们已经使用trace获取了数据,我们能够在时间轴面板放大或缩小被显示的详情,允许我们集中于具体部分更详情的内容(Figure 11-2)
Zooming into the timeline pane of a trace document
To zoom in and out of your data 为了放大和缩小数据,可以使用以下方法:
1:To zoom in or out,pinch the trackpad. 为了放大和缩小,触摸触控板,使用2根指头至于触摸板,2指向内缩小,相反放大
2:To zoom in or out, use the scroll wheel while pressing the Option key with the pointer positioned over the track view 为了放大和缩小,按住Option建通过滚动滚轮与指针定位跟踪视图。即按住Option,使用2指在触摸板上像左滑动缩小,向右滑动放大
3:To zoom in, press the Option key, and drag across the section of data you wish to isolate. 为了放大,按住Option键,并拖动想要放大的区域
4:To zoom out, press the Control key, and drag across a section of data 为了缩小,按住Control键,并拖动想要缩小的区域
To fit all recorded data in the timeline 在时间面板上显示所有的记录数据
1:Choose View > Snap Track To Fit (or press Control-Command-Z).
To increase the size of the data displayed in the timeline 增加在时间面板上显示数据的大小
1:Choose View > Increase Deck Size (or press Command-Plus Sign)
跟踪当前选中的instrument,增加显示view的高度:
To decrease the size of the data displayed in the timeline
1:Choose View > Decrease Deck Size (or press Command-Minus Sign).
Filter a Range of Data
当进行性能分析,instrument收集了大量的信息,我们可以进行设置详情面板快速过滤数据,只显示发生在具体时间段内的数据。如:Figure 11-3
Selecting a range of data in the timeline pane of a trace document
To select a time range for inspection
1:Drag across a section of data without using any modifier keys. Only the data you drag across is displayed. 拖过一段数据而不使用任何修改键,只有你拖动的数据显示。
instrument高亮了时间面板上的内容,那部分就是我们所指定的范围
To clear an inspection range
1:Click outside the selected range in the timeline pane 点击事件面板之外的区域
2:Choose View > Clear Inspection Range
Set Flags
旗帜标志允许我们迅速范围时间面板上的兴趣点(Figure 11-4),我们能够为每一个flag添加名字和描述。
Flags in the timeline pane of a trace document
To set a flag at the current playhead position in the timeline pane 在当前时间面板播放头位置设置旗帜
1:Choose Edit > Add Flag (or press Command-Down Arrow).
To navigate between your timeline flags 操作时间面板中的旗帜
1:Choose Window > Manage Flags (or press Shift-Command-T) to display the Flags palette.
2:Click a flag in the Flags palette to navigate to it in the timeline pane。点击弹出款中可以看到时间面板上所有的旗帜并可以进行相关操作
To delete a flag 删除flag
1:Drag the flag out of the timeline navigation bar
To hide a flag
1:Choose Window > Manage Flags (or press Shift-Command-T) to display the Flags palette
2:Deselect the checkbox next to a flag in the Flags palette to hide it in the timeline pane. 反选就可以隐藏flag
Access Trace Data for Multiple Runs
trace document在APP运行时能够记录多部分运行数据,如Figure 11-5.通过这样做,那么就不会抛弃之前的记录数据。
Viewing multiple runs in the timeline pane of a trace document
To view a list of runs for a selected instrument
1:Click the disclosure triangle to the left of the instrument in the strategy pane. 点击左侧的三角符号
To view the run data for a selected instrument
1:Click the disclosure triangle to the left of the instrument in the strategy pane 点击左侧三角符号
2:Select the desired run’s track in the timeline pane 在时间轴面板上选择所需的运行轨道
To move to the next run for the selected instrument
1:Choose Instrument > Next Run, or press Command-Single Quotation Mark (').
To move to the previous run for the selected instrument
1:Choose Instrument > Previous Run, or press Command-Quotation Mark (").
Navigate the Detail Pane 操作详情面板
在我们确认了时间轴面板上潜在的问题区域之后,使用详情面板来检测数据,如Figure 12-1所示。详情面板显示了与当前选中instrument的trace相关联的数据。instruments每次只能够显示一个在详情面板,所以我们需要自己切换不同的工具(instrument)来看不同的详情内容。
Viewing detail data for an instrument
To open or close the detail pane
1:Choose View > Detail
2:Click the Detail View button () at the right end of the toolbar 点击导航条右侧底部的详情视图按钮
To view data in the detail pane for a specific instrument
1:Click the instrument in the strategy pane.
View Different Types of Data in the Detail Pane 在详情模板看不同类型数据
对于有些instrument,我们能够使用不止一种的格式进行显示。例如: Activity Monitor instrument 允许我们看数据的概要,父子视图信息,和取样列表。
To switch between detail types
Sort in the Detail Pane
为了对在详情面板中显示的信息进行分类,可以根据数据所在的具体列,点击合理的列头(Figure 12-3)。每一个instrument所对应的详情面板中的列都不一样。在堆栈轨迹中我们可以隐藏系统调用通过点击按钮 在扩展详情区域中堆栈轨迹的顶部:
Hiding system calls in the extended detail area of the inspector pane
Map Data to Source Code 映射数据到源码
Instrument需要工程(project)的精准信息来提供最好的结果。如果系统能够看到所有与工程相关的symbols,我们将获取最完整的信息。当在跟踪文件(trace document)中通过instrument工具生成的是地址(addresses)而不是symbol,我们能够手动提供缺少的信息。映射地址到symbol是被包含在dSYM文件中。很显然,instrument自动找到dSYM文件,基于我们在工具参数( Instruments preferences)中设置的Spotlight indexes 和路径(path).当instrument不能够自动找到dSYM文件,我们可以自己手动指定instrument正确的方向。一旦我们这样做,instrument将能够映射地址到他们相关联的对象和具体的行信息。
To locate the dSYM path for an executable or framework
1:Choose File > Symbols. 注意:该命令只在我们运行了trace或者加载了之前保存过的trace 才会出现。
2:Select the executable (binary) or framework that is missing symbols.
3:Click the Locate button.
4:In the dialog that appears, select your symbol file or the folder that contains it.
5:Click Open.
为了正确的在trace document中显示symbol,instrument需要访问具体的symbol 文件,该文件是当我们进行测试所生成的。因此,当使用一个电脑创建需要在其他电脑上进行性能测试时我们需要手动定位dSYM文件。
在Symbol对话框中的过滤条中能够让我们对可执行(executable)和不完整的二级制(incomplete binaries)、框架(framework)进行相应的过滤.我们也可以通过输入文本内容来搜索具体的 binary、framework。
笔记:默契情况下,Xcode在Build folder中保存symbols到dSYM bundles。Xcode通过添加扩展名.dSYM到可执行名(executable name)创建默认的 dSYM bundle path.为了在Xcode生成dSYM bundles,进入工程中的Build Settings区域,在Build Options部分选择调试(Debug)信息部分Format > Dwarf with dSYM File.
See Your App's Source Code 查看对应APP源码
To see your app’s source code