feat:add switch for performance,default is off

This commit is contained in:
pengfei.zhou 2021-07-08 11:58:22 +08:00 committed by osborn
parent 34876de69f
commit f5bd4406d9
7 changed files with 86 additions and 29 deletions

View File

@ -44,5 +44,6 @@ public class MyApplication extends Application {
DoricRegistry.setEnvironmentValue(map); DoricRegistry.setEnvironmentValue(map);
} }
}, intentFilter); }, intentFilter);
//DoricRegistry.enablePerformance(true);
} }
} }

View File

@ -83,6 +83,16 @@ public class DoricRegistry {
private Drawable defaultErrorDrawable = null; 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) { private static void initRegistry(DoricRegistry doricRegistry) {
for (DoricLibrary library : doricLibraries) { for (DoricLibrary library : doricLibraries) {
library.load(doricRegistry); library.load(doricRegistry);

View File

@ -15,10 +15,14 @@
*/ */
package pub.doric.performance; package pub.doric.performance;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log; import android.util.Log;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import pub.doric.DoricRegistry;
/** /**
* @Description: pub.doric.performance * @Description: pub.doric.performance
@ -37,18 +41,37 @@ public class DoricPerformanceProfile {
private static final String MARK_END = "end"; private static final String MARK_END = "end";
private final String name; 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) { public DoricPerformanceProfile(String name) {
this.name = name; this.name = name;
} }
private final Map<String, Long> anchorMap = new ConcurrentHashMap<>(); public void enable(boolean enable) {
this.enable = enable;
}
private void markAnchor(String eventName) { private final Map<String, Long> anchorMap = new HashMap<>();
if (DEBUG) {
anchorMap.put(eventName, System.currentTimeMillis()); 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) { private String getPrepareAnchor(String anchorName) {
@ -73,25 +96,31 @@ public class DoricPerformanceProfile {
public void end(String anchorName) { public void end(String anchorName) {
markAnchor(getEndAnchor(anchorName)); markAnchor(getEndAnchor(anchorName));
if (DEBUG) { print(anchorName);
print(anchorName);
}
} }
private void print(String anchorName) { private void print(final String anchorName) {
Long prepare = anchorMap.get(getPrepareAnchor(anchorName)); if (!enable) {
Long start = anchorMap.get(getStartAnchor(anchorName)); return;
Long end = anchorMap.get(getEndAnchor(anchorName));
if (end == null) {
end = System.currentTimeMillis();
} }
if (start == null) { performanceHandler.post(new Runnable() {
start = end; @Override
} public void run() {
if (prepare == null) { Long prepare = anchorMap.get(getPrepareAnchor(anchorName));
prepare = start; Long start = anchorMap.get(getStartAnchor(anchorName));
} Long end = anchorMap.get(getEndAnchor(anchorName));
Log.d(TAG, String.format("%s: %s prepared %dms, cost %dms.", if (end == null) {
name, anchorName, start - prepare, end - start)); 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));
}
});
} }
} }

View File

@ -46,6 +46,7 @@ - (void)viewDidLoad {
it.delegate = self; it.delegate = self;
}]]; }]];
[DoricRegistry register:[DemoLibrary new]]; [DoricRegistry register:[DemoLibrary new]];
[DoricRegistry enablePerformance:YES];
} }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

View File

@ -52,6 +52,10 @@ NS_ASSUME_NONNULL_BEGIN
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor; - (void)registerMonitor:(id <DoricMonitorProtocol>)monitor;
+ (void)register:(DoricLibrary *)library; + (void)register:(DoricLibrary *)library;
+ (void)enablePerformance:(BOOL)enable;
+ (BOOL)isEnablePerformance;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@ -59,6 +59,7 @@ @interface DoricLibraries : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries; @property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@property(nonatomic, strong) NSMutableArray <NSValue *> *registries; @property(nonatomic, strong) NSMutableArray <NSValue *> *registries;
@property(nonatomic, strong) NSMutableDictionary *envDic; @property(nonatomic, strong) NSMutableDictionary *envDic;
@property(nonatomic, assign) BOOL enablePerformance;
+ (instancetype)instance; + (instancetype)instance;
@end @end
@ -69,6 +70,7 @@ - (instancetype)init {
_libraries = [NSMutableSet new]; _libraries = [NSMutableSet new];
_registries = [NSMutableArray new]; _registries = [NSMutableArray new];
_envDic = [NSMutableDictionary new]; _envDic = [NSMutableDictionary new];
_enablePerformance = NO;
} }
return self; return self;
} }
@ -133,6 +135,14 @@ - (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine {
return self; return self;
} }
+ (void)enablePerformance:(BOOL)enable {
DoricLibraries.instance.enablePerformance = enable;
}
+ (BOOL)isEnablePerformance {
return DoricLibraries.instance.enablePerformance;
}
- (void)innerRegister { - (void)innerRegister {
[self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"]; [self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"];
[self registerNativePlugin:DoricModalPlugin.class withName:@"modal"]; [self registerNativePlugin:DoricModalPlugin.class withName:@"modal"];

View File

@ -21,13 +21,13 @@
// //
#import "DoricPerformanceProfile.h" #import "DoricPerformanceProfile.h"
#import "DoricRegistry.h"
const bool _DEBUG = YES;
@interface DoricPerformanceProfile () @interface DoricPerformanceProfile ()
@property(nonatomic, copy) NSString *name; @property(nonatomic, copy) NSString *name;
@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;
@end @end
@implementation DoricPerformanceProfile @implementation DoricPerformanceProfile
@ -36,6 +36,7 @@ - (instancetype)initWithName:(NSString *)name {
_name = name; _name = 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];
} }
return self; return self;
} }
@ -53,7 +54,7 @@ - (NSString *)getEndAnchor:(NSString *)anchorName {
} }
- (void)markAnchor:(NSString *)eventName { - (void)markAnchor:(NSString *)eventName {
if (!_DEBUG) { if (!self.enable) {
return; return;
} }
NSUInteger time = (NSUInteger) ([[NSDate date] timeIntervalSince1970] * 1000); NSUInteger time = (NSUInteger) ([[NSDate date] timeIntervalSince1970] * 1000);
@ -73,12 +74,13 @@ - (void)start:(NSString *)anchorName {
- (void)end:(NSString *)anchorName { - (void)end:(NSString *)anchorName {
[self markAnchor:[self getEndAnchor:anchorName]]; [self markAnchor:[self getEndAnchor:anchorName]];
if (_DEBUG) { [self print:anchorName];
[self print:anchorName];
}
} }
- (void)print:(NSString *)anchorName { - (void)print:(NSString *)anchorName {
if (!self.enable) {
return;
}
dispatch_async(self.anchorQueue, ^{ dispatch_async(self.anchorQueue, ^{
NSNumber *prepare = self.anchorMap[[self getPrepareAnchor:anchorName]]; NSNumber *prepare = self.anchorMap[[self getPrepareAnchor:anchorName]];
NSNumber *start = self.anchorMap[[self getStartAnchor:anchorName]]; NSNumber *start = self.anchorMap[[self getStartAnchor:anchorName]];