feat:add global anchor hook to monitor all performance hook

This commit is contained in:
pengfei.zhou
2021-07-20 10:50:24 +08:00
committed by osborn
parent ba3635769a
commit 17e14e7119
16 changed files with 303 additions and 12 deletions

View File

@@ -54,6 +54,9 @@ + (instancetype)instance {
- (void)createContext:(DoricContext *)context script:(NSString *)script source:(NSString *)source {
context.contextId = [NSString stringWithFormat:@"%ld", (long) ++self.counter];
context.performanceProfile = [[DoricPerformanceProfile alloc] initWithName:context.contextId];
if (context.driver.registry.globalPerformanceAnchorHook) {
[context.performanceProfile addAnchorHook:context.driver.registry.globalPerformanceAnchorHook];
}
dispatch_sync(self.mapQueue, ^() {
[self.contextMapTable setObject:context forKey:context.contextId];
});

View File

@@ -27,10 +27,12 @@
NS_ASSUME_NONNULL_BEGIN
@class DoricLibrary;
@class DoricJSEngine;
@protocol DoricPerformanceGlobalAnchorHookProtocol;
@interface DoricRegistry : NSObject <DoricMonitorProtocol>
@property(nonatomic, strong) UIImage *defaultPlaceHolderImage;
@property(nonatomic, strong) UIImage *defaultErrorImage;
@property(nonatomic, strong) id <DoricPerformanceGlobalAnchorHookProtocol> globalPerformanceAnchorHook;
- (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine;

View File

@@ -58,7 +58,11 @@ @implementation DoricJSEngine
- (instancetype)init {
if (self = [super init]) {
_initialized = NO;
_registry = [[DoricRegistry alloc] initWithJSEngine:self];
_profile = [[DoricPerformanceProfile alloc] initWithName:@"JSEngine"];
if (_registry.globalPerformanceAnchorHook) {
[_profile addAnchorHook:_registry.globalPerformanceAnchorHook];
}
[_profile prepare:@"Init"];
_jsThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadRun) object:nil];
[_jsThread start];
@@ -95,7 +99,6 @@ - (instancetype)init {
@"localeLanguage": [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode] ?: @"",
@"localeCountry": [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode] ?: @"",
}.mutableCopy;
self.registry = [[DoricRegistry alloc] initWithJSEngine:self];
[self ensureRunOnJSThread:^() {
[self.profile start:@"Init"];
self.timers = [[NSMutableDictionary alloc] init];

View File

@@ -31,9 +31,17 @@ NS_ASSUME_NONNULL_BEGIN
end:(NSNumber *)end;
@end
@class DoricPerformanceProfile;
@protocol DoricPerformanceGlobalAnchorHookProtocol <NSObject, DoricPerformanceAnchorHookProtocol>
- (void)onAnchorName:(NSString *)name
prepare:(NSNumber *)prepare
start:(NSNumber *)start
end:(NSNumber *)end
in:(DoricPerformanceProfile *)profile;
@end
@interface DoricPerformanceProfile : NSObject
@property(nonatomic, strong) NSMutableDictionary <NSString *, NSNumber *> *anchorMap;
- (instancetype)initWithName:(NSString *)name;

View File

@@ -28,6 +28,7 @@ @interface DoricPerformanceProfile ()
@property(nonatomic, strong) dispatch_queue_t anchorQueue;
@property(nonatomic, assign) BOOL enable;
@property(nonatomic, strong) NSHashTable<id <DoricPerformanceAnchorHookProtocol>> *hooks;
@property(nonatomic, strong) NSMutableDictionary <NSString *, NSNumber *> *anchorMap;
@end
@implementation DoricPerformanceProfile
@@ -114,7 +115,11 @@ - (void)print:(NSString *)anchorName {
@(end.integerValue - start.integerValue)
);
for (id <DoricPerformanceAnchorHookProtocol> hook in self.hooks) {
[hook onAnchorName:anchorName prepare:prepare start:start end:end];
if ([hook conformsToProtocol:@protocol(DoricPerformanceGlobalAnchorHookProtocol)]) {
[hook onAnchorName:anchorName prepare:end start:end end:end in:self];
} else {
[hook onAnchorName:anchorName prepare:prepare start:start end:end];
}
}
});
}