Create, Save, and Open Trace Documents
对于instrument,所有的工作都在trace document中进行,在trace document中包含了一系列的instrument。它们被配置好并且收集了数据。一旦使用trace document分析(profile)了APP,我们能够保存document来保存我们收集的trace data,并可以在之后打开用于观察和分析。
About Profiling Templates
当Instruments首先启动的时候,它提供了一系列的性能分析模板(see Figure
6-1).每一个模板都包含了一个工具集合能够用于分析APP和获取有用的信息来确认问题或使的APP执行更加有效。
Figure 6-1The
profiling template selection dialog
无论什么时候为了特定的目标我们需要收集数据,我们应该计划使用分析模板作为起点。例如:如果我们想确认APP的内存问题,我们应该选择与内存相关的模板(memory-related
template),比如:Allocations, Leaks, or Zombies。在使用了模板创建trace document之后,当需要的时候,我们能够经常添加或去除独立的instrument或重新配置instrument。
Create a Trace Document
我们需要创建trace document来进行分析:To create a trace document
1:Launch Instruments. 启动Instruments
2:In the profiling template selection dialog that appears, select a target device and process. 在弹出的性能分析模板对话框中选择target和process。
3:Select the desired profiling template. 选中需要的性能分析模板
4:Click Choose. 点击choose,到这里新的document已经被创建并且配置了我们刚刚选中的instrument。
建议:为了在创建trace document之后自动开始APP性能分析,在性能分析选择对话框中可以按住option建。当我们操作之后,choose按钮变为了Profile.点击Profile创建document并且初始化,然后立马自动进行性能分析。如果我们找不到我们需要的模板或仅仅是想创建一个自己的模板,我们可以选择Blank template.
一旦我们创建了trace document,我们能够保存它并且再之后进行引用。Instruments保存document作为使用.trace扩展名的Instruments
trace文件
To save a trace document
2:Choose File > Save. Or, choose File > Save As if you want to save a previously saved trace document
as a new file.保存文件使用File > Save或者File > Save As保存之前的跟踪文件作为新的文件
3:Enter a name for the file. 输入文件名
4:Choose a destination for the file. 选择文件存储位置
5:Click Save. 点击保存
Save a Trace Document as a Profiling Template
如果我们创建trace document,考虑到需要再次用于分析其他APP,我们能够保存trace document作为自定义模板,这样我们就没有必要在运行Instrument的时候再一次创建跟踪文件。
To save a trace document as a profiling template
2:Choose File > Save As Template.
3:Enter a name for the template.
4:Choose a destination for the template. 选择模板的存储位置
Save your template in the /Users/
<username>/Library/Application
Support/Instruments/Templates
directory to make it available in the custom section of the Instruments template window. This is the default directory in the Save As Template dialog.
5:Select an icon for the template. 选择模板的icon
6:Enter a description for the template. 输入模板的描述信息
7:Click Save. 点击保存
Once you save a template, it automatically appears in the profiling template selection dialog, grouped with the Custom templates. Next time you create a trace document,
you can select it as your starting point. 一旦我们保存了模板,它将自动显示在性能分析模板选择对话框中国,属于自定义模板部分,下一次当我们创建跟踪文件,我们能够选择它作为起始点。
Close a Trace Document
To close an opened trace document
1:Choose File > Close (or press Command-W).
2:If your document contains unsaved data, select whether to save the updated document. 如果文件中包含了未存储的数据,选择是否保存更新文件。
Open a Trace Document
To open a saved trace document 打开保存的跟踪文件,以下任意方式都可以:
1:在Finder中双击.trace文件
2:拖动.trace文件到Instrument图标APP
3:打开Instruments并在执行下列步骤:
1)Choose File > Open (or press Command-O).
2)Locate a saved .trace
document
file.
Target Devices and Processes
为了使用trace document来分析,我们需要把它作为目标(target).简单点理解就是,我们需要告诉Instruments 我们想要分析什么,对哪里进行分析,比如:是设备或者模拟器。
当我们创建了一个新的trace document,Instruments会显示性能分析模板选择对话框。在对话框的顶部包括了一个target列表和target process列表(如:Figure
7-1),这允许我们当选择了模板之后指定目标(terget)。这些列表包括了任意的设备,有过配置的,或者与mac相连的设备,APP扩展,活跃的process,系统的process。
Figure 7-1Target device and process lists in the profiling template selection dialog
Target list对于每一个trace document都有一个工具栏(see Figure
7-2),允许我们在trace document的任意时间改变target。
Figure 7-2Target device and process
lists in the trace document toolbar
To target a device and a process
1:Click the target device list or target process list, and select the desired device and process. 点击target设备列表或者target process列表,选择需要的设备和process(程序)
有一些模拟器允许我们把所有的应用程序作为目标(target),如果我们想这么做,在Target Processes中选择All Processes
如果想要的程序不在列表中,在target process list中选择Choose
Target ,这会显示一个对话框,允许我们浏览任意程序,我们也可以为程序配置环境变量。
建议:如果我们尝试着分析(profile)APP,但是APP并没有运行,instrument将自动启动程序。
Target an iOS Device
为了将iOS设备作为目标(target),需要将设备与Mac相连。一旦建立连接,设备将显示在目标设备列表( target device list)中,如下图所示:
Figure 7-3Targeting an iOS device in Instruments
Target an iOS Device Wirelessly
Instruments也允许我们进行无线iOS设备数据收集,无线性能分析在以下情况非常有用:
Accelerometers:Move
the device in all directions without its being tethered 向各个方向移动设备没有被限制
Accessories:Plug
your USB accessory into the free slot and test it.
To enable an iOS device for wireless profiling
1:Make sure your iOS device is connected to your development Mac by a USB or Lightning cable.
确保你的iOS设备通过USB连接到您的开发Mac或电缆。
2:Press the Option key, and click the target device list. 按住Option建,并且点击目标设备列表
3:Select your mobile device to enable wireless profiling for it. 选中自己的移动设备允许无线性能分析
4:Click the target device list again to open it, select the wireless version of your device,
and select a process.
5:Disconnect the device from the USB or Lightning cable. You should now be able to profile the process wirelessly on your device. 去除设备与mac的链接,现在可以进行无线分析
Target an Apple Watch
在对Apple Watch进行instrument之前,必须提供一个Apple Watch
To prepare an Apple Watch for profiling
1:Ensure that your Apple Watch is paired with your development iPhone.
2:Quit Instruments.
3:Launch Xcode.
4:Connect the iPhone that’s paired with your Apple Watch to your development Mac by a USB or Lightning cable.
5:Choose
Window > Devices (or press Shift-Command-2) to open the Devices window in Xcode.
6:Wait
for the iPhone to appear in the Devices list.
7:Wait for the Apple Watch to appear as a paired device for the iPhone.
8:Wait for Xcode to indicate that the Apple Watch has been prepared. This process may take several minutes.
9:Disconnect the iPhone from
the USB or Lightning cable.
10:Connect the iPhone to the USB or Lightning cable again.
一旦Apple Watch已经准备进行性能分析,启动instrument,Apple
Watch应该显示在设备目标列表中,如下:
Other Ways to Target
这一节,我们已经学了怎样在Instruments中指定具体的目标APP和设备。我们也可以在没有实际的APP输入从外部选择APP设备,,有以下方式:
Access and Use Individual Instruments
在跟踪文件(trace document)内,独立的数据收集模块称之为instruments收集APP数据,过程(process),设备超时。每一个instrument收集并显示具体类型的信息,比如:文件的访问,内存使用,网络的活跃。Instrument包括大量的工具库,每一个工具都能够满足大多数开发者的需求。对于高级开发者,使用DTrace聚集其它类型的数据开发自己的自定义instrument。
注意:有一些Apple Apps,比如:iTunes, DVD Player, QuickTime,为了保护敏感或者版权数据这些APP将阻止Dtrace的数据收集。
Display the Library Palette
工具库(Figure
8-1)显示了所有我们能够添加到跟踪文件中的工具。包含了系统自带的标准instrument和自定义instrument。
Figure 8-1The Library palette
To display the Library palette
为了显示库,可以使用以下任一方法:
1:Click the Add Instrument button (+) in the trace document toolbar.
2:Choose Window > Library (or press Command-L).
Find Instruments in the Library Palette
在工具库中的显示的instrument列表是非常的长,如果我们添加了自定义的instrument到库中,那么instrument工具就更多了。所用我们可以使用底部过滤区域迅速找到我们所需要的instrument,基于名字,描述,关键字
Figure 8-2Filtering for instruments
in the Library palette
Change the Library View Mode
instrument在库中能够被显示成icon, 或没有名字和描述。点击instrument库底部的视图按钮 (),可以显示每一个instrument对应的icon,如(Figure
8-3).为了再一次显示icon,姓名,描述,点击列表视图按钮 ()
Figure 8-3The library palette in
icon view
、
Add an Instrument to a Trace Document
不管是基于模板创建了跟踪文件还是空的跟踪文件,我们都可以添加新的instrument到跟踪文件以扩展功能。
To add an instrument to a trace document
1:Open the desired trace document. 打开需要的跟踪文件
2:Display the Library palette. 显示工具库
3:Find the desired instrument. 找到需要的instrument
4:Drag the instrument from the Library palette to the desired position in the strategy pane instruments list in your trace document. 从工具库拖动instrument到跟踪文件中的策略面板位置中。
Remove an Instrument from a Trace Document
当trace document在工作的时候,是非常容易去除instrument。
To remove an instrument from a trace document
1:Open the desired trace document. 打开跟踪文件
2:Select the instrument you want to delete in the strategy pane instruments list. 选中想要删除的instrument
3:Press the Delete key, or choose Instrument > Delete instrument
name Instrument. 按住删除键或者Instrument > Delete instrument
name Instrument
4:When prompted, click OK to confirm that you really want to remove the instrument. 在弹出框选着confirm进行删除
Configure an Instrument’s Record Settings
只要添加instrument到跟踪文件,工具就已经准备好使用了。在某些情况下,我们可能需要为instrument自定义设置。例如:可能希望为Activity Monitor instrument调节采样率或者在分析 Allocations
instrument时为了空闲内存而丢弃事件。不同的instrument有着不同的可配置性,许多的instrument都包含了控制收集什么类型的信息。也可以在检测区域中的录制设置区域里调节这些设置。
Figure 8-4Record
settings for the Activity Monitor instrument
Configure an Instrument’s Display Settings
在记录了trace之后,Instrument将显示结果在详情面板中。根据Instruments的不一样结果也不一样。它们可能包括概要(summary),调用树(call tree),采样列表(sample list),许多的数据都显示在这里。为了帮助我们获取正确的信息,Instrument经常允许我们进行调节,对信息进行过滤。如果Instrument支持这些操作,就像(Figure
8-5)显示一样。
Figure 8-5Display
settings for the Activity Monitor instrument
Record, Pause, and Stop Traces
Record a Trace
当准备开始分析APP的时候,需要初始化记录(recording)。在跟踪文件(trace document)中的instrument将会更具事先的配置对APP进行监控。当记录发生,时间轴面板和详情面板将更新提供实时捕获的视图。
To begin recording
下面任一方法进行开始记录:
1:Click the Record button ()
in the trace document toolbar. 直接点击跟踪文件中的记录按钮
2:Choose File > Record Trace (or press Command-R). 选择File
> Record Trace
Press Command-R again to stop recording. 按压 Command-R停止记录
Set Recording Options
设置记录选项是为了提高跟踪文件中数据的收集。可以在记录之前,选择延时,记录的时长,保存数据量的多少,在延时模式下是否记录数据等。
To set the recording options for a trace
1:Choose File > Record Options to open the Record Options
dialog.
2:Customize the settings as desired. 定制设置需求
Options include:
Time limit: The
maximum amount of time, in seconds, that the instruments will record data 最大的时间,以秒为单位,instrument将记录数据
Window limit: The
maximum duration, in seconds, of data that will be saved. If recording continues for longer than the time you specify as the window limit (for example, longer than 4 seconds), only the data collected during the last 4 seconds is saved 最大持续时间,以秒为单位,数据将被保存。如果记录持续时间比所指定的时间窗的限制(例如,超过4秒)更长,只有在最后4秒保存收集的数据。
Deferred mode: Select
to defer data analysis until after data collection is complete. This can result in faster and more accurate data collection, as more system resources are directed at the process being recorded. 选择推迟直到数据收集完成后进行数据分析。随着越来越多的系统资源针对过程被记录下来,这会更快和更准确的数据收集。
3:Click OK to close the Record Options dialog. 点击OK关闭记录选择对话框
Pause a Trace
如果有必要的话,我们可以进行暂停和恢复记录。
To pause an active recording
1:Click the Pause button ()
in the trace document toolbar.
2:Choose File > Pause Trace (or press Shift-Command-R).
Stop a Trace
一旦记录了所需要的数据,那么开始停止记录开始数据分析。
To stop an active recording
1:Click the Stop button ()
in the trace document toolbar.
2:Choose File > Stop Trace (or press Command-R).
Profile from the Dock
我们可以在Dock中点击Instrument图标来触发Time Profile,在后台自动记录某些事件。
To collect Time Profiler information from the Dock
1:With Instruments open, Control-click the Instruments icon in the Dock. 点击Dock中的Instruments打开Instruments。
2:From the shortcut menu that appears, choose the process to profile to start recording. 从显示的快捷菜单中选择需要分析的程序
Options include:
1:System Time Profile. Starts
profiling all system processes. 开始分析所有程序
2:Time Profile Specific
Process. Starts the Time Profiler instrument with a specific app from the submenu. 从子菜单栏指定具体的APP进行性能分析
3:Automatically
Time Profile Spinning Applications. Automatically profiles blocked (spinning) apps in the future. 在将来自动分析阻塞APP
4:Allow Tracing of Any
Process (10 hours). Trace any process that occurs in the next 10 hours. Bypasses having to enter a password during the 10 hours. 跟踪任何过程,发生在未来10个小时。绕过必须输入一个密码在10个小时。
Profile from Xcode
Instrument是Xcode开发流程中主要的组成部分,我们可以直接从Xcode中进行使用用于测试APP。
Profile from the build and product menus
Xcode编译菜单栏允许我们运行,测试,分析APP,也允许我们profile APP。
To profile your app from the Xcode build and product menus
1:Open an app project in Xcode.
2:Do one of the following:
1)Click and hold the build menu in the Xcode main window, and choose Profile from the menu that appears.
2)Choose Product > Profile (or press Command-I).
Instruments launches, and the profiling template selection dialog appears. Note that your app is automatically selected in the target
device and process lists. 启动Instruments,并且选择分析模板对话框已经显示,并且可以看到已经自动选中了目标设备和对应是APP程序。
To use a default template when profiling from the Xcode build and product menus
1:Open an app project in Xcode.
2:Open the scheme editor dialog by doing one of the following: 使用以下任一方式打开编辑对话框
Select Edit Scheme from the target device list in the Xcode main window toolbar.
Choose Product > Scheme > Edit Scheme, or press Command–Less Than (<).
3:Click Profile in the sidebar of the scheme editor dialog. 在编辑对话框中的侧边栏选中profile
4:From the Instrument pop-up menu, choose the desired profiling template. 点击Instrument选项,选中想要的分析模板
5: Click Close.Now,
whenever you profile your app, Instruments automatically opens it in the template you selected in the scheme editor dialog and starts profiling. 点击关闭, 现在,无论什么时候profile APP,instrument将自动打开之前选中的模板。
Profile from Xcode gauges while debugging
Xcode调试导航(debug navigator)包括一些仪表用于监控APP,方便测试和调试。这些仪表跟踪了各种各样的因素,包括CPU使用,内存,能量影响,并且能够警告你潜在的影响性能的问题,电量使用等。如果在测试APP的时候确认了所想要的趋势,希望进一步研究,能够过渡监控到instrument获取更加详细的分析。
To initiate profiling from an Xcode gauge
1:Open an app project in Xcode and run it.
2:While your app is running, display the debug navigator by doing one of the following:
Click the Debug Navigator button in the navigator selector bar.
Choose View > Navigators > Show Debug Navigator (or press Command-6).
3:Click the desired gauge in the debug navigator.
4:Click the gauge’s Profile in Instruments button in the main editor.
注意到,并不是Xcode的每一个仪表都有Profile in instrument按钮。Energy Impact 仪表部分,包括了一些按钮用于初始化不同类型的分析会话(profiling sessions in Instrument).
5:When prompted, select whether to transfer the currently running instance of your app over
to Instruments or stop the app and start a new instance of it with Instruments. 当弹出提示框的时候,选择是否转移当前运行的APP实例到instrument或者停止当前在instrument开始一个新的实例
Profile from the Command Line
Profile with instruments
使用instrument命令行工具来分析APP并制定具体的模板而不需要启动Instrument ,数据在分析期间存储到指定目录下的 .trace文件中。在分析完成之后,可以使用instrument来打开记录的数据文件,观察显示的数据。
To profile with instruments
1:Open Terminal, in /Applications/Utilities.
2:Enter an instruments
command
to collect data.
For example, the following command profiles an app with the Allocations template and saves the
results to the desktop in a .trace
file.
-
-
3:Open Instruments, and choose File > Open.
4:Find your saved .trace
file,
and click Open.
在打开保存的文件之后,Instrument将自动添加相关到工具到跟踪文件并且使用收集数据填充。为了找到问题所在,我们可以看和分析数据。
instruments Options:instruments
provides
the following set of configuration options for defining what data to collect.
Configuration Option
|
Description
|
-t template
|
The name or path of the profiling template to use when analyzing your app.
|
-s
|
Returns a list of all installed profiling templates.
|
-D document
|
The path where the .trace document should be saved. If the file already
exists, the newly recorded data is appended to it as a new run.
|
-l #
|
The amount of time to record, in milliseconds, before terminating. If not provided, recording occurs indefinitely, until the app is manually terminated.
|
-i #
|
The index of the instrument to use for recording.
|
-p pid
|
The process ID of the app to be recorded.
|
application
|
The path of the app to be recorded.
|
-w hardware device
|
The ID of the device to target.
|
-e variable value
|
An environment variable to be applied while profiling.
|
argument
|
A command-line argument to be passed to the app being profiled. Multiple arguments may be specified, if desired.
|
-v
|
Enables verbose logging while profiling.
|
Collect and view data with iprofiler
使用 iprofiler命令行工具 (see iprofiler(1)
Mac OS X Developer Tools Manual Page)来测试APP性能而不需要启动instrument。在收集了性能数据之后,为了视觉化显示数据导入到instrument中。收集的数据被profiler保持为.dtps。更多内容,请看官方文档。
Minimize Instruments Impact on Data Collection
Instruments工具的目的是减少自己对数据收集影响。通过改变一下基本的设置,能够进一步减低instrument对数据的影响。为了收集更多的数据,能够为许多的instrument减少采样间隔。短的采样时间间隔得到高采样频率,同时导致了一些问题:
Processor time is required for every sample. High
sample rates use more processor time. 高采样率将需要更多的处理时间
Sample interval timing may not be consistent. Interrupts
are used to start each sample. Variations in when these interrupts occur can cause significant variations in the sample rate when using very small sample intervals. 启动中断用于每次开始一次取样。当使用非常小的样本间隔,这些中断发生的变化会导致采样率显著变化。
Small sample intervals cause more samples to be taken. Each
sample uses system memory, and a large number of samples quickly uses up the available memory on machines with less memory. 每一次取样都使用系统内存,大量的迅速采样将使用的机器上的变量有较少的内存使用
Run Instruments in Deferred Mode
通过延时数据分析,即一直到停止正在测试的APP,增加性能相关数据的准确性。当APP运行的时候Instrument分析和显示数据,允许我们查看收集的数据。通过占用CPU时间和内存执行性能分析会减缓目标过程,这让你的测量可能不会反应正常的处理行为。运行instrument在延迟模式分析数据,直到数据收集完成后,或者在你的应用程序运行结束之后或者在单击停止。在延迟模式中,你是无法收集数据与设备进行交互。
在延时模式下,instrument完成数据收集之后,instrument将处理数据并且显示在屏幕上。推迟数据分析将时间添加到数据收集过程的后期,但它有助于确保性能相关数据是准确的。
To set deferred mode for Instruments
1:Choose Instruments > Preferences.
2:In the Recording pane, select the “Always use deferred mode” checkbox.
对于特别长的跟踪,由延时模式所导致的延时将会有很大的影响。
To set deferred mode for a trace
1:Choose File > Record Options.
2:In the dialog that appears, select the Deferred Mode checkbox and then click OK. 在显示的对话框中,选中延时并点击Done
如果在Instruments — >preferences中设置了“Always
use deferred mode” ,该选项失效。