velocity_tracker_bench.dart 1.72 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/gestures.dart';

7
import '../common.dart';
8
import 'data/velocity_tracker_data.dart';
9

10 11
const int _kNumIters = 10000;

12
class TrackerBenchmark {
13
  TrackerBenchmark({required this.name, required this.tracker });
14 15 16 17 18

  final VelocityTracker tracker;
  final String name;
}

19
void main() {
20
  assert(false, "Don't run benchmarks in checked mode! Use 'flutter run --release'.");
21
  final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
22
  final List<TrackerBenchmark> benchmarks = <TrackerBenchmark>[
23
    TrackerBenchmark(name: 'velocity_tracker_iteration', tracker: VelocityTracker.withKind(PointerDeviceKind.touch)),
24
    TrackerBenchmark(name: 'velocity_tracker_iteration_ios_fling', tracker: IOSScrollViewFlingVelocityTracker(PointerDeviceKind.touch)),
25
  ];
26
  final Stopwatch watch = Stopwatch();
27

28 29 30
  for (final TrackerBenchmark benchmark in benchmarks) {
    print('${benchmark.name} benchmark...');
    final VelocityTracker tracker = benchmark.tracker;
31 32 33 34 35 36 37 38 39
    watch.reset();
    watch.start();
    for (int i = 0; i < _kNumIters; i += 1) {
      for (final PointerEvent event in velocityEventData) {
        if (event is PointerDownEvent || event is PointerMoveEvent)
          tracker.addPosition(event.timeStamp, event.position);
        if (event is PointerUpEvent)
          tracker.getVelocity();
      }
40
    }
41 42
    watch.stop();
    printer.addResult(
43
      description: 'Velocity tracker: ${tracker.runtimeType}',
44 45
      value: watch.elapsedMicroseconds / _kNumIters,
      unit: 'µs per iteration',
46
      name: benchmark.name,
47
    );
48
  }
49 50

  printer.printToStdout();
51
}