iOS:Add PerformanceProfile

This commit is contained in:
pengfei.zhou 2021-03-29 17:53:27 +08:00 committed by osborn
parent 51719d3fc7
commit a40413f991
7 changed files with 174 additions and 0 deletions

View File

@ -24,6 +24,7 @@
#import "DoricDriverProtocol.h" #import "DoricDriverProtocol.h"
#import "DoricNavigatorDelegate.h" #import "DoricNavigatorDelegate.h"
#import "DoricNavBarDelegate.h" #import "DoricNavBarDelegate.h"
#import "DoricPerformanceProfile.h"
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -44,6 +45,7 @@ NS_ASSUME_NONNULL_BEGIN
@property(nonatomic, strong) NSMutableDictionary <NSString *, NSMutableDictionary <NSString *, DoricViewNode *> *> *headNodes; @property(nonatomic, strong) NSMutableDictionary <NSString *, NSMutableDictionary <NSString *, DoricViewNode *> *> *headNodes;
@property(nonatomic, copy) NSString *extra; @property(nonatomic, copy) NSString *extra;
@property(nonatomic, assign) BOOL destroyed; @property(nonatomic, assign) BOOL destroyed;
@property(nonatomic, strong) DoricPerformanceProfile *performanceProfile;
- (instancetype)initWithScript:(NSString *)script source:(NSString *)source extra:(NSString *)extra; - (instancetype)initWithScript:(NSString *)script source:(NSString *)source extra:(NSString *)extra;

View File

