From 29306b08960d8837fe7dd3a3e1af914c0a8d131c Mon Sep 17 00:00:00 2001 From: Harry Terkelsen Date: Thu, 20 Feb 2020 11:16:58 -0800 Subject: [PATCH] [Web] Add Material Card Infinite Scroll benchmark (#51005) * [Web] Add Material Card Infinite Scroll benchmark Adds a benchmark that makes an infinite list of Material cards and scrolls it. This benchmark exercises more heavyweight rendering like shadows and clipping and paths. * Fix analyzer warnings. Respond to comments --- .../src/web/bench_card_infinite_scroll.dart | 77 +++++++++++++++++++ .../macrobenchmarks/lib/web_benchmarks.dart | 2 + 2 files changed, 79 insertions(+) create mode 100644 dev/benchmarks/macrobenchmarks/lib/src/web/bench_card_infinite_scroll.dart diff --git a/dev/benchmarks/macrobenchmarks/lib/src/web/bench_card_infinite_scroll.dart b/dev/benchmarks/macrobenchmarks/lib/src/web/bench_card_infinite_scroll.dart new file mode 100644 index 0000000000..e36026808d --- /dev/null +++ b/dev/benchmarks/macrobenchmarks/lib/src/web/bench_card_infinite_scroll.dart @@ -0,0 +1,77 @@ +// 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 'dart:async'; + +import 'package:flutter/material.dart'; + +import 'recorder.dart'; +import 'test_data.dart'; + +/// Creates an infinite list of Material cards and scrolls it. +class BenchCardInfiniteScroll extends WidgetRecorder { + BenchCardInfiniteScroll() : super(name: benchmarkName); + + static const String benchmarkName = 'bench_card_infinite_scroll'; + + @override + Widget createWidget() => const MaterialApp( + title: 'Infinite Card Scroll Benchmark', + home: _InfiniteScrollCards(), + ); +} + +class _InfiniteScrollCards extends StatefulWidget { + const _InfiniteScrollCards({Key key}) : super(key: key); + + @override + State<_InfiniteScrollCards> createState() => _InfiniteScrollCardsState(); +} + +class _InfiniteScrollCardsState extends State<_InfiniteScrollCards> { + ScrollController scrollController; + + double offset; + static const double distance = 1000; + static const Duration stepDuration = Duration(seconds: 1); + + @override + void initState() { + super.initState(); + + scrollController = ScrollController(); + offset = 0; + + // Without the timer the animation doesn't begin. + Timer.run(() async { + while (true) { + await scrollController.animateTo( + offset + distance, + curve: Curves.linear, + duration: stepDuration, + ); + offset += distance; + } + }); + } + + @override + Widget build(BuildContext context) { + return ListView.builder( + controller: scrollController, + itemBuilder: (BuildContext context, int index) { + return SizedBox( + height: 100.0, + child: Card( + elevation: 16.0, + child: Text( + lipsum[index % lipsum.length], + textAlign: TextAlign.center, + ), + ), + ); + }, + ); + } +} diff --git a/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart b/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart index 1eba887828..f6af3a5f92 100644 --- a/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart +++ b/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart @@ -8,6 +8,7 @@ import 'dart:html' as html; import 'package:macrobenchmarks/src/web/bench_text_out_of_picture_bounds.dart'; +import 'src/web/bench_card_infinite_scroll.dart'; import 'src/web/bench_draw_rect.dart'; import 'src/web/bench_simple_lazy_text_scroll.dart'; import 'src/web/bench_text_out_of_picture_bounds.dart'; @@ -16,6 +17,7 @@ import 'src/web/recorder.dart'; typedef RecorderFactory = Recorder Function(); final Map benchmarks = { + BenchCardInfiniteScroll.benchmarkName: () => BenchCardInfiniteScroll(), BenchDrawRect.benchmarkName: () => BenchDrawRect(), BenchTextOutOfPictureBounds.benchmarkName: () => BenchTextOutOfPictureBounds(), BenchSimpleLazyTextScroll.benchmarkName: () => BenchSimpleLazyTextScroll(),