Timering

Purp

Instruments tutorial Part 1 - profiling templates, deferred mode, launch instruments

Instrument is a flexible performance-analysis and testing tool that’s part of the Xcode tool set. You can detect memory allocation leaks, check CPU usage, energy usage.., you can use this tool to improve your codes, and use the best solution for your apps and your users. Start with basics, which can be helpful later. In this tutorial we launch Instruments at different ways, and set some really great settings.

As the User Guide says and you can see at the Instruments Workflow:

"At a high level, it consists of the following main phases:

  1. Set up a trace document containing the desired instruments and settings.
  2. Target a device and an app to profile.
  3. Profile the app.
  4. Analyze the data captured during profiling.
  5. Fix any problems in your source code."

Instruments_diagram_workflow _1_1

So you need to rewrite your own source code after analyze. Instrument will not do that for you!

Steps

  1. Profile - first steps
  2. Types of profiling templates
  3. Start Instruments in different ways
  4. Settings: Data collections in Deferred mode

Part of the series

  1. Instruments tutorial Part 2 - Navigate Timeline Pane, Detail Pane
  2. Instruments tutorial Part 3 - Profile Performance - CPU Usage - Time Profiler
  3. Instruments tutorial Part 4 - Profile Performance - Graphics Performance - Core Animation, OpenGl Activity
  4. Instruments tutorial Part 5 - Profile Performance - Monitor NetWork - Network
  5. Instruments tutorial Part 6 - Profile Memory - Activity Monitor
  6. Instruments tutorial Part 7 - Profile Memory - Allocations
  7. Instruments tutorial Part 8 - Profile Memory - Leaks
  8. Instruments tutorial Part 9 - Profile Memory - Zombies
  9. Instruments tutorial Part 10 - Profile Energy - Energy Impact
  10. Instruments tutorial Part 11 - Create Custom Instrument
  11. Instruments tutorial Part 12 - Automate UI Testing

Video

1. Profile - first steps 

If you want to use your Instruments App, instead of start your App a usual way(click on play, to run), click on Product at Xcode menu and select "Profile".

Instruments_ open _profile 1_1

Your Instruments App open, and you have to select a type of analysis. You can use Tim Profile, which is the fast way to detect our CPU usage problem. Or Core Animation, which can detect your bad Image loading or caching methods, or some allocation issues too. 

2. Type of templates

You can choose of "Standard", "Custom" and "Recent" profiling templates.

Instruments tutroial 1_4

If you created your own mixed profiling template, later you can use that with ease from "Custom".

At Standard profiling templates, you can choose from these types:

