Testing Flutter apps翻譯-效能分析
當我們說到移動開發,效能對使用者體驗是極其重要的。使用者希望應用程式的滾動是平滑的,動畫是有意義的,程式沒有卡頓和被跳過的幀,這被稱作“jank.” (什麼東西?但是原文是這個)。我們如何確保我們的應用程式在各種裝置上都不受任何影響?
這裡有兩條選擇:
- 我們可以在不同的裝置上手動測試應用程式。這種方法可能只適用於小型應用程式,隨著應用的變大這種方法將會變的非常麻煩。
- 我們可以執行整合測試去執行一個具體的任務並且記錄效能時間線。然後,我們需要檢視結果再去確定我們應用程式的特定部分程式碼是否需要被優化。
在本文裡,我們將會學習到當執行一個特定的任務的時候如何編寫記錄效能時間線的測試,並且將結果摘要記錄到本地檔案。
步驟:
- 編寫一個滾動列表的測試。
- 記錄應用程式的效能。
- 儲存結果到磁碟。
- 執行測試。
- 檢視儲存的結果。
1. 編寫一個滾動列表的測試。
在本文裡,我們將會記錄一個列表應用程式的效能。為了聚焦在效能分析上,本文程式碼基於Scrolling in integration tests 這篇文章。
請根據本文講解建立一個應用程式,一個可測試的應用程式,並且編寫測試程式碼去驗證一切是否按預期工作。
2. 記錄應用程式的效能。
下一步,我們需要記錄應用程式在滾動時候的效能。為了完成這個任務,我們可以使用
FlutterDriver
提供的
traceAction
方法。
此方法執行傳入的函式引數並記錄
Timeline
,其中包含有關應用程式效能的詳細資訊。在這個例子裡,我們提供了一個函式去滾動列表,確保一個特定的 item 被顯示。當這個函式完成以後,這個traceAction
方法返回一個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. 儲存結果到磁碟。
現在我們得到了一個性能分析時間線,我們需要一個方法去檢視它!這個Timeline
物件提供了發生過的所有事件的詳細資訊,但是沒有提供一個方便的方法去檢視結果。
所以,我們可以轉換Timeline
為
TimelineSummary
。這個TimelineSummary
可以執行兩個任務來讓檢視Timeline
結果變的簡單:
-
它可以在硬碟寫一個包含了
Timeline
彙總資料的json文件。這個彙總包括了跳過的幀的數量,最慢的build時間等資訊。 -
它能將已完成的
Timeline
儲存為json檔案儲存到硬盤裡。這個檔案可以用Chrome瀏覽器的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 chrome://tracing. summary.writeTimelineToFile('scrolling_timeline', pretty: true); 複製程式碼
4. 執行測試。
在我們配置我們的測試去捕獲Timeline
的效能分析結果並編寫將結果儲存到硬碟的程式碼以後,我們可以執行下面的命令來測試:
flutter drive --target=test_driver/app.dart 複製程式碼
5. 檢視儲存的結果。
在測試完成以後,專案根目錄的build
資料夾下面會生成兩個檔案:
-
scrolling_summary.timeline_summary.json
包含總結摘要。可以用任意文字編輯器開啟該檔案以檢視其中的資訊。通過更高階的設定,我們可以儲存每次測試執行的總結摘要並且可以將這些結果做成圖表。 -
scrolling_timeline.timeline.json
包含完成的timeline資料。 用Chrome瀏覽器的chrome://tracing 頁面中的跟蹤工具開啟該檔案。這個追蹤工具提供了一個方便的介面來分析timeline資料,該工具可以找到效能問題的源頭。
總結摘要檔案內容的示例:
{ "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 ] } 複製程式碼
完整程式碼:
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 chrome://tracing. summary.writeTimelineToFile('scrolling_timeline', pretty: true); }); }); } 複製程式碼