scroll_perf.dart 2.39 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:complex_layout/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_test/flutter_test.dart';

/// The speed, in pixels per second, that the drag gestures should end with.
const double speed = 1500.0;

/// The number of down drags and the number of up drags. The total number of
/// gestures is twice this number.
const int maxIterations = 4;

/// The time that is allowed between gestures for the fling effect to settle.
const Duration pauses = Duration(milliseconds: 500);

Future<void> main() async {
23 24
  final Completer<void> ready = Completer<void>();
  runApp(GestureDetector(
25 26 27 28 29
    onTap: () {
      debugPrint('Received tap.');
      ready.complete();
    },
    behavior: HitTestBehavior.opaque,
30
    child: const IgnorePointer(
31
      child: ComplexLayoutApp(),
32 33
    ),
  ));
34
  await SchedulerBinding.instance?.endOfFrame;
35

36 37 38
  /// Wait 50ms to allow the raster thread to actually put up the frame. (The
  /// endOfFrame future ends when we send the data to the engine, before
  /// the raster thread has had a chance to rasterize, etc.)
39
  await Future<void>.delayed(const Duration(milliseconds: 50));
40 41 42 43 44 45
  debugPrint('==== MEMORY BENCHMARK ==== READY ====');

  await ready.future; // waits for tap sent by devicelab task
  debugPrint('Continuing...');

  // remove onTap handler, enable pointer events for app
46
  runApp(GestureDetector(
47
    child: const IgnorePointer(
48
      ignoring: false,
49
      child: ComplexLayoutApp(),
50 51
    ),
  ));
52
  await SchedulerBinding.instance?.endOfFrame;
53

54
  final WidgetController controller = LiveWidgetController(WidgetsBinding.instance!);
55 56 57 58 59

  // Scroll down
  for (int iteration = 0; iteration < maxIterations; iteration += 1) {
    debugPrint('Scroll down... $iteration/$maxIterations');
    await controller.fling(find.byType(ListView), const Offset(0.0, -700.0), speed);
60
    await Future<void>.delayed(pauses);
61 62 63 64 65 66
  }

  // Scroll up
  for (int iteration = 0; iteration < maxIterations; iteration += 1) {
    debugPrint('Scroll up... $iteration/$maxIterations');
    await controller.fling(find.byType(ListView), const Offset(0.0, 300.0), speed);
67
    await Future<void>.delayed(pauses);
68 69 70 71
  }

  debugPrint('==== MEMORY BENCHMARK ==== DONE ====');
}