From a021aae3de5c52efd25df462ec685d2845886bbe Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Tue, 20 Jul 2021 11:29:53 +0800 Subject: [PATCH] feat:use global anchor hook to print log --- .../main/java/pub/doric/devkit/DoricDev.java | 7 +--- .../devkit/DoricDevPerformanceAnchorHook.java | 14 ++++++-- .../doric/devkit/ui/DoricDevPerfActivity.java | 36 +++++++++---------- .../performance/DoricPerformanceProfile.java | 6 ++-- doric-iOS/Devkit/Classes/DoricDev.m | 15 ++++---- .../Classes/DoricDevPerformanceAnchorHook.h | 9 +++++ .../Classes/DoricDevPerformanceAnchorHook.m | 17 +++++++++ .../Performance/DoricPerformanceProfile.h | 1 + .../Performance/DoricPerformanceProfile.m | 9 +---- 9 files changed, 71 insertions(+), 43 deletions(-) create mode 100644 doric-iOS/Devkit/Classes/DoricDevPerformanceAnchorHook.h create mode 100644 doric-iOS/Devkit/Classes/DoricDevPerformanceAnchorHook.m diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java index ed26d78c..03302a9e 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java @@ -52,12 +52,7 @@ public class DoricDev { private DoricDev() { this.isRunningInEmulator = SimulatorUtil.isSimulator(Doric.application()); DoricNativeDriver.getInstance().getRegistry().registerMonitor(new DoricDevMonitor()); - DoricNativeDriver.getInstance().getRegistry().setGlobalPerformanceAnchorHook(new DoricPerformanceProfile.AnchorHook() { - @Override - public void onAnchor(String name, long prepare, long start, long end) { - - } - }); + DoricNativeDriver.getInstance().getRegistry().setGlobalPerformanceAnchorHook(new DoricDevPerformanceAnchorHook()); } public static DoricDev getInstance() { diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDevPerformanceAnchorHook.java b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDevPerformanceAnchorHook.java index 15829f66..b85e97b9 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDevPerformanceAnchorHook.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDevPerformanceAnchorHook.java @@ -15,6 +15,8 @@ */ package pub.doric.devkit; +import android.util.Log; + import pub.doric.performance.DoricPerformanceProfile; /** @@ -22,9 +24,17 @@ import pub.doric.performance.DoricPerformanceProfile; * @Author: pengfei.zhou * @CreateDate: 2021/7/20 */ -public class DoricDevPerformanceAnchorHook implements DoricPerformanceProfile.AnchorHook { +public class DoricDevPerformanceAnchorHook implements DoricPerformanceProfile.GlobalAnchorHook { + private static final String TAG = "DoricPerformance"; + + @Override + public void onAnchor(DoricPerformanceProfile profile, String name, long prepare, long start, long end) { + Log.d(TAG, String.format("%s: %s prepared %dms, cost %dms.", + profile.getName(), name, start - prepare, end - start)); + } + @Override public void onAnchor(String name, long prepare, long start, long end) { - + //DO nothing } } diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevPerfActivity.java b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevPerfActivity.java index c77b9f14..6a01bc28 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevPerfActivity.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevPerfActivity.java @@ -78,24 +78,24 @@ public class DoricDevPerfActivity extends DoricDevBaseActivity { private List anchorNodes = new ArrayList<>(); private MyAdapter() { - Map anchorMap = doricContext.getPerformanceProfile().getAnchorMap(); - for (String key : anchorMap.keySet()) { - if (key.endsWith("#prepare")) { - Long prepare = anchorMap.get(key); - if (prepare != null) { - AnchorNode anchorNode = new AnchorNode(); - anchorNode.name = key.substring(0, key.lastIndexOf("#prepare")); - anchorNode.prepare = prepare; - anchorNodes.add(anchorNode); - } - } - } - Collections.sort(anchorNodes, new Comparator() { - @Override - public int compare(AnchorNode o1, AnchorNode o2) { - return (int) (o1.prepare - o2.prepare); - } - }); +// Map anchorMap = doricContext.getPerformanceProfile().getAnchorMap(); +// for (String key : anchorMap.keySet()) { +// if (key.endsWith("#prepare")) { +// Long prepare = anchorMap.get(key); +// if (prepare != null) { +// AnchorNode anchorNode = new AnchorNode(); +// anchorNode.name = key.substring(0, key.lastIndexOf("#prepare")); +// anchorNode.prepare = prepare; +// anchorNodes.add(anchorNode); +// } +// } +// } +// Collections.sort(anchorNodes, new Comparator() { +// @Override +// public int compare(AnchorNode o1, AnchorNode o2) { +// return (int) (o1.prepare - o2.prepare); +// } +// }); } @Override 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 7016984d..25ec2358 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 @@ -65,6 +65,10 @@ public class DoricPerformanceProfile { this.name = name; } + public String getName() { + return this.name; + } + public void addAnchorHook(AnchorHook hook) { this.hooks.add(hook); } @@ -136,8 +140,6 @@ public class DoricPerformanceProfile { if (prepare == null) { prepare = start; } - Log.d(TAG, String.format("%s: %s prepared %dms, cost %dms.", - name, anchorName, start - prepare, end - start)); for (AnchorHook hook : hooks) { if (hook instanceof GlobalAnchorHook) { ((GlobalAnchorHook) hook).onAnchor(DoricPerformanceProfile.this, anchorName, prepare, start, end); diff --git a/doric-iOS/Devkit/Classes/DoricDev.m b/doric-iOS/Devkit/Classes/DoricDev.m index 80f9a9d5..56b7cf02 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.m +++ b/doric-iOS/Devkit/Classes/DoricDev.m @@ -27,7 +27,7 @@ #import "DoricDebugDriver.h" #import "DoricDevViewController.h" #import "DoricDevMonitor.h" -#import "DoricUtil.h" +#import "DoricDevPerformanceAnchorHook.h" @interface DoricContextDebuggable : NSObject @property(nonatomic, weak) DoricContext *doricContext; @@ -79,6 +79,7 @@ - (instancetype)init { _callbacks = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; _reloadingContexts = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; [DoricNativeDriver.instance.registry registerMonitor:[DoricDevMonitor new]]; + DoricNativeDriver.instance.registry.globalPerformanceAnchorHook = [DoricDevPerformanceAnchorHook new]; } return self; } @@ -199,7 +200,7 @@ - (void)reload:(NSString *)source script:(NSString *)script { DoricLog(@"Context source %@ in debugging,skip reload", source); } else { DoricLog(@"Context reload :id %@,source %@", context.contextId, source); - + dispatch_async(dispatch_get_main_queue(), ^{ [context reload:script]; [self.reloadingContexts addObject:context]; @@ -274,32 +275,32 @@ - (NSString *)ip { withString:@""]; } -UIViewController* _Nonnull findBestViewController(UIViewController* _Nonnull vc) { +UIViewController *_Nonnull findBestViewController(UIViewController *_Nonnull vc) { if (vc.presentedViewController && ![vc.presentedViewController isKindOfClass:[UIAlertController class]]) { // Return presented view controller return findBestViewController(vc.presentedViewController); } else if ([vc isKindOfClass:[UISplitViewController class]]) { // Return right hand side - UISplitViewController* svc = (UISplitViewController*) vc; + UISplitViewController *svc = (UISplitViewController *) vc; if (svc.viewControllers.count > 0) return findBestViewController(svc.viewControllers.lastObject); else return vc; } else if ([vc isKindOfClass:[UINavigationController class]]) { // Return top view - UINavigationController* svc = (UINavigationController*) vc; + UINavigationController *svc = (UINavigationController *) vc; if (svc.viewControllers.count > 0) return findBestViewController(svc.topViewController); else return vc; } else if ([vc isKindOfClass:[UITabBarController class]]) { // Return visible view - UITabBarController* svc = (UITabBarController*) vc; + UITabBarController *svc = (UITabBarController *) vc; if (svc.viewControllers.count > 0) return findBestViewController(svc.selectedViewController); else return vc; - } else { + } else { // Unknown view controller type, return last child view controller return vc; } diff --git a/doric-iOS/Devkit/Classes/DoricDevPerformanceAnchorHook.h b/doric-iOS/Devkit/Classes/DoricDevPerformanceAnchorHook.h new file mode 100644 index 00000000..b4421582 --- /dev/null +++ b/doric-iOS/Devkit/Classes/DoricDevPerformanceAnchorHook.h @@ -0,0 +1,9 @@ +// +// Created by pengfei.zhou on 2021/7/20. +// + +#import +#import + +@interface DoricDevPerformanceAnchorHook : NSObject +@end \ No newline at end of file diff --git a/doric-iOS/Devkit/Classes/DoricDevPerformanceAnchorHook.m b/doric-iOS/Devkit/Classes/DoricDevPerformanceAnchorHook.m new file mode 100644 index 00000000..4e89958c --- /dev/null +++ b/doric-iOS/Devkit/Classes/DoricDevPerformanceAnchorHook.m @@ -0,0 +1,17 @@ +// +// Created by pengfei.zhou on 2021/7/20. +// + +#import "DoricDevPerformanceAnchorHook.h" + + +@implementation DoricDevPerformanceAnchorHook +- (void)onAnchorName:(NSString *)name prepare:(NSNumber *)prepare start:(NSNumber *)start end:(NSNumber *)end in:(DoricPerformanceProfile *)profile { + NSLog(@"[DoricPerformance] %@: %@ prepared %@ms, cost %@ms", + profile.name, + name, + @(start.integerValue - prepare.integerValue), + @(end.integerValue - start.integerValue) + ); +} +@end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.h b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.h index 9f2190d5..25118677 100644 --- a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.h +++ b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.h @@ -42,6 +42,7 @@ NS_ASSUME_NONNULL_BEGIN @end @interface DoricPerformanceProfile : NSObject +@property(nonatomic, copy) NSString *name; - (instancetype)initWithName:(NSString *)name; diff --git a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m index f0b2ad42..a275d5c4 100644 --- a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m +++ b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m @@ -24,7 +24,6 @@ #import "DoricRegistry.h" @interface DoricPerformanceProfile () -@property(nonatomic, copy) NSString *name; @property(nonatomic, strong) dispatch_queue_t anchorQueue; @property(nonatomic, assign) BOOL enable; @property(nonatomic, strong) NSHashTable> *hooks; @@ -108,15 +107,9 @@ - (void)print:(NSString *)anchorName { if (!prepare) { prepare = start; } - NSLog(@"[DoricPerformanceProfile] %@: %@ prepared %@ms, cost %@ms", - self.name, - anchorName, - @(start.integerValue - prepare.integerValue), - @(end.integerValue - start.integerValue) - ); for (id hook in self.hooks) { if ([hook conformsToProtocol:@protocol(DoricPerformanceGlobalAnchorHookProtocol)]) { - [hook onAnchorName:anchorName prepare:end start:end end:end in:self]; + [(id ) hook onAnchorName:anchorName prepare:end start:end end:end in:self]; } else { [hook onAnchorName:anchorName prepare:prepare start:start end:end]; }