@ -53,6 +53,7 @@ + (instancetype)instance {
- (void)createContext:(DoricContext *)context script:(NSString *)script source:(NSString *)source { - (void)createContext:(DoricContext *)context script:(NSString *)script source:(NSString *)source {
context.contextId = [NSString stringWithFormat:@"%ld", (long) ++self.counter]; context.contextId = [NSString stringWithFormat:@"%ld", (long) ++self.counter];
context.performanceProfile = [[DoricPerformanceProfile alloc] initWithName:context.contextId];
dispatch_sync(self.mapQueue, ^() { dispatch_sync(self.mapQueue, ^() {
[self.contextMapTable setObject:context forKey:context.contextId]; [self.contextMapTable setObject:context forKey:context.contextId];
}); });

View File

@ -24,6 +24,7 @@
#import "DoricJSEngine.h" #import "DoricJSEngine.h"
#import "DoricConstant.h" #import "DoricConstant.h"
#import "DoricContextManager.h" #import "DoricContextManager.h"
#import "DoricPerformanceProfile.h"
@interface DoricNativeDriver () @interface DoricNativeDriver ()
@property(nonatomic, strong) DoricJSEngine *jsExecutor; @property(nonatomic, strong) DoricJSEngine *jsExecutor;
@ -109,20 +110,28 @@ - (DoricAsyncResult *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArr
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method arguments:(va_list)args { - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method arguments:(va_list)args {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init]; DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init]; NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableArray *printedArgs = [[NSMutableArray alloc] init];
[array addObject:contextId]; [array addObject:contextId];
[array addObject:method]; [array addObject:method];
[printedArgs addObject:method];
id arg = va_arg(args, id); id arg = va_arg(args, id);
while (arg != nil) { while (arg != nil) {
[array addObject:arg]; [array addObject:arg];
[printedArgs addObject:arg];
arg = va_arg(args, JSValue *); arg = va_arg(args, JSValue *);
} }
DoricPerformanceProfile *performanceProfile = [DoricContextManager.instance getContext:contextId].performanceProfile;
NSString *anchorName = [NSString stringWithFormat:@"call:%@", [printedArgs componentsJoinedByString:@","]];
[performanceProfile prepare:anchorName];
__weak typeof(self) _self = self; __weak typeof(self) _self = self;
[self.jsExecutor ensureRunOnJSThread:^{ [self.jsExecutor ensureRunOnJSThread:^{
__strong typeof(_self) self = _self; __strong typeof(_self) self = _self;
if (!self) return; if (!self) return;
@try { @try {
[performanceProfile start:anchorName];
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array]; JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array];
[ret setupResult:jsValue]; [ret setupResult:jsValue];
[performanceProfile end:anchorName];
} @catch (NSException *exception) { } @catch (NSException *exception) {
[ret setupError:exception]; [ret setupError:exception];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]]; [self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
@ -139,13 +148,18 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
for (id arg in args) { for (id arg in args) {
[array addObject:arg]; [array addObject:arg];
} }
DoricPerformanceProfile *performanceProfile = [DoricContextManager.instance getContext:contextId].performanceProfile;
NSString *anchorName = [NSString stringWithFormat:@"call:%@,%@", method, [args componentsJoinedByString:@","]];
[performanceProfile prepare:anchorName];
__weak typeof(self) _self = self; __weak typeof(self) _self = self;
[self.jsExecutor ensureRunOnJSThread:^{ [self.jsExecutor ensureRunOnJSThread:^{
__strong typeof(_self) self = _self; __strong typeof(_self) self = _self;
if (!self) return; if (!self) return;
@try { @try {
[performanceProfile start:anchorName];
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array]; JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array];
[ret setupResult:jsValue]; [ret setupResult:jsValue];
[performanceProfile end:anchorName];
} @catch (NSException *exception) { } @catch (NSException *exception) {
[ret setupError:exception]; [ret setupError:exception];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]]; [self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
@ -156,13 +170,17 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
- (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source { - (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init]; DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
DoricPerformanceProfile *performanceProfile = [DoricContextManager.instance getContext:contextId].performanceProfile;
[performanceProfile prepare:@"Create"];
__weak typeof(self) _self = self; __weak typeof(self) _self = self;
[self.jsExecutor ensureRunOnJSThread:^{ [self.jsExecutor ensureRunOnJSThread:^{
__strong typeof(_self) self = _self; __strong typeof(_self) self = _self;
if (!self) return; if (!self) return;
@try { @try {
[performanceProfile start:@"Create"];
[self.jsExecutor prepareContext:contextId script:script source:source]; [self.jsExecutor prepareContext:contextId script:script source:source];
[ret setupResult:@YES]; [ret setupResult:@YES];
[performanceProfile end:@"Create"];
} @catch (NSException *exception) { } @catch (NSException *exception) {
[ret setupError:exception]; [ret setupError:exception];
[self.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]]; [self.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
@ -173,13 +191,17 @@ - (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)scr
- (DoricAsyncResult *)destroyContext:(NSString *)contextId { - (DoricAsyncResult *)destroyContext:(NSString *)contextId {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init]; DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
DoricPerformanceProfile *performanceProfile = [DoricContextManager.instance getContext:contextId].performanceProfile;
[performanceProfile prepare:@"Destroy"];
__weak typeof(self) _self = self; __weak typeof(self) _self = self;
[self.jsExecutor ensureRunOnJSThread:^{ [self.jsExecutor ensureRunOnJSThread:^{
__strong typeof(_self) self = _self; __strong typeof(_self) self = _self;
if (!self) return; if (!self) return;
@try { @try {
[performanceProfile start:@"Destroy"];
[self.jsExecutor destroyContext:contextId]; [self.jsExecutor destroyContext:contextId];
[ret setupResult:@YES]; [ret setupResult:@YES];
[performanceProfile end:@"Destroy"];
} @catch (NSException *exception) { } @catch (NSException *exception) {
[ret setupError:exception]; [ret setupError:exception];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]]; [self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];

View File

@ -28,6 +28,7 @@
#import <sys/utsname.h> #import <sys/utsname.h>
#import "DoricContext.h" #import "DoricContext.h"
#import "DoricContextManager.h" #import "DoricContextManager.h"
#import "DoricPerformanceProfile.h"
@interface DoricDefaultMonitor : NSObject <DoricMonitorProtocol> @interface DoricDefaultMonitor : NSObject <DoricMonitorProtocol>
@end @end
@ -49,6 +50,7 @@ @interface DoricJSEngine ()
@property(nonatomic, strong) NSThread *jsThread; @property(nonatomic, strong) NSThread *jsThread;
@property(nonatomic, assign) BOOL destroyed; @property(nonatomic, assign) BOOL destroyed;
@property(nonatomic, assign) BOOL initialized; @property(nonatomic, assign) BOOL initialized;
@property(nonatomic, strong) DoricPerformanceProfile *profile;
@end @end
@implementation DoricJSEngine @implementation DoricJSEngine
@ -56,6 +58,8 @@ @implementation DoricJSEngine
- (instancetype)init { - (instancetype)init {
if (self = [super init]) { if (self = [super init]) {
_initialized = NO; _initialized = NO;
_profile = [[DoricPerformanceProfile alloc] initWithName:@"JSEngine"];
[_profile prepare:@"Init"];
_jsThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadRun) object:nil]; _jsThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadRun) object:nil];
[_jsThread start]; [_jsThread start];
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
@ -93,6 +97,7 @@ - (instancetype)init {
}.mutableCopy; }.mutableCopy;
self.registry = [[DoricRegistry alloc] initWithJSEngine:self]; self.registry = [[DoricRegistry alloc] initWithJSEngine:self];
[self ensureRunOnJSThread:^() { [self ensureRunOnJSThread:^() {
[_profile start:@"Init"];
self.timers = [[NSMutableDictionary alloc] init]; self.timers = [[NSMutableDictionary alloc] init];
self.bridgeExtension = [DoricBridgeExtension new]; self.bridgeExtension = [DoricBridgeExtension new];
self.bridgeExtension.registry = self.registry; self.bridgeExtension.registry = self.registry;
@ -100,6 +105,7 @@ - (instancetype)init {
[self initJSExecutor]; [self initJSExecutor];
[self initDoricEnvironment]; [self initDoricEnvironment];
self.initialized = YES; self.initialized = YES;
[_profile end:@"Init"];
}]; }];
[self.registry registerMonitor:[DoricDefaultMonitor new]]; [self.registry registerMonitor:[DoricDefaultMonitor new]];
} }

View File

@ -0,0 +1,37 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// DoricPerformanceProfile.h
// DoricCore
//
// Created by pengfei.zhou on 2021/3/29.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface DoricPerformanceProfile : NSObject
- (instancetype)initWithName:(NSString *)name;
- (void)prepare:(NSString *)anchorName;
- (void)start:(NSString *)anchorName;
- (void)end:(NSString *)anchorName;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,103 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// DoricPerformanceProfile.m
// DoricCore
//
// Created by pengfei.zhou on 2021/3/29.
//
#import "DoricPerformanceProfile.h"
const bool _DEBUG = YES;
@interface DoricPerformanceProfile ()
@property(nonatomic, copy) NSString *name;
@property(nonatomic, strong) NSMutableDictionary <NSString *, NSNumber *> *anchorMap;
@property(nonatomic, strong) dispatch_queue_t anchorQueue;
@end
@implementation DoricPerformanceProfile
- (instancetype)initWithName:(NSString *)name {
if (self = [super init]) {
_name = name;
_anchorQueue = dispatch_queue_create("doric.performance.profile", DISPATCH_QUEUE_SERIAL);
_anchorMap = [NSMutableDictionary new];
}
return self;
}
- (NSString *)getPrepareAnchor:(NSString *)anchorName {
return [NSString stringWithFormat:@"%@#prepare", anchorName];
}
- (NSString *)getStartAnchor:(NSString *)anchorName {
return [NSString stringWithFormat:@"%@#start", anchorName];
}
- (NSString *)getEndAnchor:(NSString *)anchorName {
return [NSString stringWithFormat:@"%@#end", anchorName];
}
- (void)markAnchor:(NSString *)eventName {
if (!_DEBUG) {
return;
}
NSUInteger time = (NSUInteger) ([[NSDate date] timeIntervalSince1970] * 1000);
dispatch_async(self.anchorQueue, ^{
self.anchorMap[eventName] = @(time);
});
}
- (void)prepare:(NSString *)anchorName {
[self markAnchor:[self getPrepareAnchor:anchorName]];
}
- (void)start:(NSString *)anchorName {
[self markAnchor:[self getStartAnchor:anchorName]];
}
- (void)end:(NSString *)anchorName {
[self markAnchor:[self getEndAnchor:anchorName]];
if (_DEBUG) {
[self print:anchorName];
}
}
- (void)print:(NSString *)anchorName {
dispatch_async(self.anchorQueue, ^{
NSNumber *prepare = self.anchorMap[[self getPrepareAnchor:anchorName]];
NSNumber *start = self.anchorMap[[self getStartAnchor:anchorName]];
NSNumber *end = self.anchorMap[[self getEndAnchor:anchorName]];
if (!end) {
end = @( (NSUInteger) [[NSDate date] timeIntervalSince1970] * 1000);
}
if (!start) {
start = end;
}
if (!prepare) {
prepare = start;
}
NSLog(@"[DoricPerformanceProfile] %@: %@ prepared %@ms, cost %@ms",
self.name,
anchorName,
@(start.integerValue - prepare.integerValue),
@(end.integerValue - start.integerValue)
);
});
}
@end

View File

@ -33,12 +33,14 @@ - (void)render:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
if (!argument) { if (!argument) {
return; return;
} }
[self.doricContext.performanceProfile prepare:@"Render"];
__weak typeof(self) _self = self; __weak typeof(self) _self = self;
[self.doricContext dispatchToMainQueue:^{ [self.doricContext dispatchToMainQueue:^{
__strong typeof(_self) self = _self; __strong typeof(_self) self = _self;
if (self.doricContext == nil) { if (self.doricContext == nil) {
return; return;
} }
[self.doricContext.performanceProfile start:@"Render"];
NSString *viewId = [argument optString:@"id"]; NSString *viewId = [argument optString:@"id"];
if (self.doricContext.rootNode.viewId == nil && [@"Root" isEqualToString:[argument optString:@"type"]]) { if (self.doricContext.rootNode.viewId == nil && [@"Root" isEqualToString:[argument optString:@"type"]]) {
@ -51,6 +53,7 @@ - (void)render:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
[viewNode requestLayout]; [viewNode requestLayout];
} }
[promise resolve:nil]; [promise resolve:nil];
[self.doricContext.performanceProfile end:@"Render"];
}]; }];
} }