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

Commit d88212c

Browse files
authored
added microbenchmark for loading assets (#105982)
1 parent 7bad4eb commit d88212c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 'package:flutter/services.dart' show PlatformAssetBundle;
6+
import 'package:flutter/widgets.dart';
7+
8+
import '../common.dart';
9+
10+
const int _kBatchSize = 100;
11+
const int _kNumIterations = 100;
12+
13+
void main() async {
14+
assert(false,
15+
"Don't run benchmarks in debug mode! Use 'flutter run --release'.");
16+
final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
17+
WidgetsFlutterBinding.ensureInitialized();
18+
final Stopwatch watch = Stopwatch();
19+
final PlatformAssetBundle bundle = PlatformAssetBundle();
20+
21+
final List<double> values = <double>[];
22+
for (int j = 0; j < _kNumIterations; ++j) {
23+
double tally = 0;
24+
watch.reset();
25+
watch.start();
26+
for (int i = 0; i < _kBatchSize; i += 1) {
27+
// Note: We don't load images like this. PlatformAssetBundle is used for
28+
// other assets (like Rive animations). We are using an image because it's
29+
// conveniently sized and available for the test.
30+
tally += (await bundle.load('packages/flutter_gallery_assets/places/india_pondicherry_salt_farm.png')).lengthInBytes;
31+
}
32+
watch.stop();
33+
values.add(watch.elapsedMicroseconds.toDouble() / _kBatchSize);
34+
if (tally < 0.0) {
35+
print("This shouldn't happen.");
36+
}
37+
}
38+
39+
printer.addResultStatistics(
40+
description: 'PlatformAssetBundle.load 1MB',
41+
values: values,
42+
unit: 'us per iteration',
43+
name: 'PlatformAssetBundle_load_1MB',
44+
);
45+
46+
printer.printToStdout();
47+
}

dev/devicelab/lib/tasks/microbenchmarks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ TaskFunction createMicrobenchmarkTask() {
6262
...await runMicrobench('lib/foundation/all_elements_bench.dart'),
6363
...await runMicrobench('lib/foundation/clamp.dart'),
6464
...await runMicrobench('lib/foundation/change_notifier_bench.dart'),
65+
...await runMicrobench('lib/foundation/platform_asset_bundle.dart'),
6566
...await runMicrobench('lib/foundation/standard_method_codec_bench.dart'),
6667
...await runMicrobench('lib/foundation/standard_message_codec_bench.dart'),
6768
...await runMicrobench('lib/foundation/timeline_bench.dart'),

0 commit comments

Comments
 (0)