feat:add performance anchor hook

This commit is contained in:
pengfei.zhou 2021-07-08 14:40:28 +08:00 committed by osborn
parent b7dab8c30f
commit fd8cac42ed
3 changed files with 42 additions and 0 deletions

View File

@ -20,7 +20,9 @@ import android.os.HandlerThread;
import android.util.Log; import android.util.Log;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import pub.doric.DoricRegistry; import pub.doric.DoricRegistry;
@ -43,6 +45,7 @@ public class DoricPerformanceProfile {
private boolean enable = DoricRegistry.isEnablePerformance(); private boolean enable = DoricRegistry.isEnablePerformance();
private static final Handler performanceHandler; private static final Handler performanceHandler;
private final Set<AnchorHook> hooks = new HashSet<>();
static { static {
HandlerThread performanceThread = new HandlerThread("DoricPerformance"); HandlerThread performanceThread = new HandlerThread("DoricPerformance");
@ -50,11 +53,22 @@ public class DoricPerformanceProfile {
performanceHandler = new Handler(performanceThread.getLooper()); performanceHandler = new Handler(performanceThread.getLooper());
} }
public interface AnchorHook {
void onAnchor(String name, long prepare, long start, long end);
}
public DoricPerformanceProfile(String name) { public DoricPerformanceProfile(String name) {
this.name = 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) { public void enable(boolean enable) {
this.enable = enable; this.enable = enable;
} }
@ -120,6 +134,9 @@ public class DoricPerformanceProfile {
} }
Log.d(TAG, String.format("%s: %s prepared %dms, cost %dms.", Log.d(TAG, String.format("%s: %s prepared %dms, cost %dms.",
name, anchorName, start - prepare, end - start)); name, anchorName, start - prepare, end - start));
for (AnchorHook hook : hooks) {
hook.onAnchor(anchorName, prepare, start, end);
}
} }
}); });
} }

View File

@ -24,6 +24,14 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@protocol DoricPerformanceAnchorHookProtocol <NSObject>
- (void)onAnchorName:(NSString *)name
prepare:(NSNumber *)prepare
start:(NSNumber *)start
end:(NSNumber *)end;
@end
@interface DoricPerformanceProfile : NSObject @interface DoricPerformanceProfile : NSObject
- (instancetype)initWithName:(NSString *)name; - (instancetype)initWithName:(NSString *)name;
@ -32,6 +40,10 @@ NS_ASSUME_NONNULL_BEGIN
- (void)start:(NSString *)anchorName; - (void)start:(NSString *)anchorName;
- (void)end:(NSString *)anchorName; - (void)end:(NSString *)anchorName;
- (void)addAnchorHook:(id <DoricPerformanceAnchorHookProtocol>)hook;
- (void)removeAnchorHook:(id <DoricPerformanceAnchorHookProtocol>)hook;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@ -28,6 +28,7 @@ @interface DoricPerformanceProfile ()
@property(nonatomic, strong) NSMutableDictionary <NSString *, NSNumber *> *anchorMap; @property(nonatomic, strong) NSMutableDictionary <NSString *, NSNumber *> *anchorMap;
@property(nonatomic, strong) dispatch_queue_t anchorQueue; @property(nonatomic, strong) dispatch_queue_t anchorQueue;
@property(nonatomic, assign) BOOL enable; @property(nonatomic, assign) BOOL enable;
@property(nonatomic, strong) NSHashTable<id <DoricPerformanceAnchorHookProtocol>> *hooks;
@end @end
@implementation DoricPerformanceProfile @implementation DoricPerformanceProfile
@ -37,10 +38,19 @@ - (instancetype)initWithName:(NSString *)name {
_anchorQueue = dispatch_queue_create("doric.performance.profile", DISPATCH_QUEUE_SERIAL); _anchorQueue = dispatch_queue_create("doric.performance.profile", DISPATCH_QUEUE_SERIAL);
_anchorMap = [NSMutableDictionary new]; _anchorMap = [NSMutableDictionary new];
_enable = [DoricRegistry isEnablePerformance]; _enable = [DoricRegistry isEnablePerformance];
_hooks = [NSHashTable new];
} }
return self; return self;
} }
- (void)addAnchorHook:(id)hook {
[self.hooks addObject:hook];
}
- (void)removeAnchorHook:(id)hook {
[self.hooks removeObject:hook];
}
- (NSString *)getPrepareAnchor:(NSString *)anchorName { - (NSString *)getPrepareAnchor:(NSString *)anchorName {
return [NSString stringWithFormat:@"%@#prepare", anchorName]; return [NSString stringWithFormat:@"%@#prepare", anchorName];
} }
@ -100,6 +110,9 @@ - (void)print:(NSString *)anchorName {
@(start.integerValue - prepare.integerValue), @(start.integerValue - prepare.integerValue),
@(end.integerValue - start.integerValue) @(end.integerValue - start.integerValue)
); );
for (id <DoricPerformanceAnchorHookProtocol> hook in self.hooks) {
[hook onAnchorName:anchorName prepare:prepare start:start end:end];
}
}); });
} }
@end @end