Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit eec8d9d

Browse files
authored
Add asset manifest parsing benchmark (#112836)
1 parent abca897 commit eec8d9d

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:convert';
6+
7+
import 'package:flutter/foundation.dart';
8+
import 'package:flutter/services.dart' show PlatformAssetBundle;
9+
import 'package:flutter/widgets.dart';
10+
11+
import '../common.dart';
12+
13+
const int _kNumIterations = 1000;
14+
15+
void main() async {
16+
assert(false, "Don't run benchmarks in debug mode! Use 'flutter run --release'.");
17+
18+
final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
19+
WidgetsFlutterBinding.ensureInitialized();
20+
final Stopwatch watch = Stopwatch();
21+
final PlatformAssetBundle bundle = PlatformAssetBundle();
22+
23+
final ByteData assetManifestBytes = await bundle.load('money_asset_manifest.json');
24+
watch.start();
25+
for (int i = 0; i < _kNumIterations; i++) {
26+
bundle.clear();
27+
final String json = utf8.decode(assetManifestBytes.buffer.asUint8List());
28+
// This is a test, so we don't need to worry about this rule.
29+
// ignore: invalid_use_of_visible_for_testing_member
30+
await AssetImage.manifestParser(json);
31+
}
32+
watch.stop();
33+
34+
printer.addResult(
35+
description: 'Load and Parse Large Asset Manifest',
36+
value: watch.elapsedMilliseconds.toDouble(),
37+
unit: 'ms',
38+
name: 'load_and_parse_large_asset_manifest',
39+
);
40+
41+
printer.printToStdout();
42+
}

dev/benchmarks/microbenchmarks/money_asset_manifest.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

dev/benchmarks/microbenchmarks/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ dependencies:
7070
flutter:
7171
uses-material-design: true
7272
assets:
73+
- money_asset_manifest.json
7374
- packages/flutter_gallery_assets/people/ali_landscape.png
7475
- packages/flutter_gallery_assets/monochrome/red-square-1024x1024.png
7576
- packages/flutter_gallery_assets/logos/flutter_white/logo.png

dev/devicelab/lib/tasks/microbenchmarks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ TaskFunction createMicrobenchmarkTask({bool enableImpeller = false}) {
5757
...await runMicrobench('lib/foundation/standard_message_codec_bench.dart'),
5858
...await runMicrobench('lib/foundation/standard_method_codec_bench.dart'),
5959
...await runMicrobench('lib/foundation/timeline_bench.dart'),
60+
...await runMicrobench('lib/foundation/decode_and_parse_asset_manifest.dart'),
6061
...await runMicrobench('lib/geometry/matrix_utils_transform_bench.dart'),
6162
...await runMicrobench('lib/geometry/rrect_contains_bench.dart'),
6263
...await runMicrobench('lib/gestures/gesture_detector_bench.dart'),

packages/flutter/lib/src/painting/image_resolution.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class AssetImage extends AssetBundleImageProvider {
284284
Completer<AssetBundleImageKey>? completer;
285285
Future<AssetBundleImageKey>? result;
286286

287-
chosenBundle.loadStructuredData<Map<String, List<String>>?>(_kAssetManifestFileName, _manifestParser).then<void>(
287+
chosenBundle.loadStructuredData<Map<String, List<String>>?>(_kAssetManifestFileName, manifestParser).then<void>(
288288
(Map<String, List<String>>? manifest) {
289289
final String chosenName = _chooseVariant(
290290
keyName,
@@ -328,7 +328,9 @@ class AssetImage extends AssetBundleImageProvider {
328328
return completer.future;
329329
}
330330

331-
static Future<Map<String, List<String>>?> _manifestParser(String? jsonData) {
331+
/// Parses the asset manifest string into a strongly-typed map.
332+
@visibleForTesting
333+
static Future<Map<String, List<String>>?> manifestParser(String? jsonData) {
332334
if (jsonData == null) {
333335
return SynchronousFuture<Map<String, List<String>>?>(null);
334336
}

0 commit comments

Comments
 (0)