實踐代碼
//
// ViewController.m
// SignpostTest
//
// Created by tongleiming on 2020/2/12.
// Copyright ? 2020 tongleiming. All rights reserved.
//
#import "ViewController.h"
#include <os/signpost.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//[self mySignpost];
//[self mySignpostAdditional];
[self mySignpostSingle];
}
// 時間間隔
- (void)mySignpost {
os_log_t refreshLog = os_log_create("com.example.your-app", "RefreshOperations");
// To do that, we're going to add another piece of data to our signpost calls called a signpost ID.
// The signpost ID will tell the system that these are the same kind of operation but each one is different from each other.
// So if two operations overlap but they have different signpost IDs, the system will know that they're two different intervals.
// signpost id用來區(qū)分相同log,相同name的記錄
// You can make signpost IDs with this constructor here that takes a log handle,
// let spid = OSSignpostID(log: refreshLog)
os_signpost_id_t spidForRefresh = os_signpost_id_generate(refreshLog);
// 第二個參數(shù)叫做"Signpost ID",第三個參數(shù)叫做"Sigpost name"
os_signpost_interval_begin(refreshLog, spidForRefresh, "forLoop");
NSArray *array = @[@"hello", @"world"];
for (NSString *str in array) {
// but you can also make them with an object.
// let spid = OSSignpostID(log: refreshLog, object:element)
// This could be useful if you have some object that represents the work that you're trying to do and the same signpost ID will be generated as long as you use the same instance of that object.
// So this means you don't have to carry or store the signpost ID around.
// NSObject *obj = [NSObject new];
// os_signpost_id_t signpostID = os_signpost_id_make_with_pointer(m_log_name, (__bridge const void * _Nullable)(obj));
// 有了額外的對象指針,開始和結(jié)束語句甚至可以保存到不同的源文件中
os_signpost_id_t spid = os_signpost_id_make_with_pointer(refreshLog, (__bridge const void * _Nullable)(str));
os_signpost_interval_begin(refreshLog, spid, "Refresh");
[NSThread sleepForTimeInterval:0.5];
os_signpost_interval_end(refreshLog, spid, "Refresh");
}
os_signpost_interval_end(refreshLog, spidForRefresh, "forLoop");
}
// 攜帶元數(shù)據(jù)
- (void)mySignpostAdditional {
os_log_t refreshLog = os_log_create("com.example.your-app", "RefreshOperations");
os_signpost_id_t spidForRefresh = os_signpost_id_generate(refreshLog);
int i = 100;
// 額外攜帶的參數(shù)和os_log格式一樣
// 驗證在instruments中是如何顯示的
// 攜帶的元數(shù)據(jù)按照instruments的格式化顯示方式顯示的話,instruments還會進行統(tǒng)計分析{xcode:}
os_signpost_interval_begin(refreshLog, spidForRefresh, "forLoop", "Start the task");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
os_signpost_interval_end(refreshLog, spidForRefresh, "forLoop", "Finished with size %d", i);
});
}
// 事件類型的os_signpost,標記了一個特定時間點
// 如何將category定義為".pointsOfInterest"
// 在Time Profile中查看"興趣點"
- (void)mySignpostSingle {
os_log_t refreshLog = os_log_create("com.example.your-app", OS_LOG_CATEGORY_POINTS_OF_INTEREST);
os_signpost_id_t spidForRefresh = os_signpost_id_generate(refreshLog);
os_signpost_event_emit(refreshLog, spidForRefresh, "refresh", "test_meta_name");
}
// 有條件地開啟和關(guān)閉signpost
- (void)conditionOpenSignPost {
os_log_t refreshLog;
// 環(huán)境變量中存在"SIGNPOSTS_FOR_REFRESH",則會開啟
if ([NSProcessInfo.processInfo.environment.allKeys containsObject:@"SIGNPOSTS_FOR_REFRESH"]) {
refreshLog = os_log_create("com.example.your-app", "RefreshOperations");
} else {
refreshLog = OS_LOG_DISABLED;
}
os_signpost_id_t spidForRefresh = os_signpost_id_generate(refreshLog);
os_signpost_interval_begin(refreshLog, spidForRefresh, "forLoop");
os_signpost_interval_end(refreshLog, spidForRefresh, "forLoop");
}
// 自定義instrument WWDC 2018 Creating Custom Instruments
@end