firebase_performance 0.7.1+5 firebase_performance: ^0.7.1+5 copied to clipboard
Flutter plugin for Google Performance Monitoring for Firebase, an app measurement solution that monitors traces and HTTP/S network requests on Android and iOS.
Google Performance Monitoring for Firebase #
A Flutter plugin to use the Google Performance Monitoring for Firebase API.
For Flutter plugins for other Firebase products, see README.md.
Usage #
To use this plugin, first connect to Firebase by following the instructions for Android / iOS / Web. Then add this plugin by following these instructions. See example/lib/main.dart
for details on the API usage.
You can confirm that Performance Monitoring results appear in the Firebase Performance Monitoring console. Results should appear within a few minutes.
⚠️ Note: First Paint and First Contentful Paint metrics of web page load trace will not be collected; automatic network request traces and screen traces will not always be collected for mobile apps.
Define a Custom Trace #
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.
final Trace myTrace = FirebasePerformance.instance.newTrace("test_trace");
await myTrace.start();
final Item item = cache.fetch("item");
if (item != null) {
await myTrace.incrementMetric("item_cache_hit", 1);
} else {
await myTrace.incrementMetric("item_cache_miss", 1);
}
await myTrace.stop();
Add monitoring for specific network requests (mobile only) #
Performance Monitoring collects network requests automatically. Although this includes most network requests for your app, some might not be reported. To include specific network requests in Performance Monitoring, add the following code to your app:
class _MetricHttpClient extends BaseClient {
_MetricHttpClient(this._inner);
final Client _inner;
@override
Future<StreamedResponse> send(BaseRequest request) async {
final HttpMetric metric = FirebasePerformance.instance
.newHttpMetric(request.url.toString(), HttpMethod.Get);
await metric.start();
StreamedResponse response;
try {
response = await _inner.send(request);
metric
..responsePayloadSize = response.contentLength
..responseContentType = response.headers['Content-Type']
..requestPayloadSize = request.contentLength
..httpResponseCode = response.statusCode;
} finally {
await metric.stop();
}
return response;
}
}
class _MyAppState extends State<MyApp> {
.
.
.
Future<void> testHttpMetric() async {
final _MetricHttpClient metricHttpClient = _MetricHttpClient(Client());
final Request request =
Request("SEND", Uri.parse("https://www.google.com"));
metricHttpClient.send(request);
}
.
.
.
}
Issues and feedback #
Please file FlutterFire specific issues, bugs, or feature requests in our issue tracker.
Plugin issues that are not specific to Flutterfire can be filed in the Flutter issue tracker.
Contribution #
To contribute a change to this plugin, please review our contribution guide and open a pull request.
Testing #
The unit test is in test
directory which you can run using flutter test
.
The integration test is in example/test_driver/firebase_performance_e2e.dart
which you can run on an emulator:
cd example
flutter drive --target=./test_driver/firebase_performance_e2e.dart
To test the web implementation, download and run ChromeDriver, and then run flutter_drive
:
flutter drive --target=./test_driver/firebase_performance_e2e.dart -d web-server --release --browser-name=chrome --web-port=8080