Konsola git

Get started with Performance Monitoring for iOS

Before you begin

If you haven't already, add Firebase to your iOS project.

Step 1: Add Performance Monitoring to your app

  1. Open the podfile you created for the project (or run pod init to create one), then add the following line inside the target section:

    pod 'Firebase/Performance'
  2. In the directory of your podfile, run the following command to install the Performance Monitoring SDK:

    pod install
  3. Open the .xcworkspace file using Xcode.

  4. Import the Firebase module in your UIApplicationDelegate:

    Swift

    import Firebase

    Objective-C

    @import Firebase;
  5. Configure a FirebaseApp shared instance, typically in your app's application:didFinishLaunchingWithOptions: method:

    Swift

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    Objective-C

    // Use Firebase library to configure APIs
    [FIRApp configure];
  6. Recompile your app.

Firebase is now monitoring automatic traces and HTTP/S network requests.

Step 2: Check the Firebase console for Performance Monitoring results

  1. Run your app in the Xcode simulator (Product > Run).

  2. Confirm that Performance Monitoring results display in the Firebase console.

    Results usually display within 12 hours.

Step 3: (Optional) Add custom traces and custom metrics

A custom trace is a report of performance data associated with some of the code in your app. To learn more about custom traces, see the Performance Monitoring overview.

You can have multiple custom traces in your app, and it's possible to have more than one custom trace running at a time. Each custom trace can have one or more metrics to count performance-related events in your app, and those metrics are associated with the traces that create them.

Note that names for custom traces and metrics must meet the following requirements: no leading or trailing whitespace, no leading underscore (_) character, and max length is 32 characters.

  1. Add the Performance Monitoring module to your header:

    Swift

    import FirebasePerformance
    

    Objective-C

    @import FirebasePerformance;
    
  2. To start and stop a custom trace, wrap the code that you want to trace with lines of code similar to the following:

    Swift

    let trace = Performance.startTrace(name: "customTraceName")
    
    // code that you want to trace
    
    trace.stop()
    

    Objective-C

    FIRTrace *trace = [FIRPerformance startTraceWithName:@"customTraceName"];
    
    // code that you want to trace
    
    [trace stop];
    
  3. To add a custom metric, add a line of code similar to the following each time that the event occurs. For example, this custom metric counts performance-related events that occur in your app, such as cache hits or retries.

    Swift

    let trace = Performance.startTrace(name: "customTraceName")
    
    trace.incrementMetric(named:"eventName", by: 1)
    // code that you want to trace (and log custom metrics)
    
    trace.stop()
    

    Objective-C

    FIRTrace *trace = [FIRPerformance startTraceWithName:@"customTraceName"];
    
    [trace incrementMetric:@"eventName" by:1];
    // code that you want to trace (and log custom metrics)
    
    [trace stop];
    

Step 4: (Optional) Add monitoring for specific network requests

Performance Monitoring collects network requests automatically. Although this includes most network requests for your app, some might not be reported.

To include custom network requests in Performance Monitoring, add the following code to your app:

Swift

guard let metric = HTTPMetric(url: "https://www.google.com", httpMethod: .get) else { return }

metric.start()
guard let url = URL(string: "https://www.google.com") else { return }
let request: URLRequest = URLRequest(url:url)
let session = URLSession(configuration: .default)
let dataTask = session.dataTask(with: request) { (urlData, response, error) in
        if let httpResponse = response as? HTTPURLResponse {
         metric.responseCode = httpResponse.statusCode
        }
        metric.stop()
}
dataTask.resume()

Objective-C

@property (nonatomic) FIRHTTPMetric *metric;

- (void)beginManualNetworkInstrumentation {
  self.metric =
      [[FIRHttpMetric alloc] initWithURL:[NSURL URLWithString:@"https://www.google.com"]
                              HTTPMethod:FIRHTTPMethodGET];

  [self.metric start];

  NSURLRequest *request =
      [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com"]];
  NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                                delegate:self];
  [connection resume];
}

- (void)connection:(NSURLConnection *)connection
    didReceiveResponse:(NSURLResponse *) response {
  NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response
  self.metric.responseCode = httpResponse.statusCode;
  [self.metric stop];
}

The HTTP/S network requests that you specifically capture in this way appear in the Firebase console along with the network requests that Performance Monitoring captures automatically.

Step 5: Deploy your app then review results

After you've validated Performance Monitoring using the Xcode simulator and one or more test devices, you can deploy the updated version of your app to your users.

You can monitor performance data in the Firebase console.

Known issues

  • Performance Monitoring has known compatibility issues with GTMSQLite. We recommend not using Performance Monitoring with apps that use GTMSQLite.
  • Method swizzling after calling FirebaseApp.configure() might interfere with the Performance Monitoring SDK.
  • Known issues with the iOS 8.0-8.2 Simulator prevent Performance Monitoring from capturing performance events. These issues are fixed in the iOS 8.3 Simulator and later versions.
  • Connections established using NSURLSession's backgroundSessionConfiguration will exhibit longer than expected connection times. These connections are executed out-of-process and the timings reflect in-process callback events.

Next steps