Application Performance using Custom Instruments — Part I

Yogesh Bhople
3 min readMay 28, 2021

Apple has provided so many profiling templates still why do we need custom? 🤔

Introduction

As a developer we have to think How to improve the performance of the application? However, one can think that my application performing good why should i do that? Then another question On what basis we say our application performance is good? to answer this we need performance data and this data has to be analysed by plotting it in graphs or by calculating average time taken for that particular task etc. Ultimately, we need some kind of report that will conclude that application perform is good or need improvement.

To generate this, Apple has provided so many tools which can be use to analyse the performance of the application.

Generally application may perform subset of following tasks

  1. web service calls
  2. Parse data
  3. Store data in database
  4. fetch data from database
  5. Display data in UI
  6. Image download
  7. View rendering
  8. Converting image
  9. Uses of 3rd party libraries
  10. etc.

let’s consider that we need to check the performance when application is concurrently performing database operation, converting image or rendering the view. We need to check How application perform in between login screen to landing screen(Home view). You may many different use cases. To address this type of custom requirement we can leverage the custom instrument. Apple has given the flexibility to use these powerful tools whatever way you want to use in your use case. They have provided the mechanism to create your own customised tool.

How this tools work?

It consist 3 major components

  1. Data Source is the application
  2. Data Store
  3. Analysis and show in reports/graphs

Data source is the iOS application which we generate the data and by using OSLog , os_signpost we need to send the data to system store. Once data is reached then we can plot those data in graphs, in list etc.

Let’s get started and write one simple instrument.

When to log the data?

  • Write a message at the start and end of functions and important tasks.
  • Write a message for any interesting events.
  • Write a message when a significant error occurs.
  • Write messages for important or unusual actions with a function. For example, log rarely taken code paths.
  • Write a message before each step of a multi-step task.

How to log the data?

We have to use the os_signpost to log the data. signpost type is important which can be start, end or event. It will be more clear in example. Also important thing is message format with arguments. Those arguments we have to map in instrument.

For this we need one application which will send the data or log the data.

In this application, we have ImageDownloader class which downloads the image and return the image in ImageDownloaderDelegate. Another important class is HomeTableViewCell. In table cell, we makes image download request, cancel request.

We are sending the data on four events —

  • Before making image download request — check .begin log
  • After receiving image download call back — Check .end in log
  • On cancel request — Check .end in log
  • When book details display we are logging .event

Till now we have covered the half part. Now our application ready to send the log messages.

Let see how to write custom instrument in part II

--

--