WWDC 2018 Measuring Performance Using Logging

实践代码

//
//  ViewController.m
//  SignpostTest
//
//  Created by tongleiming on 2020/2/12.
//  Copyright © 2020 tongleiming. All rights reserved.
//

#import "ViewController.h"
#include 


@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用来区分相同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);
    // 第二个参数叫做"Signpost ID",第三个参数叫做"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));
        // 有了额外的对象指针,开始和结束语句甚至可以保存到不同的源文件中
        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");
}

// 携带元数据
- (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;
    // 额外携带的参数和os_log格式一样
    // 验证在instruments中是如何显示的
    // 携带的元数据按照instruments的格式化显示方式显示的话,instruments还会进行统计分析{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");
}

// 有条件地开启和关闭signpost
- (void)conditionOpenSignPost {
    os_log_t refreshLog;
    // 环境变量中存在"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

你可能感兴趣的:(WWDC 2018 Measuring Performance Using Logging)