all_elements_bench.dart 2.66 KB
Newer Older
1 2 3 4 5 6
// 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 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
7
import 'package:flutter/services.dart';
8
import 'package:flutter_test/flutter_test.dart' show collectAllElementsFrom;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

import '../common.dart';

const int _kNumIters = 10000;

Future<void> main() async {
  assert(false, "Don't run benchmarks in debug mode! Use 'flutter run --release'.");
  runApp(MaterialApp(
    home: Scaffold(
      body: GridView.count(
        crossAxisCount: 5,
        children: List<Widget>.generate(25, (int index) {
          return Center(
            child: Scaffold(
              appBar: AppBar(
                title: Text('App $index'),
                actions: const <Widget>[
                  Icon(Icons.help),
                  Icon(Icons.add),
                  Icon(Icons.ac_unit),
                ],
              ),
31 32
              body: const Column(
                children: <Widget>[
33 34 35 36 37 38 39 40 41 42 43 44 45
                  Text('Item 1'),
                  Text('Item 2'),
                  Text('Item 3'),
                  Text('Item 4'),
                ],
              ),
            ),
          );
        }),
      ),
    ),
  ));

46 47 48 49 50 51
  // Lists may not be scrolled into frame in landscape.
  SystemChrome.setPreferredOrientations(<DeviceOrientation>[
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

52 53
  // Wait for frame rendering to stabilize.
  for (int i = 0; i < 5; i++) {
54
    await SchedulerBinding.instance.endOfFrame;
55
  }
56 57 58

  final Stopwatch watch = Stopwatch();

59
  print('flutter_test allElements benchmark... (${WidgetsBinding.instance.rootElement})');
60
  // Make sure we get enough elements to process for consistent benchmark runs
61
  int elementCount = collectAllElementsFrom(WidgetsBinding.instance.rootElement!, skipOffstage: false).length;
62
  while (elementCount < 2458) {
63
    await Future<void>.delayed(Duration.zero);
64
    elementCount = collectAllElementsFrom(WidgetsBinding.instance.rootElement!, skipOffstage: false).length;
65 66 67 68 69 70
  }
  print('element count: $elementCount');

  watch.start();
  for (int i = 0; i < _kNumIters; i += 1) {
    final List<Element> allElements = collectAllElementsFrom(
71
      WidgetsBinding.instance.rootElement!,
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      skipOffstage: false,
    ).toList();
    allElements.clear();
  }
  watch.stop();

  final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
  printer.addResult(
    description: 'All elements iterate',
    value: watch.elapsedMicroseconds / _kNumIters,
    unit: 'µs per iteration',
    name: 'all_elements_iteration',
  );
  printer.printToStdout();
}