Skip to content

Commit e6f3022

Browse files
authored
Adds StandardMessageCodec benchmark (#101767)
1 parent fe5dadd commit e6f3022

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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';
6+
7+
import '../common.dart';
8+
9+
const int _kNumIterations = 100000;
10+
11+
void main() {
12+
assert(false,
13+
"Don't run benchmarks in checked mode! Use 'flutter run --release'.");
14+
final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
15+
16+
const StandardMessageCodec codec = StandardMessageCodec();
17+
final Stopwatch watch = Stopwatch();
18+
watch.start();
19+
for (int i = 0; i < _kNumIterations; i += 1) {
20+
codec.encodeMessage(null);
21+
}
22+
watch.stop();
23+
24+
printer.addResult(
25+
description: 'StandardMessageCodec null',
26+
value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
27+
unit: 'us per iteration',
28+
name: 'StandardMessageCodec_null',
29+
);
30+
31+
watch.reset();
32+
watch.start();
33+
for (int i = 0; i < _kNumIterations; i += 1) {
34+
codec.encodeMessage(12345);
35+
}
36+
watch.stop();
37+
38+
printer.addResult(
39+
description: 'StandardMessageCodec int',
40+
value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
41+
unit: 'us per iteration',
42+
name: 'StandardMessageCodec_int',
43+
);
44+
45+
watch.reset();
46+
47+
watch.start();
48+
for (int i = 0; i < _kNumIterations; i += 1) {
49+
codec.encodeMessage('This is a performance test.');
50+
}
51+
watch.stop();
52+
53+
printer.addResult(
54+
description: 'StandardMessageCodec string',
55+
value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
56+
unit: 'us per iteration',
57+
name: 'StandardMessageCodec_string',
58+
);
59+
60+
watch.reset();
61+
watch.start();
62+
for (int i = 0; i < _kNumIterations; i += 1) {
63+
codec.encodeMessage(<Object>[1234, 'This is a performance test.', 1.25, true]);
64+
}
65+
watch.stop();
66+
67+
printer.addResult(
68+
description: 'StandardMessageCodec heterogenous list',
69+
value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
70+
unit: 'us per iteration',
71+
name: 'StandardMessageCodec_heterogenous_list',
72+
);
73+
74+
watch.reset();
75+
watch.start();
76+
for (int i = 0; i < _kNumIterations; i += 1) {
77+
codec.encodeMessage(<String, Object>{
78+
'integer': 1234,
79+
'string': 'This is a performance test.',
80+
'float': 1.25,
81+
'boolean': true
82+
});
83+
}
84+
watch.stop();
85+
86+
printer.addResult(
87+
description: 'StandardMessageCodec heterogenous map',
88+
value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
89+
unit: 'us per iteration',
90+
name: 'StandardMessageCodec_heterogenous_map',
91+
);
92+
93+
watch.reset();
94+
95+
printer.printToStdout();
96+
}

dev/devicelab/lib/tasks/microbenchmarks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ TaskFunction createMicrobenchmarkTask() {
6161
...await _runMicrobench('lib/language/sync_star_semantics_bench.dart'),
6262
...await _runMicrobench('lib/foundation/all_elements_bench.dart'),
6363
...await _runMicrobench('lib/foundation/change_notifier_bench.dart'),
64+
...await _runMicrobench('lib/foundation/standard_message_codec_bench.dart'),
6465
...await _runMicrobench('lib/foundation/timeline_bench.dart'),
6566
};
6667

0 commit comments

Comments
 (0)