Application Performance Monitoring (APM) uses the mobile analysis client software development kit (SDK) for instrumentation. The SDK collects app performance data, generates logs, and uploads them to the server. The server then parses the instrumentation logs from the client based on a specified data format. This process allows the server to monitor and analyze various client-side metrics.
This topic describes how to configure performance monitoring instrumentation on the client for features such as network monitoring, H5 performance monitoring, page load monitoring, startup speed monitoring, and Mini Program monitoring.
Prerequisites
The mPaaS baseline version is 10.1.68.44 or later. If you are using an earlier version, you must upgrade it. For more information, see mPaaS 10.1.68 upgrade guide.
Ensure that you have integrated the Mobile Analytics component. For more information, see Integrate the Mobile Analytics service.
Ensure that the mobile gateway component is connected. For more information, see Mobile Gateway Service.
To use the H5 performance monitoring feature, you must integrate the H5 container. For more information, see Integrate with the H5 container.
To use the Mini Program performance monitoring feature, you must integrate the Mini Program component. For more information, see Integrate with Mini Program.
Network monitoring
Enable network monitoring
You can manually enable APM network performance monitoring instrumentation in your project code. During app startup, call the following code to enable network performance monitoring.
#import <MPMasAdapter/MPAnalysisHelper.h>
[MPAnalysisHelper startAPMNetMonitor];mPaaS RPC network monitoring instrumentation
After you enable APM network monitoring, the built-in mPaaS network framework automatically performs instrumentation for mPaaS RPC. You do not need to add manual instrumentation to your code.
Custom business network monitoring instrumentation
If you use a network communication method other than mPaaS RPC and want to monitor its network quality, you must enable APM network monitoring during app startup. You must also add manual instrumentation for your custom network calls.
Call the following code for instrumentation:
#import <MPMasAdapter/MPRemoteLoggingInterface.h>
NSDictionary *param = @{
@"totalRequestTimeInterval": @(totalRequestTime),
@"dnsTimeInterval": @(dnsTime),
@"tcpTimeInterval": @(tcpTime),
@"sslTimeInterval": @(sslTime),
@"firstPackageTimeInterval": @(firstPackageTime),
@"transportTimeInterval": @(transportTime),
@"downloadAverageSpeed": @(downloadAverageSpeed),
@"httpStatusCode": @(httpStatusCode),
@"rpcStatusCode": rpcStatusCode,
@"operationType": operationType,
@"clientIP": clientIP,
@"gwIP": gwIP,
@"traceId": traceId
};
[MPRemoteLoggingInterface writeAPMNetLog:param];The input parameters are as follows:
Parameter | Description |
totalRequestTimeInterval | Total request time. Unit: ms. |
dnsTimeInterval | DNS parsing time. Unit: ms. |
tcpTimeInterval | TCP connection time. Unit: ms. |
SSLTimeInterval | SSL connection time. Unit: ms. |
firstPackageTimeInterval | Time to first byte (TTFB). This is the time from sending a request to receiving the first data packet from the server. Unit: ms. |
transportTimeInterval | Data transfer time. This is the time from receiving the first data packet to receiving the last data packet. Unit: ms. |
downloadAverageSpeed | Average download speed of the resource file. |
httpStatusCode | HTTP status code. A value of 200 indicates normal. |
rpcStatusCode | RPC status code. A value of 200 indicates normal. |
operationType | Interface identifier. It identifies the gateway operation. |
clientIP | Client IP address. |
gwIP | Gateway IP address. |
traceId | Trace ID of the client request. |
H5 performance monitoring
The mPaaS H5 container automatically performs instrumentation for H5 performance monitoring. You only need to integrate the H5 container. No manual instrumentation is required.
Page load monitoring
The mPaaS native page automated instrumentation SDK performs instrumentation for native page load times. After you integrate the mobile analysis component, no manual instrumentation is required.
Startup speed monitoring
Based on the mPaaS framework
If your project integrates the mPaaS framework, the framework records the startup speed. After the app starts, for example, when the home page appears, you can record the startup time and send a startup completion notification.
// globalMonitorStartUpTime is a predefined variable. You can use it after you import the <mPaas/MPaaS+MonitorStartUpTime.h> header file.
double time = CFAbsoluteTimeGetCurrent() - globalMonitorStartUpTime;
[ [NSNotificationCenter defaultCenter] postNotificationName:@"APMonitor_Startup_Cost_Time" object:nil userInfo:@{@"CostTimeOnUserFeel": [NSString stringWithFormat:@"%f", time]}];Based on the native framework
If your project does not integrate the mPaaS framework, you must manually call the API to upload the startup instrumentation log when the app starts.
#import <MPMasAdapter/MPAnalysisHelper.h>
//record the time interval used for the app startup
NSTimeInterval time = CFAbsoluteTimeGetCurrent() - __start_timestamp;
[[MPAnalysisHelper sharedInstance] writeLogForStartupWithTime:time];Crash monitoring
Add crash instrumentation to collect crash-related data.
Based on the mPaaS framework
If you integrate the framework (your project contains the APMobileFramework library), the crash reporting module automatically captures crash logs and uploads them to the server. After you integrate the SDK, you only need to confirm that crash monitoring is enabled. To ensure that crash logs are reported promptly, call this interface in the main function.
Based on the native framework
If you do not integrate the mPaaS framework (your project does not contain the APMobileFramework library), you must enable crash monitoring at startup and report crash logs after startup.
Disaster recovery switch
By default, disaster recovery is triggered after four consecutive crashes. This process clears the files in the Documents folder to prevent crashes caused by dirty data. In versions 10.1.60 and later, you can manually call the following interface to enable or disable disaster recovery.
#import <MPMasAdapter/MPAnalysisHelper.h>
/**
* Enables or disables crash disaster recovery. This feature is enabled by default.
*/
+ (void)enableDisasterRecovery:(BOOL)enable;Only crash logs from an app running on a physical device are captured and uploaded to the log server. To debug crash monitoring, you must disconnect from Xcode and not use an emulator.
To ensure that the version in the crash log matches the product version, set the bundle version and product version to the same value in your project's
info.plistfile.
Stuttering and freeze monitoring
Add performance instrumentation to collect data about stuttering and freezes.
Based on the mPaaS framework
By default, stuttering and freeze monitoring is enabled for 10% of devices. You can use the following interface to set the enablement rate.
[MPAnalysisHelper setLagMonitorPercent: 100]; // Enable monitoring for 100% of devices. This must be set before you call startPerformanceMonitor.NoteStuttering and freeze monitoring is enabled only on a physical device and when not debugging in Xcode.
Call
[MPAnalysisHelper startPerformanceMonitor]at startup in the-(void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptionsmethod.
Based on the native framework
The SDK provides a performance monitoring interface. Call [PerformanceHelper performanceMonitor] in the - (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions method of AppDelegate.
Stuttering and freeze monitoring is enabled only on a physical device and when not debugging in Xcode.
#import "PerformanceHelper.h"
#import <MPAnalysis/MPAnalysisHelper.h>
static NSTimeInterval __start_timestamp = 0;
@implementation PerformanceHelper
+ (void)load
{
__start_timestamp = CFAbsoluteTimeGetCurrent();
}
+ (void)performanceMonitor
{
//start performance monitor
[MPAnalysisHelper setLagMonitorPercent: 100]; // Enable monitoring for 100% of devices. This must be set before you call startPerformanceMonitor.
[MPAnalysisHelper startPerformanceMonitor];
//record the time interval used for the app startup
NSTimeInterval time = CFAbsoluteTimeGetCurrent() - __start_timestamp;
[[MPAnalysisHelper sharedInstance] writeLogForStartupWithTime:time];
}
@endEnable Mini Program monitoring
After you integrate the Mini Program component, the component automatically reports performance monitoring data. No manual instrumentation is required.