- 中文介绍请看这里
- 支持静态库方式启动analyzer:Support static library
Under the iOS platform, the analyzer is a tool which statistics CPU, FPS, Memory, Loading-Time and provides the output of statistical data. And contain SQL execution time monitor base on FMDatabase and UI refresh in main thread monitor
Because of the needs of project, I write the code for performance analyzer. As above, the analyzer can statistic some app-data, such as CPU, FPS, Memory, Loading-Time. And the memory overhead may be large because analyzer statistics every 0.5s defaultly(except FPS, it does it every 1s). So If you really care about memory overhead, you can switch off other statistical items and only open the Memory statistical item. As for how to turn off, you can see the detailed in below chapters.
- Statistic CPU, FPS, Memory, PageLoading-time. And them can be controled to show.
- Custom set the end flag of Loading-time.
- Skip modules which you don't want.
- Freedom to turn on or turn off by shake the iPhone device.
- Generate output file in sandbox.
- SQL execution time monitor. Set the threshold of the time. If timeout, you will receive notification.
- UI refresh monitor. If a view does not refresh in main thread, you will receive notification.
- Get statistical data.
- Copy directory
PerformanceAnalyzer/CHPerformanceAnalyzer/CHPerformanceAnalyzer/PerformanceAnalyzer/
to your project. - Run your APP!!! Don't need to do anythig except that.
You can define CHPerformanceAnalyzerShowType CHPerformanceAnalyzerShowTypeSetting;
anywhere in the global to control statistical items. The value of CHPerformanceAnalyzerShowType
just see CHGlobalDefines.h
Generally, your app delegate which is a class follows UIApplicationDelegate protocol is AppDelegate
. But if not, you need define NSString *CHPerformanceAnalyzerApplicationDelegateClassName = @"YourAppDelegate";
anywhere in the global.
You can complete above things in source code CHPerformancerExterns.m. Defaultly, there has some content and you may need to modify it.
- Analyzer will save current statistical data to sandbox when you tap statistical window twice by two fingers.
- CHPerformanceAnalyzer has a delegate which follows CHPerformanceAnalyzerDelegate protocol which contains an optional method
- (void)performanceAnalyzer:completeWithFilePath:
. Delegate will tell you fullpath of data save when you trigger save operation. - Analyzer save the data at shared directory of sandbox. You can add field
Application supports iTunes file sharing
into 'info.plist' and set it toYES
. And then connect the iPhone to iTunes. And selectApplication
item at iTunes and slide down to 'File Sharing' bar at right side. Finally, select your app, and later you can see content like below picture. Fileperformance.txt
saved the data - The format of statistical data is CSV-fromat of Excel. It can be import into Excel. You can find the import-tutorial at here(uhm..Chinese needed). Remember the separator is comma(',') defaultly. And If occur problem when transforming, I suggest that thansform the original data to CN-encoding such as 'GB2312' before import.
Sometimes view controller is special, that is, finishing loading the whole view is needed much time and meanwhile method - (void)viewDidAppear:
has been called. So, we need make a method to represent 'END FLAG' and replace analyzer's 'END FLAG'. Detailed code is in CHPerformancerExterns.m.
@interface WebViewController (PageLoading)
@end
@implementation WebViewController (PageLoading)
- (void)loadView_aop2
{
CHPerformanceAnalyzer *analyzer = [CHPerformanceAnalyzer sharedPerformanceAnalyzer];
// Observered Model. Make analyzer become observer of the self to concern the property path'navigationItem.title'.
[analyzer addObservered:self forKeyPath:@"navigationItem.title"];
// Fixed position.
[self loadView_aop2];
}
- (void)viewDidDisappear_aop:(BOOL)animated
{
[self viewDidDisappear_aop:animated];
CHPerformanceAnalyzer *analyzer = [CHPerformanceAnalyzer sharedPerformanceAnalyzer];
// '[analyzer addObservered:self forKeyPath:@"navigationItem.title"];' must be called, otherwise there will throw exception.
[analyzer removeObservered:self forKeyPath:@"navigationItem.title"];
}
@end
void(^CHPerformanceAnalyzerAOPInitializer)() = ^{
CHPerformanceAnalyzer *analyzer = [CHPerformanceAnalyzer sharedPerformanceAnalyzer];
[analyzer registerLoadingRuleWithClass:[WebViewController class]
originalSelector:@selector(loadView)
newSEL:@selector(loadView_aop2)];
[analyzer registerLoadingRuleWithClass:[WebViewController class]
originalSelector:@selector(viewDidDisappear:)
newSEL:@selector(viewDidDisappear_aop:)];
};
Finishing a webpage when method - (void)webViewDidFinishLoad:
has been called. I change the title of navigation. Function [analyzer addObservered:self forKeyPath:@"navigationItem.title"]
make anlayzer become observer of controller to concern the property path navigationItem.title
. When its value has been changed, analyzer will receive notification and update the loading-time. You can also concern other property path. I just throw out a minnow to catch a whale.
In [self loadView_aop2]
and CHPerformanceAnalyzerAOPInitializer
[analyzer registerLoadingRuleWithClass:[WebViewController class]
originalSelector:@selector(loadView)
newSEL:@selector(loadView_aop2)];
Their order cannot be reversed.
Note: can't use loadView_aop
as your name of custom method. Analyzer uses the name in the internal. If you do, program will go into infinite recursion until stack overflow.
Now, you can get all of module names which analyzer statisticed through property modulesOfStatistic
. According to the modules, call method - (id)statisticsWithType:ofKey:
to get statistical data. Generally, the method returns a NSArray object. To be currency in the future, write return type to id.
Shake the phone to turn off analyzer, turn on shake again.
- The module which Analyzer got comes from property
title
ofviewController
. If it's nil, analyzer will find from propertytitle
ofnavigationItem
. And if still nil, analyzer will try to call method- (NSString *)performanceAnalyzer: titleMethodWithViewController:
of analyzer's delegate. - The loading-time is cacluated from that before calling
- (void)loadView
to finish calling- (void)viewDidAppear:
- Xcode 7.3
- iOS 7.0+
- PureLayout
- libextobjc
- FMDB If you need db. This is optional.
- 2016-08-04 Support SQL execute time an UI refresh monitor
- 2016-04-08 The first ore-release