feat:add switch for performance,default is off

This commit is contained in:
pengfei.zhou
2021-07-08 11:58:22 +08:00
committed by osborn
parent 34876de69f
commit f5bd4406d9
7 changed files with 86 additions and 29 deletions

View File

@@ -46,6 +46,7 @@ - (void)viewDidLoad {
it.delegate = self;
}]];
[DoricRegistry register:[DemoLibrary new]];
[DoricRegistry enablePerformance:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

View File

@@ -52,6 +52,10 @@ NS_ASSUME_NONNULL_BEGIN
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor;
+ (void)register:(DoricLibrary *)library;
+ (void)enablePerformance:(BOOL)enable;
+ (BOOL)isEnablePerformance;
@end
NS_ASSUME_NONNULL_END

View File

@@ -59,6 +59,7 @@ @interface DoricLibraries : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@property(nonatomic, strong) NSMutableArray <NSValue *> *registries;
@property(nonatomic, strong) NSMutableDictionary *envDic;
@property(nonatomic, assign) BOOL enablePerformance;
+ (instancetype)instance;
@end
@@ -69,6 +70,7 @@ - (instancetype)init {
_libraries = [NSMutableSet new];
_registries = [NSMutableArray new];
_envDic = [NSMutableDictionary new];
_enablePerformance = NO;
}
return self;
}
@@ -133,6 +135,14 @@ - (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine {
return self;
}
+ (void)enablePerformance:(BOOL)enable {
DoricLibraries.instance.enablePerformance = enable;
}
+ (BOOL)isEnablePerformance {
return DoricLibraries.instance.enablePerformance;
}
- (void)innerRegister {
[self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"];
[self registerNativePlugin:DoricModalPlugin.class withName:@"modal"];

View File

@@ -21,13 +21,13 @@
//
#import "DoricPerformanceProfile.h"
const bool _DEBUG = YES;
#import "DoricRegistry.h"
@interface DoricPerformanceProfile ()
@property(nonatomic, copy) NSString *name;
@property(nonatomic, strong) NSMutableDictionary <NSString *, NSNumber *> *anchorMap;
@property(nonatomic, strong) dispatch_queue_t anchorQueue;
@property(nonatomic, assign) BOOL enable;
@end
@implementation DoricPerformanceProfile
@@ -36,6 +36,7 @@ - (instancetype)initWithName:(NSString *)name {
_name = name;
_anchorQueue = dispatch_queue_create("doric.performance.profile", DISPATCH_QUEUE_SERIAL);
_anchorMap = [NSMutableDictionary new];
_enable = [DoricRegistry isEnablePerformance];
}
return self;
}
@@ -53,7 +54,7 @@ - (NSString *)getEndAnchor:(NSString *)anchorName {
}
- (void)markAnchor:(NSString *)eventName {
if (!_DEBUG) {
if (!self.enable) {
return;
}
NSUInteger time = (NSUInteger) ([[NSDate date] timeIntervalSince1970] * 1000);
@@ -73,12 +74,13 @@ - (void)start:(NSString *)anchorName {
- (void)end:(NSString *)anchorName {
[self markAnchor:[self getEndAnchor:anchorName]];
if (_DEBUG) {
[self print:anchorName];
}
[self print:anchorName];
}
- (void)print:(NSString *)anchorName {
if (!self.enable) {
return;
}
dispatch_async(self.anchorQueue, ^{
NSNumber *prepare = self.anchorMap[[self getPrepareAnchor:anchorName]];
NSNumber *start = self.anchorMap[[self getStartAnchor:anchorName]];