Add asset manifest parsing benchmark (#112836)

This commit is contained in:
Andrew Kolos 2022-10-07 06:39:35 -07:00 committed by GitHub
parent abca8976e6
commit eec8d9d9cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 2 deletions

View File

@ -0,0 +1,42 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' show PlatformAssetBundle;
import 'package:flutter/widgets.dart';
import '../common.dart';
const int _kNumIterations = 1000;
void main() async {
assert(false, "Don't run benchmarks in debug mode! Use 'flutter run --release'.");
final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
WidgetsFlutterBinding.ensureInitialized();
final Stopwatch watch = Stopwatch();
final PlatformAssetBundle bundle = PlatformAssetBundle();
final ByteData assetManifestBytes = await bundle.load('money_asset_manifest.json');
watch.start();
for (int i = 0; i < _kNumIterations; i++) {
bundle.clear();
final String json = utf8.decode(assetManifestBytes.buffer.asUint8List());
// This is a test, so we don't need to worry about this rule.
// ignore: invalid_use_of_visible_for_testing_member
await AssetImage.manifestParser(json);
}
watch.stop();
printer.addResult(
description: 'Load and Parse Large Asset Manifest',
value: watch.elapsedMilliseconds.toDouble(),
unit: 'ms',
name: 'load_and_parse_large_asset_manifest',
);
printer.printToStdout();
}

File diff suppressed because one or more lines are too long

View File

@ -70,6 +70,7 @@ dependencies:
flutter:
uses-material-design: true
assets:
- money_asset_manifest.json
- packages/flutter_gallery_assets/people/ali_landscape.png
- packages/flutter_gallery_assets/monochrome/red-square-1024x1024.png
- packages/flutter_gallery_assets/logos/flutter_white/logo.png

View File

@ -57,6 +57,7 @@ TaskFunction createMicrobenchmarkTask({bool enableImpeller = false}) {
...await runMicrobench('lib/foundation/standard_message_codec_bench.dart'),
...await runMicrobench('lib/foundation/standard_method_codec_bench.dart'),
...await runMicrobench('lib/foundation/timeline_bench.dart'),
...await runMicrobench('lib/foundation/decode_and_parse_asset_manifest.dart'),
...await runMicrobench('lib/geometry/matrix_utils_transform_bench.dart'),
...await runMicrobench('lib/geometry/rrect_contains_bench.dart'),
...await runMicrobench('lib/gestures/gesture_detector_bench.dart'),

View File

@ -284,7 +284,7 @@ class AssetImage extends AssetBundleImageProvider {
Completer<AssetBundleImageKey>? completer;
Future<AssetBundleImageKey>? result;
chosenBundle.loadStructuredData<Map<String, List<String>>?>(_kAssetManifestFileName, _manifestParser).then<void>(
chosenBundle.loadStructuredData<Map<String, List<String>>?>(_kAssetManifestFileName, manifestParser).then<void>(
(Map<String, List<String>>? manifest) {
final String chosenName = _chooseVariant(
keyName,
@ -328,7 +328,9 @@ class AssetImage extends AssetBundleImageProvider {
return completer.future;
}
static Future<Map<String, List<String>>?> _manifestParser(String? jsonData) {
/// Parses the asset manifest string into a strongly-typed map.
@visibleForTesting
static Future<Map<String, List<String>>?> manifestParser(String? jsonData) {
if (jsonData == null) {
return SynchronousFuture<Map<String, List<String>>?>(null);
}