Open Source iOS Debugging / Profiling Tools MTHawkeye

Currently, we’ve open source our debugging/profiling tools MTHawkeye, it’s designed to help iOS developers improve development productivity and assist in optimizing the App performance.

We introduced MTHawkeye in both development, test and online, it help us discover, find, analyze, locate, and solve problems faster.

  • Development phase, focusing on development and debugging assistance, detect problems in a timely manner, and prompt developers to deal with them.
  • Test phase, focusing on collecting performance data as much as possible from the test case, for generating automated test analysis reports.
  • Online phase, focusing on performance data that needs by our own business but missing from third party APM components.

## What MTHawkeye contains

MTHawkeye can be divided into upper, middle, and lower layer. In addition to the bottom `Base` layer, the middle is the `UI Skeleton` and `Desktop Adaptor`, the uppermost plug-ins are internally split according to different scenarios. You can optionally add these plugins to your own scenario. The overall structure is as follows:

MTHawkeye Architecture

### 1. Memory Plugins

#### # LivingObjectSniffer

`LivingObjectSniffer` is mainly used to track and observe objects directly or indirectly held by ViewController, as well as custom View objects. Detects whether these objects are unexpected alive, which may cause by leaks, not released in time, or unnecessary memory buffers.

In the development and testing phase, the detected unexpected alive objects can be prompted to the developer in the form of floating windows flash warning or toast.

In the automated test, the recorded unexpected alive objects can also be extracted for the further memory usage analysis.

#### # Allocations

`Allocations` is similar to the Instrument’s Allocations module, It tracks the memory details actually allocated by the application. When the application memory usage is abnormal (abnormal rise, OOM exit), the recorded memory allocation details can be used to analyze specific memory usage issues.

Allocation Tracing Result Demo

### 2. TimeConsuming

#### # UITimeProfiler

`UITimeProfiler` is used to assist in optimizing the time-consuming tasks of the main thread.

The data collection part mainly includes two components, `VC Life Trace` and `ObjC CallTrace`. `VC Life Trace` tracking the time of each key node when opening ViewController, and when `ObjC CallTrace` is turned on, it can track Objective-C methods that are executed on the main thread and take longer than the specified threshold.

The interface layer part combines the two parts of data to make it easier for developers to find out the time-consuming details of the operations they are focusing on. The example diagram is as shown in the previous section, for a more detailed description, see the `UITimeProfiler` plugin documentation.

After enabling the plug-in on the automated test or online phase, without other code, you can continuously automate tracking the startup, page open, and other critical processes time-consuming.

UI Time Profiler Demo

#### # ANRTrace

`ANRTrace` is used to capture the stuck event, and will sample the main thread stack frame when the jam occurs.

#### # FPSTrace

`FPSTrace` is used to track the interface FPS and OpenGL flush FPS, and display the current value on the floating window.

### 3. Energy

#### # CPUTrace

`CPUTrace` is used to track the CPU’s continuous high-load usage, and will recording which methods are mainly called during the high-load CPU usage.

### 4. Network

#### # NetworkMonitor

`NetworkMonitor` observes and records HTTP(S) network transactions with metrics info in the App. Providing built-in records viewing interface for a developer to troubleshoot network problems

  1. Inherit FLEX’s network recording logic, and optimize the initialization logic, greatly reducing the impact by hooking on startup time.
  2. For NSURLSession after iOS 9, add the URLSessionTaskMetrics record to view the time of each stage of the transaction.
  3. Add a waterfall view similar to Chrome network debugging based on transaction metrics, to view the queue and concurrency of network transactions, and do further optimization.
  4. Add the ability to detect duplicate unnecessary network transactions.
  5. Enhanced search bar to support multi-condition search (host filter, status filter)
  6. Record the network transaction with request header, request body, response body.
Network Monitor Demo

#### # NetworkInspect

`NetworkInspect` is based on `NetworkMonitor`. Depending on the actual of the network transaction, checking whether the network request can be improved according to the inspection rules, and you can add your own inspection rules by yourself.

### 5. Graphics

#### # OpenGLTrace

`OpengGLTrace` is used to track the memory usage of OpenGL resources, and to help find OpenGL API error calls and exception parameter passing.

### 6. Storage

#### # DirectoryWatcher

`DirectoryWatcher` is used to track the size of the specified sandbox folders, it also integrates FLEX’s sandbox file browser.

### 7. Utility

#### # FLEX

FLEX is commonly widely in daily development, MTHawkeye adds it as a plugin and extends the use of AirDrop for sandboxed files.

## Usage

Read MTHawkeye — Usage for detail.

## Develop your own plugins

If you have a module that needs to avoid a lot of pits during development, or if you have a lot of debugging/optimization related logging code during development, consider writing a debugging aid and then importing this component into MTHawkeye based on the MTHawkeye API. Used in the framework to unify interactions and interfaces.

If the performance metrics you care about are not continuously tracked during automated testing, consider writing a profiling plugin to collect performance data.

For detail: MTHawkeye plugin development Guide

## Performance impact

The performance impact of each plugin can be see under the documentation of the plugin.

## Source Repo