BBPAppSpeedMonitor.h 2.68 KB
//
//  BBPAppSpeedMonitor.h
//  Pyramid
//
//  Created by jilutao on 2017/8/18.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, BBPEventTimeStatus) {
    BBPEventTimeStart = 0,
    BBPEventTimeEnd
};

__used extern void BBPSpeedEventTimeProfile(NSString *component, NSString *event, NSString *selector, BBPEventTimeStatus status);

#define BBPStartSpeedEventTime(component, event, selector) (BBPSpeedEventTimeProfile(component, event, selector, BBPEventTimeStart))
#define BBPEndSpeedEventTime(component, event) (BBPSpeedEventTimeProfile(component, event, nil, BBPEventTimeEnd))
// !!!block需要单独一个变量提出来,要不然无法调试
// 如:void (^speedBlock)(void) = ^{ }; BBPSpeedEventTime(@"component", @"event", @"selector", speedBlock);
#define BBPSpeedEventTime(component, event, selector, block) BBPStartSpeedEventTime(component, event, selector);block(); BBPEndSpeedEventTime(component, event)

@class BBPEventInfo;
@interface BBPAppSpeedMonitor : NSObject

+ (BBPAppSpeedMonitor *)sharedInstance;

/**
 应用启动时调用该方法,最好在main.m中调用
 */
- (void)appStartLaunch;

/**
 环境搭建完成
 */
- (void)appEnvironmentDidSetup;

/**
 记录事件时间
 
 @param eventName 事件名称
 @param component 组件
 @param selector 选择子
 @param status 事件状态,开始或者结束
 */
- (void)recordEvent:(NSString *)eventName
          component:(NSString *)component
           selector:(NSString *)selector
             status:(BBPEventTimeStatus)status;
/**
 记录事件时间
 
 @param eventName 事件名称
 @param component 组件
 @param selector 选择子
 @param status 事件状态,开始或者结束
 @param onEvent 事件回调
 */
- (void)recordEvent:(NSString *)eventName
          component:(NSString *)component
           selector:(NSString *)selector
             status:(BBPEventTimeStatus)status
            onEvent:(void(^)(BBPEventInfo *info))onEvent;

/**
 监控到的节点数据
 */
- (NSArray <BBPEventInfo *>*)recordedEventDataArray;

/**
 停止监控
 */
- (void)stopRecord;

#ifdef DEBUG
- (void)printOutTimeProfileResult;
- (void)saveTimeProfileDataIntoFile:(NSString *)filePath;
#endif

@end


// 速度打点节点
/**
 事件信息
 */
@interface BBPEventInfo : NSObject
/**
 组件
 */
@property (nonatomic, readonly, copy) NSString *component;

/**
 事件
 */
@property (nonatomic, readonly, copy) NSString *event;

/**
 方法名
 */
@property (nonatomic, readonly, copy) NSString *selector;

/**
 时长 (s)
 */
@property (nonatomic, readonly, assign) CFTimeInterval duration;

/**
 时间戳 (s)
 */
@property (nonatomic, readonly, assign) CFTimeInterval timestamp;

@end