Always the users

My software does email archiving. One user complained that after a couple of hours archiving got slower and slower. I thought to myself that this is impossible. Then the second user complained about the same issue.

I wanted to prove them wrong and started archiving myself. It was totally astonishing to see that - yes - after a couple of hours archiving emails got quite a bit slower.

I knew, however, that I could find the culprit with Instruments. The app comes with XCode and can be used to identify slowdowns with the Time Profiler part. That’s what we are going to do now.

The article assumes that you have installed XCode. You can use Instruments on any running app.

Start Instruments

Use Spotlight or Alfred to start Instruments.

Start Instruments

You will now see the screen for choosing a profiling template. Today we are going to look only at the Time Profiler one:Profiling Template

After selecting the template you will see the main screen of Instruments. I’m not going to explain the different sections right away. We will first load some data so that the sections make sense.

Your application needs to be started because we are going to select it in a second.

Click on “All Processes”:Select a Process

Now you will need to the select the application that you want to check. That’s why we had to start the app before:Select process

Now click on the red button in the upper left side of the screen:

Start profiling

I always think that the button looks like a stop button. Especially with the red color. But what do I know about interface design. Do something in your app. After 10 - 20 seconds stop recording by clicking on the start button again.

Interface Overview

Now we can make sense out of the interface.

  1. Recording Controls: We already tried the recording controls: start/pause/stop. And we had a look at the process selection.

  2. Run panel: You can stop recording and start it again. Then the run number will be 2 instead of one. I prefer to do separate runs. Your mileage may vary.

  3. Show/Hide: You can show or hide different parts with the button at 3. The first button would hide parts 6 and 7. The second button hides part 8.

  4. App/Threads: In area 4 you can select the whole application or the individual threads.

  5. CPU: Shows the details for area 4. For instance while the CPU usage varies quite a bit the CPU usage for the Main Thread shows interesting spikes.

  6. Details: This is the main part where you see which parts of your code uses how much CPU time. The weight column tells you the percentage of the total time your code spends in a method.

  7. Bottom Bar: In the bottom bar you can select different options what to show.

  8. Heaviest Stack Trace: You can jumpstart your search in 7 by clicking on lines in 8.

Interface Details

Clicking on CPU Usage gives me information about the whole app:

If I would click on Main Thread a bit below I would only see the Main Thread information.

In the area “Heaviest Stack Trace” I can jumpstart my search for slow parts of the code.

After clicking on ArchiveThread.Event_run the method is selected on the left side.

Heaviest Stack Trace

When you click on the “Call Tree” button you will get some options to display the detail area.

Separate by State: you can separate the threads by their state (running etc). 

Separate by Thread: the default option shows the different threads as top level. 

Invert Call Tree: shows the call tree from most recent to least recent. 

Hide System Libraries: You can hide the Systm Libraries. 

Flatten recursion: hides recursive calls in your code. 

Top Functions: Here I’m going to rely on for the explanation on Ray Wenderlich:

Enabling this makes Instruments consider the total time spent in a function as the sum of the time directly within that function, as well as the time spent in functions called by that function. So if function A calls B, then A’s time is reported as the time spent in A PLUS the time spent in B.