- Install
- Set up an editor
- Test drive
- Write your first Flutter app, part 1
- Learn more
- Flutter for Android developers
- Flutter for iOS developers
- Flutter for React Native developers
- Flutter for web developers
- Flutter for Xamarin.Forms developers
- Introduction to declarative UI
- Cookbook
- Codelabs
- Tutorials
- User interface
- Introduction to widgets
- Layouts in Flutter
- Layout tutorial
- Dealing with box constraints
- Adding interactivity to your Flutter app
- Adding assets and images
- Navigation & routing
- Introduction to animations
- Animations overview
- Animations tutorial
- Hero Animations
- Staggered Animations
- Advanced UI
- Slivers
- Taps, drags, and other gestures
- Widget catalog
- Data & backend
- State management
- State management
- Start thinking declaratively
- Differentiate between ephemeral state and app state
- Simple app state management
- List of state management approaches
- JSON and serialization
- Firebase
- Accessibility & internationalization
- Accessibility
- Internationalizing Flutter apps
- Platform integration
- Writing custom platform-specific code
- Packages & plugins
- Using packages
- Developing packages & plugins
- Background processes
- Tools & techniques
- Android Studio / IntelliJ
- Visual Studio Code
- Upgrading Flutter
- Hot reload
- Code formatting
- Debugging Flutter apps
- Using OEM debuggers
- Flutter's build modes
- Testing Flutter apps
- Performance best practices
- Flutter performance profiling
- Creating flavors for Flutter
- Preparing an Android App for Release
- Preparing an iOS App for Release
- Continuous Delivery using fastlane with Flutter
- Bootstrap into Dart
- Inside Flutter
- Platform specific behaviors and adaptations
- Technical Overview
- Technical videos
- FAQ
- Flutter widget index
- Install
- Windows install
- MacOS install
- Linux install
- Set up an editor
- Write your first Flutter app, part 1
- Learn more
- Cupertino (iOS-style) widgets
- Layout widgets
- Animation and motion widgets
- Retrieve the value of a text field
- Basic widgets
- Material Components widgets
- Animate the properties of a Container
- Fade a Widget in and out
- Add a Drawer to a screen
- Displaying SnackBars
- Exporting fonts from a package
- Updating the UI based on orientation
- Using Themes to share colors and font styles
- Using custom fonts
- Working with Tabs
- Building a form with validation
- Create and style a text field
- Focus on a Text Field
- Handling changes to a text field
- Retrieve the value of a text field
- Adding Material Touch Ripples
- Handling Taps
- Implement Swipe to Dismiss
- Display images from the internet
- Fade in images with a placeholder
- Working with cached images
- Basic List
- Create a horizontal list
- Creating a Grid List
- Creating lists with different types of items
- Place a floating app bar above a list
- Working with long lists
- Report errors to a service
- Animating a Widget across screens
- Navigate to a new screen and back
- Navigate with named routes
- Pass arguments to a named route
- Return data from a screen
- Send data to a new screen
- Fetch data from the internet
- Making authenticated requests
- Parsing JSON in the background
- Working with WebSockets
- Persist data with SQLite
- Reading and Writing Files
- Storing key-value data on disk
- Play and pause a video
- Take a picture using the Camera
- An introduction to integration testing
- Performance profiling
- Scrolling
- An introduction to unit testing
- Mock dependencies using Mockito
- An introduction to widget testing
- Finding widgets
- Tapping, dragging and entering text
- Development
- Introduction to widgets
- Layout tutorial
- Dealing with box constraints
- Adding interactivity to your Flutter app
- Adding assets and images
- Navigation & routing
- Navigate to a new screen and back
- Send data to a new screen
- Return data from a screen
- Navigate with named routes
- Animating a Widget across screens
- AnimatedList
- Sample App Catalog
- Animations overview
- Animations tutorial
- Staggered Animations
- Slivers
- Taps, drags, and other gestures
- Accessibility widgets
- Assets, images, and icon widgets
- Async widgets
- Input widgets
- Interaction model widgets
- Painting and effect widgets
- Scrolling widgets
- Styling widgets
- Text widgets
- State management
- Start thinking declaratively
- Differentiate between ephemeral state and app state
- Simple app state management
- List of state management approaches
- JSON and serialization
- Accessibility
- Internationalizing Flutter apps
- Writing custom platform-specific code
- Using packages
- Fetch data from the internet
- Developing packages & plugins
- Background processes
- Android Studio / IntelliJ
- Set up an editor
- Flutter inspector
- Creating Useful Bug Reports
- Visual Studio Code
- Set up an editor
- Upgrading Flutter
- Hot reload
- Code formatting
Performance profiling
When it comes to mobile apps, performance is critical to user experience. Users expect apps to have smooth scrolling and meaningful animations free of stuttering or skipped frames, known as “jank.” How can we ensure our apps are free of jank on a wide variety of devices?
There are two options: first, we could manually test the app on different devices. While that approach might work for a smaller app, it will become more cumbersome as an app grows in size. Alternatively, we can run an integration test that performs a specific task and record a performance timeline. Then, we can examine the results to determine whether or not a specific section of our app needs to be improved.
In this recipe, we’ll learn how to write a test that records a performance timeline while performing a specific task and saves a summary of the results to a 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 a list of items. In order to focus on performance profiling, this recipe builds upon the Scrolling in integration tests recipe.
Please follow the instructions in that recipe to create an app, instrument the app, 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 the list. To achieve this task, we can use the traceAction
method provided by the FlutterDriver
class.
This method runs the provided function and records a Timeline
with detailed information about the performance of the app. In this example, we provide a function that scrolls through the list of items, ensuring a specific item is displayed. When the function completes, the traceAction
method returns a Timeline
.
// Record a performance timeline as we scroll through the list of items
final timeline = await driver.traceAction(() async {
await driver.scrollUntilVisible(
listFinder,
itemFinder,
dyScroll: -300.0,
);
expect(await driver.getText(itemFinder), 'Item 50');
});
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 that took place, but it does not provide a convenient way to review the results.
Therefore, we can convert the Timeline
into a TimelineSummary
. The TimelineSummary
can perform two tasks that make it easier to review the results:
- 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 https://flutter.axuer.com/docs/cookbook/testing/integration/chrome://tracing.
// Convert the Timeline into a TimelineSummary that's easier to read and
// understand.
final summary = new TimelineSummary.summarize(timeline);
// Then, save the summary to disk
summary.writeSummaryToFile('scrolling_summary', pretty: true);
// Optionally, write the entire timeline to disk in a json format. This
// file can be opened in the Chrome browser's tracing tools found by
// navigating to https://flutter.axuer.com/docs/cookbook/testing/integration/chrome://tracing.
summary.writeTimelineToFile('scrolling_timeline', pretty: true);
4. Run the test
After we’ve configured our test to capture a performance Timeline
and save a summary of the results to disk, we can run the test with the following command:
flutter drive --target=test_driver/app.dart
5. Review the results
After the test completes successfully, the build
directory at the root of the 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 https://flutter.axuer.com/docs/cookbook/testing/integration/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
{
"average_frame_build_time_millis": 4.2592592592592595,
"worst_frame_build_time_millis": 21.0,
"missed_frame_build_budget_count": 2,
"average_frame_rasterizer_time_millis": 5.518518518518518,
"worst_frame_rasterizer_time_millis": 51.0,
"missed_frame_rasterizer_budget_count": 10,
"frame_count": 54,
"frame_build_times": [
6874,
5019,
3638
],
"frame_rasterizer_times": [
51955,
8468,
3129
]
}
Complete example
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';
void main() {
group('Scrollable App', () {
FlutterDriver driver;
setUpAll(() async {
driver = await FlutterDriver.connect();
});
tearDownAll(() async {
if (driver != null) {
driver.close();
}
});
test('verifies the list contains a specific item', () async {
final listFinder = find.byValueKey('long_list');
final itemFinder = find.byValueKey('item_50_text');
// Record a performance profile as we scroll through the list of items
final timeline = await driver.traceAction(() async {
await driver.scrollUntilVisible(
listFinder,
itemFinder,
dyScroll: -300.0,
);
expect(await driver.getText(itemFinder), 'Item 50');
});
// Convert the Timeline into a TimelineSummary that's easier to read and
// understand.
final summary = new TimelineSummary.summarize(timeline);
// Then, save the summary to disk
summary.writeSummaryToFile('scrolling_summary', pretty: true);
// Optionally, write the entire timeline to disk in a json format. This
// file can be opened in the Chrome browser's tracing tools found by
// navigating to https://flutter.axuer.com/docs/cookbook/testing/integration/chrome://tracing.
summary.writeTimelineToFile('scrolling_timeline', pretty: true);
});
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论