Profiling templates
Template Blank 48 2x This is a blank profiling template containing no instruments. Select this template if you can’t find a template to meet your needs, or if you want full control over what individual instruments and configuration settings are used to profile your app.
Template Activity Monitor 48 2x This template monitors the overall CPU, memory, disk, and network usage statistics of the specified process or processes. This template may be used with OS X or iOS.
Template Allocations 48 2x This template tracks the anonymous virtual memory and heap of a specified process. It provides class names and, optionally, retain and release histories for objects. This template may be used with OS X or iOS.
Template Automation 48 2x This template executes a script that simulates user interface interaction in an iOS app launched by Instruments. This template may be used with iOS only.
Template Cocoa Layout 48 2x This template observes changes made to layout constraint objects in order to determine when and where a layout constraint is not working as intended. This template may be used with OS X only.
Template Core Animator 48 2x This template uses time profiling to measure graphics performance and CPU usage in an app. This template may be used with iOS only.
Template Core Data 48 2x This template monitors Core Data activity in the file system, including fetches, cache misses, and saves. This template may be used with OS X only.
Template Counters1 48 2x This template monitors counter (PCM) events using time- or event-based sampling methods. This template may be used with OS X only.
Template Energy Diagnostics1 48 2x This template gathers diagnostics regarding energy usage and the on/off state of major hardware components, such as Bluetooth and Wi-Fi. This template profiles device behavior, not individual app behavior. This template may be used with iOS only.
Template File Activity 48 2x This template monitors file and directory activity, including file opens and closes, permission modifications, directory creation, file moves, and more. This template may be used with OS X only.
Template Gpu Driver 48 2x This template measures GPU driver statistics and samples active CPU usage. This template may be used with OS X or iOS. Included GPU Driver, Time Profiler.
Template Leaks 48 2x This template measures general memory usage and checks for leaked memory. It provides statistics on object allocations by class, as well as memory address histories for all active allocations and leaked blocks. This template may be used with OS X or iOS.
Template Metal System Trace 48 2x This template profiles the performance of iOS Metal applications by providing tracing information from the application, driver, and GPU layers. This template may be used with iOS only.
Template Network 48 2x This template analyzes the TCP/IP and UDP/IP connections an app uses. This template may be used with iOS only.
Template Open Gles Analysis 48 2x This template measures and analyzes OpenGL ES activity in order to identify performance and other OpenGL ES problems, and offer recommendations for resolving them. This template may be used with OS X or iOS. - Included GPU Driver, OpenGl ES Analyzer.
Template System Trace 48 2x This template provides comprehensive information about system behavior. It shows when threads are scheduled, and shows thread transitions from user to system code via system calls and memory operations. This template may be used with OS X or iOS. - Included Scheduling, System Call, VM Tracker. 
Template System Usage 48 2x This template monitors an app and records system I/O activity related to files, sockets, and shared memory. This includes inputs, outputs, duration, backtrace, call tree, and more for each call. This template may be used with iOS only.
Template Time Profiler 48 2x This template performs low-overhead time-based sampling of running processes. This template may be used with OS X or iOS.
Template Zombies 48 2x This template measures general memory usage of an app, focusing on the detection of over-released objects, known as zombies. It provides statistics on object allocations by class, as well as memory address histories for active allocations. This template may be used with OS X or iOS.

4. Start Instruments in different ways

1. Profile - select Xcode Product menu and choose Profile - choose Blank or other profiling Template, and start the Test with red record icon.

Instruments start with record icon

2. Run your App and at Debug session, choose "Profile in Instruments" button. 

You can restart the Application or transfer the current debug session, and you can see the already exists debug session in Instruments. 

instruments start from debug session 1

instruments start from debug session 2

3. You can launch from Dock or from Launchpad from Xcode Developer Tool

Instruments tutorial launch from dock

Instruments tutorial launch from launchpad

4. You can launch Instruments with Command Line:

In Terminal, in Applications/Utilities run this line: open /Applications/Xcode.app/Contents/Applications/Instruments.app

3. Settings: Data collections in Deferred mode 

The most important thing, if you want to get really accurate data, check at Instruments performance - Recording - "Always use deferred mode"

Instruments preferences

Instruments always use deferred mode

"Increase the accuracy of performance-related data by deferring data analysis until you quit the app you are testing. Typically, Instruments analyzes and displays data while your app runs, allowing you to view the data as it is collected. Performing analysis live slows down the target process by taking up CPU time and memory, which leaves you with measurements that may not reflect how the process would normally behave. Running Instruments in deferred mode delays the analysis of data until the data collection is done, either after your app has finished running or after you click Stop. While in deferred mode, you are blocked from interacting with the instruments that are collecting data.

In deferred mode, after Instruments finishes collecting data, Instruments processes the data and displays it onscreen. Deferring data analysis adds time to the later stages of the data collection process, but it helps ensure that performance-related data is accurate."

NEXT

The next part of the series: Instruments tutorial Part 2 - Navigate Timeline Pane, Detail Pane- soon!

Share & Follow

If you would like to be notified of my new solutions, please subscribe to my Newsletter or my youtube channel and/ or follow me on Facebook or Twitter! Check Footer section for these opportunities!