From fd8cac42edcf0d2382da13f151fbba8ddfd1d6e2 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Thu, 8 Jul 2021 14:40:28 +0800 Subject: [PATCH] feat:add performance anchor hook --- .../performance/DoricPerformanceProfile.java | 17 +++++++++++++++++ .../Performance/DoricPerformanceProfile.h | 12 ++++++++++++ .../Performance/DoricPerformanceProfile.m | 13 +++++++++++++ 3 files changed, 42 insertions(+) diff --git a/doric-android/doric/src/main/java/pub/doric/performance/DoricPerformanceProfile.java b/doric-android/doric/src/main/java/pub/doric/performance/DoricPerformanceProfile.java index d77fa615..d6fb6e55 100644 --- a/doric-android/doric/src/main/java/pub/doric/performance/DoricPerformanceProfile.java +++ b/doric-android/doric/src/main/java/pub/doric/performance/DoricPerformanceProfile.java @@ -20,7 +20,9 @@ import android.os.HandlerThread; import android.util.Log; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import pub.doric.DoricRegistry; @@ -43,6 +45,7 @@ public class DoricPerformanceProfile { private boolean enable = DoricRegistry.isEnablePerformance(); private static final Handler performanceHandler; + private final Set hooks = new HashSet<>(); static { HandlerThread performanceThread = new HandlerThread("DoricPerformance"); @@ -50,11 +53,22 @@ public class DoricPerformanceProfile { performanceHandler = new Handler(performanceThread.getLooper()); } + public interface AnchorHook { + void onAnchor(String name, long prepare, long start, long end); + } public DoricPerformanceProfile(String name) { this.name = name; } + public void addAnchorHook(AnchorHook hook) { + this.hooks.add(hook); + } + + public void removeAnchorHook(AnchorHook hook) { + this.hooks.remove(hook); + } + public void enable(boolean enable) { this.enable = enable; } @@ -120,6 +134,9 @@ public class DoricPerformanceProfile { } Log.d(TAG, String.format("%s: %s prepared %dms, cost %dms.", name, anchorName, start - prepare, end - start)); + for (AnchorHook hook : hooks) { + hook.onAnchor(anchorName, prepare, start, end); + } } }); } diff --git a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.h b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.h index db14fa1c..cf3cad17 100644 --- a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.h +++ b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.h @@ -24,6 +24,14 @@ NS_ASSUME_NONNULL_BEGIN +@protocol DoricPerformanceAnchorHookProtocol +- (void)onAnchorName:(NSString *)name + prepare:(NSNumber *)prepare + start:(NSNumber *)start + end:(NSNumber *)end; +@end + + @interface DoricPerformanceProfile : NSObject - (instancetype)initWithName:(NSString *)name; @@ -32,6 +40,10 @@ NS_ASSUME_NONNULL_BEGIN - (void)start:(NSString *)anchorName; - (void)end:(NSString *)anchorName; + +- (void)addAnchorHook:(id )hook; + +- (void)removeAnchorHook:(id )hook; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m index a6d9a23b..0a42acba 100644 --- a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m +++ b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m @@ -28,6 +28,7 @@ @interface DoricPerformanceProfile () @property(nonatomic, strong) NSMutableDictionary *anchorMap; @property(nonatomic, strong) dispatch_queue_t anchorQueue; @property(nonatomic, assign) BOOL enable; +@property(nonatomic, strong) NSHashTable> *hooks; @end @implementation DoricPerformanceProfile @@ -37,10 +38,19 @@ - (instancetype)initWithName:(NSString *)name { _anchorQueue = dispatch_queue_create("doric.performance.profile", DISPATCH_QUEUE_SERIAL); _anchorMap = [NSMutableDictionary new]; _enable = [DoricRegistry isEnablePerformance]; + _hooks = [NSHashTable new]; } return self; } +- (void)addAnchorHook:(id)hook { + [self.hooks addObject:hook]; +} + +- (void)removeAnchorHook:(id)hook { + [self.hooks removeObject:hook]; +} + - (NSString *)getPrepareAnchor:(NSString *)anchorName { return [NSString stringWithFormat:@"%@#prepare", anchorName]; } @@ -100,6 +110,9 @@ - (void)print:(NSString *)anchorName { @(start.integerValue - prepare.integerValue), @(end.integerValue - start.integerValue) ); + for (id hook in self.hooks) { + [hook onAnchorName:anchorName prepare:prepare start:start end:end]; + } }); } @end