feat:use global anchor hook to print log

This commit is contained in:
pengfei.zhou 2021-07-20 11:29:53 +08:00 committed by osborn
parent 17e14e7119
commit a021aae3de
9 changed files with 71 additions and 43 deletions

View File

@ -52,12 +52,7 @@ public class DoricDev {
private DoricDev() { private DoricDev() {
this.isRunningInEmulator = SimulatorUtil.isSimulator(Doric.application()); this.isRunningInEmulator = SimulatorUtil.isSimulator(Doric.application());
DoricNativeDriver.getInstance().getRegistry().registerMonitor(new DoricDevMonitor()); DoricNativeDriver.getInstance().getRegistry().registerMonitor(new DoricDevMonitor());
DoricNativeDriver.getInstance().getRegistry().setGlobalPerformanceAnchorHook(new DoricPerformanceProfile.AnchorHook() { DoricNativeDriver.getInstance().getRegistry().setGlobalPerformanceAnchorHook(new DoricDevPerformanceAnchorHook());
@Override
public void onAnchor(String name, long prepare, long start, long end) {
}
});
} }
public static DoricDev getInstance() { public static DoricDev getInstance() {

View File

@ -15,6 +15,8 @@
*/ */
package pub.doric.devkit; package pub.doric.devkit;
import android.util.Log;
import pub.doric.performance.DoricPerformanceProfile; import pub.doric.performance.DoricPerformanceProfile;
/** /**
@ -22,9 +24,17 @@ import pub.doric.performance.DoricPerformanceProfile;
* @Author: pengfei.zhou * @Author: pengfei.zhou
* @CreateDate: 2021/7/20 * @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 @Override
public void onAnchor(String name, long prepare, long start, long end) { public void onAnchor(String name, long prepare, long start, long end) {
//DO nothing
} }
} }

View File

@ -78,24 +78,24 @@ public class DoricDevPerfActivity extends DoricDevBaseActivity {
private List<AnchorNode> anchorNodes = new ArrayList<>(); private List<AnchorNode> anchorNodes = new ArrayList<>();
private MyAdapter() { private MyAdapter() {
Map<String, Long> anchorMap = doricContext.getPerformanceProfile().getAnchorMap(); // Map<String, Long> anchorMap = doricContext.getPerformanceProfile().getAnchorMap();
for (String key : anchorMap.keySet()) { // for (String key : anchorMap.keySet()) {
if (key.endsWith("#prepare")) { // if (key.endsWith("#prepare")) {
Long prepare = anchorMap.get(key); // Long prepare = anchorMap.get(key);
if (prepare != null) { // if (prepare != null) {
AnchorNode anchorNode = new AnchorNode(); // AnchorNode anchorNode = new AnchorNode();
anchorNode.name = key.substring(0, key.lastIndexOf("#prepare")); // anchorNode.name = key.substring(0, key.lastIndexOf("#prepare"));
anchorNode.prepare = prepare; // anchorNode.prepare = prepare;
anchorNodes.add(anchorNode); // anchorNodes.add(anchorNode);
} // }
} // }
} // }
Collections.sort(anchorNodes, new Comparator<AnchorNode>() { // Collections.sort(anchorNodes, new Comparator<AnchorNode>() {
@Override // @Override
public int compare(AnchorNode o1, AnchorNode o2) { // public int compare(AnchorNode o1, AnchorNode o2) {
return (int) (o1.prepare - o2.prepare); // return (int) (o1.prepare - o2.prepare);
} // }
}); // });
} }
@Override @Override

View File

@ -65,6 +65,10 @@ public class DoricPerformanceProfile {
this.name = name; this.name = name;
} }
public String getName() {
return this.name;
}
public void addAnchorHook(AnchorHook hook) { public void addAnchorHook(AnchorHook hook) {
this.hooks.add(hook); this.hooks.add(hook);
} }
@ -136,8 +140,6 @@ public class DoricPerformanceProfile {
if (prepare == null) { if (prepare == null) {
prepare = start; prepare = start;
} }
Log.d(TAG, String.format("%s: %s prepared %dms, cost %dms.",
name, anchorName, start - prepare, end - start));
for (AnchorHook hook : hooks) { for (AnchorHook hook : hooks) {
if (hook instanceof GlobalAnchorHook) { if (hook instanceof GlobalAnchorHook) {
((GlobalAnchorHook) hook).onAnchor(DoricPerformanceProfile.this, anchorName, prepare, start, end); ((GlobalAnchorHook) hook).onAnchor(DoricPerformanceProfile.this, anchorName, prepare, start, end);

View File

@ -27,7 +27,7 @@
#import "DoricDebugDriver.h" #import "DoricDebugDriver.h"
#import "DoricDevViewController.h" #import "DoricDevViewController.h"
#import "DoricDevMonitor.h" #import "DoricDevMonitor.h"
#import "DoricUtil.h" #import "DoricDevPerformanceAnchorHook.h"
@interface DoricContextDebuggable : NSObject @interface DoricContextDebuggable : NSObject
@property(nonatomic, weak) DoricContext *doricContext; @property(nonatomic, weak) DoricContext *doricContext;
@ -79,6 +79,7 @@ - (instancetype)init {
_callbacks = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; _callbacks = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
_reloadingContexts = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; _reloadingContexts = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
[DoricNativeDriver.instance.registry registerMonitor:[DoricDevMonitor new]]; [DoricNativeDriver.instance.registry registerMonitor:[DoricDevMonitor new]];
DoricNativeDriver.instance.registry.globalPerformanceAnchorHook = [DoricDevPerformanceAnchorHook new];
} }
return self; return self;
} }
@ -274,27 +275,27 @@ - (NSString *)ip {
withString:@""]; withString:@""];
} }
UIViewController* _Nonnull findBestViewController(UIViewController* _Nonnull vc) { UIViewController *_Nonnull findBestViewController(UIViewController *_Nonnull vc) {
if (vc.presentedViewController && ![vc.presentedViewController isKindOfClass:[UIAlertController class]]) { if (vc.presentedViewController && ![vc.presentedViewController isKindOfClass:[UIAlertController class]]) {
// Return presented view controller // Return presented view controller
return findBestViewController(vc.presentedViewController); return findBestViewController(vc.presentedViewController);
} else if ([vc isKindOfClass:[UISplitViewController class]]) { } else if ([vc isKindOfClass:[UISplitViewController class]]) {
// Return right hand side // Return right hand side
UISplitViewController* svc = (UISplitViewController*) vc; UISplitViewController *svc = (UISplitViewController *) vc;
if (svc.viewControllers.count > 0) if (svc.viewControllers.count > 0)
return findBestViewController(svc.viewControllers.lastObject); return findBestViewController(svc.viewControllers.lastObject);
else else
return vc; return vc;
} else if ([vc isKindOfClass:[UINavigationController class]]) { } else if ([vc isKindOfClass:[UINavigationController class]]) {
// Return top view // Return top view
UINavigationController* svc = (UINavigationController*) vc; UINavigationController *svc = (UINavigationController *) vc;
if (svc.viewControllers.count > 0) if (svc.viewControllers.count > 0)
return findBestViewController(svc.topViewController); return findBestViewController(svc.topViewController);
else else
return vc; return vc;
} else if ([vc isKindOfClass:[UITabBarController class]]) { } else if ([vc isKindOfClass:[UITabBarController class]]) {
// Return visible view // Return visible view
UITabBarController* svc = (UITabBarController*) vc; UITabBarController *svc = (UITabBarController *) vc;
if (svc.viewControllers.count > 0) if (svc.viewControllers.count > 0)
return findBestViewController(svc.selectedViewController); return findBestViewController(svc.selectedViewController);
else else

View File

@ -0,0 +1,9 @@
//
// Created by pengfei.zhou on 2021/7/20.
//
#import <Foundation/Foundation.h>
#import <DoricCore/Doric.h>
@interface DoricDevPerformanceAnchorHook : NSObject<DoricPerformanceGlobalAnchorHookProtocol>
@end

View File

@ -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

View File

@ -42,6 +42,7 @@ NS_ASSUME_NONNULL_BEGIN
@end @end
@interface DoricPerformanceProfile : NSObject @interface DoricPerformanceProfile : NSObject
@property(nonatomic, copy) NSString *name;
- (instancetype)initWithName:(NSString *)name; - (instancetype)initWithName:(NSString *)name;

View File

@ -24,7 +24,6 @@
#import "DoricRegistry.h" #import "DoricRegistry.h"
@interface DoricPerformanceProfile () @interface DoricPerformanceProfile ()
@property(nonatomic, copy) NSString *name;
@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; @property(nonatomic, strong) NSHashTable<id <DoricPerformanceAnchorHookProtocol>> *hooks;
@ -108,15 +107,9 @@ - (void)print:(NSString *)anchorName {
if (!prepare) { if (!prepare) {
prepare = start; prepare = start;
} }
NSLog(@"[DoricPerformanceProfile] %@: %@ prepared %@ms, cost %@ms",
self.name,
anchorName,
@(start.integerValue - prepare.integerValue),
@(end.integerValue - start.integerValue)
);
for (id <DoricPerformanceAnchorHookProtocol> hook in self.hooks) { for (id <DoricPerformanceAnchorHookProtocol> hook in self.hooks) {
if ([hook conformsToProtocol:@protocol(DoricPerformanceGlobalAnchorHookProtocol)]) { if ([hook conformsToProtocol:@protocol(DoricPerformanceGlobalAnchorHookProtocol)]) {
[hook onAnchorName:anchorName prepare:end start:end end:end in:self]; [(id <DoricPerformanceGlobalAnchorHookProtocol>) hook onAnchorName:anchorName prepare:end start:end end:end in:self];
} else { } else {
[hook onAnchorName:anchorName prepare:prepare start:start end:end]; [hook onAnchorName:anchorName prepare:prepare start:start end:end];
} }