diff --git a/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java b/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java index 8e68d1ab..c7906c8e 100644 --- a/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java +++ b/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java @@ -44,5 +44,6 @@ public class MyApplication extends Application { DoricRegistry.setEnvironmentValue(map); } }, intentFilter); + //DoricRegistry.enablePerformance(true); } } diff --git a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java index 99359721..91b948ae 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -83,6 +83,16 @@ public class DoricRegistry { private Drawable defaultErrorDrawable = null; + private static boolean enablePerformance = false; + + public static void enablePerformance(boolean enable) { + enablePerformance = enable; + } + + public static boolean isEnablePerformance() { + return enablePerformance; + } + private static void initRegistry(DoricRegistry doricRegistry) { for (DoricLibrary library : doricLibraries) { library.load(doricRegistry); 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 2a4150fb..d77fa615 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 @@ -15,10 +15,14 @@ */ package pub.doric.performance; +import android.os.Handler; +import android.os.HandlerThread; import android.util.Log; +import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; + +import pub.doric.DoricRegistry; /** * @Description: pub.doric.performance @@ -37,18 +41,37 @@ public class DoricPerformanceProfile { private static final String MARK_END = "end"; private final String name; - private static final boolean DEBUG = true; + private boolean enable = DoricRegistry.isEnablePerformance(); + private static final Handler performanceHandler; + + static { + HandlerThread performanceThread = new HandlerThread("DoricPerformance"); + performanceThread.start(); + performanceHandler = new Handler(performanceThread.getLooper()); + } + public DoricPerformanceProfile(String name) { this.name = name; } - private final Map anchorMap = new ConcurrentHashMap<>(); + public void enable(boolean enable) { + this.enable = enable; + } - private void markAnchor(String eventName) { - if (DEBUG) { - anchorMap.put(eventName, System.currentTimeMillis()); + private final Map anchorMap = new HashMap<>(); + + private void markAnchor(final String eventName) { + if (!enable) { + return; } + performanceHandler.post(new Runnable() { + @Override + public void run() { + long time = System.currentTimeMillis(); + anchorMap.put(eventName, time); + } + }); } private String getPrepareAnchor(String anchorName) { @@ -73,25 +96,31 @@ public class DoricPerformanceProfile { public void end(String anchorName) { markAnchor(getEndAnchor(anchorName)); - if (DEBUG) { - print(anchorName); - } + print(anchorName); } - private void print(String anchorName) { - Long prepare = anchorMap.get(getPrepareAnchor(anchorName)); - Long start = anchorMap.get(getStartAnchor(anchorName)); - Long end = anchorMap.get(getEndAnchor(anchorName)); - if (end == null) { - end = System.currentTimeMillis(); + private void print(final String anchorName) { + if (!enable) { + return; } - if (start == null) { - start = end; - } - if (prepare == null) { - prepare = start; - } - Log.d(TAG, String.format("%s: %s prepared %dms, cost %dms.", - name, anchorName, start - prepare, end - start)); + performanceHandler.post(new Runnable() { + @Override + public void run() { + Long prepare = anchorMap.get(getPrepareAnchor(anchorName)); + Long start = anchorMap.get(getStartAnchor(anchorName)); + Long end = anchorMap.get(getEndAnchor(anchorName)); + if (end == null) { + end = System.currentTimeMillis(); + } + if (start == null) { + start = end; + } + if (prepare == null) { + prepare = start; + } + Log.d(TAG, String.format("%s: %s prepared %dms, cost %dms.", + name, anchorName, start - prepare, end - start)); + } + }); } } diff --git a/doric-iOS/Example/Example/ViewController.m b/doric-iOS/Example/Example/ViewController.m index 767b6d44..d50d8a87 100644 --- a/doric-iOS/Example/Example/ViewController.m +++ b/doric-iOS/Example/Example/ViewController.m @@ -46,6 +46,7 @@ - (void)viewDidLoad { it.delegate = self; }]]; [DoricRegistry register:[DemoLibrary new]]; + [DoricRegistry enablePerformance:YES]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { diff --git a/doric-iOS/Pod/Classes/DoricRegistry.h b/doric-iOS/Pod/Classes/DoricRegistry.h index d883398f..ff329b83 100644 --- a/doric-iOS/Pod/Classes/DoricRegistry.h +++ b/doric-iOS/Pod/Classes/DoricRegistry.h @@ -52,6 +52,10 @@ NS_ASSUME_NONNULL_BEGIN - (void)registerMonitor:(id )monitor; + (void)register:(DoricLibrary *)library; + ++ (void)enablePerformance:(BOOL)enable; + ++ (BOOL)isEnablePerformance; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Pod/Classes/DoricRegistry.m b/doric-iOS/Pod/Classes/DoricRegistry.m index 75e12a4e..b6211d87 100644 --- a/doric-iOS/Pod/Classes/DoricRegistry.m +++ b/doric-iOS/Pod/Classes/DoricRegistry.m @@ -59,6 +59,7 @@ @interface DoricLibraries : NSObject @property(nonatomic, strong) NSMutableSet *libraries; @property(nonatomic, strong) NSMutableArray *registries; @property(nonatomic, strong) NSMutableDictionary *envDic; +@property(nonatomic, assign) BOOL enablePerformance; + (instancetype)instance; @end @@ -69,6 +70,7 @@ - (instancetype)init { _libraries = [NSMutableSet new]; _registries = [NSMutableArray new]; _envDic = [NSMutableDictionary new]; + _enablePerformance = NO; } return self; } @@ -133,6 +135,14 @@ - (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine { return self; } ++ (void)enablePerformance:(BOOL)enable { + DoricLibraries.instance.enablePerformance = enable; +} + ++ (BOOL)isEnablePerformance { + return DoricLibraries.instance.enablePerformance; +} + - (void)innerRegister { [self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"]; [self registerNativePlugin:DoricModalPlugin.class withName:@"modal"]; diff --git a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m index 23326fe9..a6d9a23b 100644 --- a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m +++ b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m @@ -21,13 +21,13 @@ // #import "DoricPerformanceProfile.h" - -const bool _DEBUG = YES; +#import "DoricRegistry.h" @interface DoricPerformanceProfile () @property(nonatomic, copy) NSString *name; @property(nonatomic, strong) NSMutableDictionary *anchorMap; @property(nonatomic, strong) dispatch_queue_t anchorQueue; +@property(nonatomic, assign) BOOL enable; @end @implementation DoricPerformanceProfile @@ -36,6 +36,7 @@ - (instancetype)initWithName:(NSString *)name { _name = name; _anchorQueue = dispatch_queue_create("doric.performance.profile", DISPATCH_QUEUE_SERIAL); _anchorMap = [NSMutableDictionary new]; + _enable = [DoricRegistry isEnablePerformance]; } return self; } @@ -53,7 +54,7 @@ - (NSString *)getEndAnchor:(NSString *)anchorName { } - (void)markAnchor:(NSString *)eventName { - if (!_DEBUG) { + if (!self.enable) { return; } NSUInteger time = (NSUInteger) ([[NSDate date] timeIntervalSince1970] * 1000); @@ -73,12 +74,13 @@ - (void)start:(NSString *)anchorName { - (void)end:(NSString *)anchorName { [self markAnchor:[self getEndAnchor:anchorName]]; - if (_DEBUG) { - [self print:anchorName]; - } + [self print:anchorName]; } - (void)print:(NSString *)anchorName { + if (!self.enable) { + return; + } dispatch_async(self.anchorQueue, ^{ NSNumber *prepare = self.anchorMap[[self getPrepareAnchor:anchorName]]; NSNumber *start = self.anchorMap[[self getStartAnchor:anchorName]];