Skip to content

Commit 98120d1

Browse files
authored
Add a microbenchmark that measures performance of flutter compute. (flutter#91257)
* Add a microbenchmark that measures performance of flutter compute. This benchmark should show performance improvements when compute is switched to Isolate.exit * Fix analyzer warnings, increase warmup iterations * Fix license header
1 parent 35fe52c commit 98120d1

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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/foundation.dart';
6+
7+
import '../common.dart';
8+
9+
const int _kNumIterations = 10;
10+
const int _kNumWarmUp = 100;
11+
12+
class Data {
13+
Data(this.value);
14+
15+
Map<String, dynamic> toJson() => <String, dynamic>{ 'value': value };
16+
17+
final int value;
18+
}
19+
20+
List<Data> test(int length) {
21+
return List<Data>.generate(length,
22+
(int index) => Data(index * index));
23+
}
24+
25+
Future<void> main() async {
26+
assert(false, "Don't run benchmarks in checked mode! Use 'flutter run --release'.");
27+
28+
// Warm up lap
29+
for (int i = 0; i < _kNumWarmUp; i += 1) {
30+
await compute(test, 10);
31+
}
32+
33+
final Stopwatch watch = Stopwatch();
34+
watch.start();
35+
for (int i = 0; i < _kNumIterations; i += 1) {
36+
await compute(test, 1000000);
37+
}
38+
final int elapsedMicroseconds = watch.elapsedMicroseconds;
39+
40+
final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
41+
const double scale = 1000.0 / _kNumIterations;
42+
printer.addResult(
43+
description: 'compute',
44+
value: elapsedMicroseconds * scale,
45+
unit: 'ns per iteration',
46+
name: 'compute_iteration',
47+
);
48+
printer.printToStdout();
49+
}

dev/devicelab/lib/tasks/microbenchmarks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ TaskFunction createMicrobenchmarkTask() {
5454
...await _runMicrobench('lib/gestures/velocity_tracker_bench.dart'),
5555
...await _runMicrobench('lib/gestures/gesture_detector_bench.dart'),
5656
...await _runMicrobench('lib/stocks/animation_bench.dart'),
57+
...await _runMicrobench('lib/language/compute_bench.dart'),
5758
...await _runMicrobench('lib/language/sync_star_bench.dart'),
5859
...await _runMicrobench('lib/language/sync_star_semantics_bench.dart'),
5960
...await _runMicrobench('lib/foundation/all_elements_bench.dart'),

0 commit comments

Comments
 (0)