When it comes to mobile apps, performance is critical to user experience. Usersexpect apps to have smooth scrolling and meaningful animations free ofstuttering or skipped frames, known as “jank.” How can we ensure our apps arefree of jank on a wide variety of devices?

There are two options: first, we could manually test the app on differentdevices. While that approach might work for a smaller app, it will become morecumbersome as an app grows in size. Alternatively, we can run an integrationtest that performs a specific task and record a performance timeline. Then, wecan examine the results to determine whether or not a specific section of ourapp needs to be improved.

In this recipe, we’ll learn how to write a test that records a performancetimeline while performing a specific task and saves a summary of the results toa local file.

Directions

  • Write a test that scrolls through a list of items
  • Record the performance of the app
  • Save the results to disk
  • Run the test
  • Review the results

1. Write a test that scrolls through a list of items

In this recipe, we’ll record the performance of an app as it scrolls through alist of items. In order to focus on performance profiling, this recipe buildsupon theScrolling in integration testsrecipe.

Please follow the instructions in that recipe to create an app, instrument theapp, and write a test to verify everything works as expected.

2. Record the performance of the app

Next, we need to record the performance of the app as it scrolls through thelist. To achieve this task, we can use thetraceActionmethod provided by theFlutterDriverclass.

This method runs the provided function and records aTimelinewith detailed information about the performance of the app. In this example, weprovide a function that scrolls through the list of items, ensuring a specificitem is displayed. When the function completes, the traceAction method returnsa Timeline.

  1. // Record a performance timeline as we scroll through the list of items
  2. final timeline = await driver.traceAction(() async {
  3. await driver.scrollUntilVisible(
  4. listFinder,
  5. itemFinder,
  6. dyScroll: -300.0,
  7. );
  8. expect(await driver.getText(itemFinder), 'Item 50');
  9. });

3. Save the results to disk

Now that we’ve captured a performance timeline, we need a way to review it!The Timeline object provides detailed information about all of the events thattook place, but it does not provide a convenient way to review the results.

Therefore, we can convert the Timeline into aTimelineSummary.The TimelineSummary can perform two tasks that make it easier to review theresults:

  • It can write a json document on disk that summarizes the data contained within the Timeline. This summary includes information about the number of skipped frames, slowest build times, and more.
  • It can save the complete Timeline as a json file on disk. This file can be opened with the Chrome browser’s tracing tools found at chrome://tracing.
  1. // Convert the Timeline into a TimelineSummary that's easier to read and
  2. // understand.
  3. final summary = new TimelineSummary.summarize(timeline);
  4. // Then, save the summary to disk
  5. summary.writeSummaryToFile('scrolling_summary', pretty: true);
  6. // Optionally, write the entire timeline to disk in a json format. This
  7. // file can be opened in the Chrome browser's tracing tools found by
  8. // navigating to chrome://tracing.
  9. summary.writeTimelineToFile('scrolling_timeline', pretty: true);

4. Run the test

After we’ve configured our test to capture a performance Timeline and save asummary of the results to disk, we can run the test with the following command:

  1. flutter drive --target=test_driver/app.dart

5. Review the results

After the test completes successfully, the build directory at the root ofthe project contains two files:

  • scrolling_summary.timeline_summary.json contains the summary. Open the file with any text editor to review the information contained within. With a more advanced setup, we could save a summary every time the test runs and create a graph of the results.
  • scrolling_timeline.timeline.json contains the complete timeline data. Open the file using the Chrome browser’s tracing tools found at chrome://tracing. The tracing tools provide a convenient interface for inspecting the timeline data in order to discover the source of a performance issue.

Summary Example

  1. {
  2. "average_frame_build_time_millis": 4.2592592592592595,
  3. "worst_frame_build_time_millis": 21.0,
  4. "missed_frame_build_budget_count": 2,
  5. "average_frame_rasterizer_time_millis": 5.518518518518518,
  6. "worst_frame_rasterizer_time_millis": 51.0,
  7. "missed_frame_rasterizer_budget_count": 10,
  8. "frame_count": 54,
  9. "frame_build_times": [
  10. 6874,
  11. 5019,
  12. 3638
  13. ],
  14. "frame_rasterizer_times": [
  15. 51955,
  16. 8468,
  17. 3129
  18. ]
  19. }

Complete example

  1. import 'package:flutter_driver/flutter_driver.dart';
  2. import 'package:test/test.dart';
  3. void main() {
  4. group('Scrollable App', () {
  5. FlutterDriver driver;
  6. setUpAll(() async {
  7. driver = await FlutterDriver.connect();
  8. });
  9. tearDownAll(() async {
  10. if (driver != null) {
  11. driver.close();
  12. }
  13. });
  14. test('verifies the list contains a specific item', () async {
  15. final listFinder = find.byValueKey('long_list');
  16. final itemFinder = find.byValueKey('item_50_text');
  17. // Record a performance profile as we scroll through the list of items
  18. final timeline = await driver.traceAction(() async {
  19. await driver.scrollUntilVisible(
  20. listFinder,
  21. itemFinder,
  22. dyScroll: -300.0,
  23. );
  24. expect(await driver.getText(itemFinder), 'Item 50');
  25. });
  26. // Convert the Timeline into a TimelineSummary that's easier to read and
  27. // understand.
  28. final summary = new TimelineSummary.summarize(timeline);
  29. // Then, save the summary to disk
  30. summary.writeSummaryToFile('scrolling_summary', pretty: true);
  31. // Optionally, write the entire timeline to disk in a json format. This
  32. // file can be opened in the Chrome browser's tracing tools found by
  33. // navigating to chrome://tracing.
  34. summary.writeTimelineToFile('scrolling_timeline', pretty: true);
  35. });
  36. });
  37. }