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);
}
}, intentFilter);
//DoricRegistry.enablePerformance(true);
}
}

View File

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

View File

@ -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<String, Long> 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<String, Long> 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));
}
});
}
}

View File

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

View File

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

View File

@ -59,6 +59,7 @@ @interface DoricLibraries : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@property(nonatomic, strong) NSMutableArray <NSValue *> *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"];

View File

@ -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 <NSString *, NSNumber *> *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]];