0 x000 preparation

It is recommended to do the following two things first, if you don’t want to: 1, look at the Creating Custom Instruments tutorial, because the current article is summarized by this tutorial. 2. The code I implemented is here at CustomInstruments. Welcome to download. Os_signpost and OSLOG are used in the following commands: 4. Learn a little about CLIPS syntax. The current article is mainly implemented according to the content mentioned in WWDC2018&410. If you can’t get the code in the video, you can only simulate it by yourself.

0x001 Create a custom project

This is as simple as creating a Mac project by following the instructions on the screen.



HGCustomInstruments

instruments
Splitting up instruments code

Note: This is a Mac application and should be run like this:



HGTick



HGTick


0x010 HGTick

Open the file (hgtick.instrpkg) and it looks like this:



XML
package
id
title
owner

<! -- Import tick module equivalent to 'import' --> <import-schema>tick</import-schema>Copy the code

Specific meanings are shown in the comments, indicating that the TICK module already exists in the system. You can import the TICK module using the import-schema label.

<! - begin to build an instrument - > < instrument >. < id > com coderhg. Ticksinstrument < / id > < title > Ticks - IT < / title > <category>Behavior</category> <purpose>tick demo</purpose> <icon>Generic</icon> <! Create a table where 'tick' --> <create-table> <id>tick-table</id> <schema-ref>tick</schema-ref> </create-table> <! --> <graph> <title> tick-gh </title> <lane> <title> tick-le </title> <table-ref>tick-table</table-ref> <! - track view displays the data - > < the plot > < value - the from > time < / value - the from > < / plot > < / lane > < / graph > <! -- Detail view --> <list> <title> tick-lt1 </title> <table-ref>tick-table</table-ref> <column>time</column> </list> <list> <title>Ticks-LT2</title> <table-ref>tick-table</table-ref> <column>time</column> </list> </instrument>Copy the code

1. After using Instrument, you still need to add the corresponding identifier, title, and other basic information. 2. To create a custom Instrument, a table needs to be created. Therefore, create-table is required. 3. Start creating a track view. The track view data comes from the tick-table table. Details view. The list tag is used to display data in details view. This list is equivalent to our UITableView, and tick-table is equivalent to our dataSource. Choose HGTick







schema
tick



tick
time


Note: this example has no practical significance, just a simple introduction.

0x011 HGJSONDecode

This is mainly to simulate parsing JSON strings, for the sake of simplicity, I will make a simulated JSON method. This needs to be intrusive to the project code being developed. So the code to be simulated is as follows:

/// Touch
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    // loghandlelet parsingLog = OSLog(subsystem: "com.coderhg.json", category: "JSONDecode"Os_signpost (.begin,log: parsingLog, name: "Parsing"."Parsing started") // Decode the JSON we just downloadedletSize = jsonDecode() os_SIGNPOST (.end,log: parsingLog, name: "Parsing"."Parsing finished SIZE:%ld"Func jsonDecode() -> UInt32 {// Time from 0 to 3.0 secondsletTimeOut = (arc4random()%6)/2 sleep(timeOut) // Simulates the size of the currently parsed stringlet size = arc4random()%100 + 10
    return size
}
Copy the code

Note: This is the code we developed in order to test the jsonDecode method, where a random number is generated to identify the parsing time required. In fact, other code has been discovered through the above code, mainly the OS_SignPOST API. Yeah, that’s what you do when you combine custom Instruments, so you have to write a start and an end here, so you can match them in custom, so you can do a more detailed analysis.

Next, take a look at the implementation in the corresponding HGJSONDecode. Instrpkg file. The os-signpost-interval-schema tag is used to customize a schema, as shown below:

<! <os-signpost-interval-schema> <id>json-parse</id> <title>JSON Decode</title> <! -- These three are analogous to one of the projects --> <subsystem>"com.coderhg.json"</subsystem>
    <category>"JSONDecode"</category>
    <name>"Parsing"</name> <! -- start matching --> <start-pattern> <message>"Parsing started"</message> </start-pattern> <! --> <end-pattern> <message>"Parsing finished SIZE:"? data-size-value</message> </end-pattern> <! -- a column in the table --> <column> <! <mnemonic>data-size</mnemonic> <title>JSON data size</ title> <! Size -in-bytes --> <type>size-in-bytes</type> <! -- display the value of data-size --> <expression>? data-size-value</expression> </column> <! -- https://help.apple.com/instruments/developer/mac/current/#/dev66257045 -->
    <column>
        <mnemonic>impact</mnemonic>
        <title>Impact</title>
        <type>event-concept</type>
        <expression>(if(&gt; ? data-size-value 80)then "High" else "Low")</expression>
    </column>
    
</os-signpost-interval-schema>
Copy the code

The code above, with my own development experience, should be readable, even if I tell you every time this is another programming language: CLIPS. If you look confidently at the start-pattern and end-pattern tags, you should know what they mean: they are mainly used to get the start and end of the JSON parsing in the project. The metadata in the project is matched at the end: the size of the parse character. The main variables used here are CLIPS language variables. And then column, the tag, even though it defines some of the fields for this shema, it turns out that the schema mentioned here is very much like a database. There are two keys in the database: data-size and impact. Impact is determined by the value of data-size. If the value is greater than 80, the value is High; otherwise, it is Low. And then the rest of it is just like HGTick. The end result looks like this:

This makes it clear how each JSON parse begins and ends, and how long it takes to execute. In the actual development, other debugging modules may be selected at the same Time, such as Time Profiler, memory detection, etc., which can be a good all-round analysis of the current running environment and running status.

0x1000 HGImageDownload

The main example is the simulation picture download, the core code in the project, as follows:

/ / / download state var loadStatus: HGImageLoadStatus? {didSet {// unknownif loadStatus == .HGImageLoadStatusUnkown {
            return
        }
        
        // ID
        let signpostID = OSSignpostID(log: HGSignpostLog.imageLoadLog, object: self.obj!)
        letaddress = unsafeBitCast(self.mockData! , to: uin.self) // startif loadStatus == .HGImageLoadStatusIng {
            os_signpost(.begin, log: HGSignpostLog.imageLoadLog, name: "Background Image", signpostID: signpostID, "Image name:%{public}@, Caller:%lu", name! , address)return
        }
        
        var status = "finish"/ / finishifLoadStatus ==.HGImageLoadStatusFinish {} // Cancelif loadStatus == .HGImageLoadStatusCancel {
            status = "cancel"
        }
        
        os_signpost(.end, log: HGSignpostLog.imageLoadLog, name: "Background Image", signpostID: signpostID, "Status:%{public}@, Size:%lu", status, needTime!)
    }
Copy the code

In other words, simulate the image download and then make the OS_SignPOST call in the code above. Mainly look at the implementation in HGImageDownload. There is more to cover in this section, but more to cover the building of the modeler than the previous two. This section, which is not intended to copy the code here, should be easy to understand through the project code described above. The main tags are: Point-schema, Modeler, aggregation and Narrative, and CLIPS files Uplicate – Call-detection. CLP. The final result is:


0 x1001 summary

Wwdc2018/410 is the main introduction. 1. HGTick simply uses the schema TICK module of the following system, and has a basic understanding of the routines and basic formats of custom Instruments. 2. HGJSONDecode customizes a schema to obtain interested information from the project through OS_SIGNPOST, and obtain metadata for final analysis and display. 3. Advanced application of custom Instruments through HGImageDownload, mainly the building of the modeler.

Summary: From this introduction, you can basically see how the process of customizing Instruments has been applied.

Refer to the article

WWDC 2018: Considerations for creating custom Instrument Xcode in Instruments(Time Profiler)