util.dart 2.03 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
// 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:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;

10 11
import 'package:macrobenchmarks/common.dart';

12 13 14
void macroPerfTest(
    String testName,
    String routeName,
15 16
    { Duration pageDelay,
      Duration duration = const Duration(seconds: 3),
17
      Duration timeout = const Duration(seconds: 30),
18
      Future<void> driverOps(FlutterDriver driver),
19
      Future<void> setupOps(FlutterDriver driver),
20
    }) {
21 22 23 24 25 26 27 28 29 30 31
  test(testName, () async {
    final FlutterDriver driver = await FlutterDriver.connect();

    // The slight initial delay avoids starting the timing during a
    // period of increased load on the device. Without this delay, the
    // benchmark has greater noise.
    // See: https://github.com/flutter/flutter/issues/19434
    await Future<void>.delayed(const Duration(milliseconds: 250));

    await driver.forceGC();

32 33
    final SerializableFinder scrollable = find.byValueKey(kScrollableName);
    expect(scrollable, isNotNull);
34 35
    final SerializableFinder button = find.byValueKey(routeName);
    expect(button, isNotNull);
36
    await driver.scrollUntilVisible(scrollable, button, dyScroll: -50.0);
37 38 39 40 41 42 43
    await driver.tap(button);

    if (pageDelay != null) {
      // Wait for the page to load
      await Future<void>.delayed(pageDelay);
    }

44 45 46 47
    if (setupOps != null) {
      await setupOps(driver);
    }

48
    final Timeline timeline = await driver.traceAction(() async {
49 50 51 52 53
      final Future<void> durationFuture = Future<void>.delayed(duration);
      if (driverOps != null) {
        await driverOps(driver);
      }
      await durationFuture;
54 55 56
    });

    driver.close();
57 58 59 60

    final TimelineSummary summary = TimelineSummary.summarize(timeline);
    await summary.writeSummaryToFile(testName, pretty: true);
    await summary.writeTimelineToFile(testName, pretty: true);
61
  }, timeout: Timeout(timeout));
62
}