From 744a16938778766b8e31fc26d9085d1fd1138e40 Mon Sep 17 00:00:00 2001 From: Yegor Date: Fri, 10 Jul 2020 17:50:24 -0700 Subject: [PATCH] Add a benchmark that focuses on Paint object performance (#60939) * Add a benchmark the focuses on Paint object performance --- .../lib/src/web/bench_draw_rect.dart | 42 +++++++++++++++++-- .../macrobenchmarks/lib/web_benchmarks.dart | 3 +- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/dev/benchmarks/macrobenchmarks/lib/src/web/bench_draw_rect.dart b/dev/benchmarks/macrobenchmarks/lib/src/web/bench_draw_rect.dart index a6a2a9ad51..f9c8b7fbcd 100644 --- a/dev/benchmarks/macrobenchmarks/lib/src/web/bench_draw_rect.dart +++ b/dev/benchmarks/macrobenchmarks/lib/src/web/bench_draw_rect.dart @@ -10,9 +10,20 @@ import 'recorder.dart'; /// /// Measures the performance of the `drawRect` operation. class BenchDrawRect extends SceneBuilderRecorder { - BenchDrawRect() : super(name: benchmarkName); + /// A variant of the benchmark that uses the same [Paint] object for all rectangles. + /// + /// This variant focuses on the performance of the `drawRect` method itself. + BenchDrawRect.staticPaint() : benchmarkPaint = false, super(name: benchmarkName); + + /// A variant of the benchmark that creates a unique [Paint] for each rectangle. + /// + /// Does not cache the [Paint] objects across frames, but generates new + /// objects every time. This variant of the benchmark focuses on construction + /// and transfer of paint data to the renderer. + BenchDrawRect.variablePaint() : benchmarkPaint = true, super(name: variablePaintBenchmarkName); static const String benchmarkName = 'draw_rect'; + static const String variablePaintBenchmarkName = 'draw_rect_variable_paint'; /// Number of rows in the grid. static const int kRows = 25; @@ -20,17 +31,42 @@ class BenchDrawRect extends SceneBuilderRecorder { /// Number of columns in the grid. static const int kColumns = 40; + /// Whether each cell should gets its own unique [Paint] value. + /// + /// This is used to benchmark the efficiency of passing a large number of + /// paint objects to the rendering system. + final bool benchmarkPaint; + /// Counter used to offset the rendered rects to make them wobble. /// /// The wobbling is there so a human could visually verify that the benchmark /// is correctly pumping frames. double wobbleCounter = 0; + static final Paint _staticPaint = Paint()..color = const Color.fromARGB(255, 255, 0, 0); + + Paint makePaint(int row, int col) { + if (benchmarkPaint) { + final Paint paint = Paint(); + final double rowRatio = row / kRows; + paint.color = Color.fromARGB(255, (255 * rowRatio).floor(), (255 * col / kColumns).floor(), 255); + paint.filterQuality = FilterQuality.values[(FilterQuality.values.length * rowRatio).floor()]; + paint.strokeCap = StrokeCap.values[(StrokeCap.values.length * rowRatio).floor()]; + paint.strokeJoin = StrokeJoin.values[(StrokeJoin.values.length * rowRatio).floor()]; + paint.blendMode = BlendMode.values[(BlendMode.values.length * rowRatio).floor()]; + paint.style = PaintingStyle.values[(PaintingStyle.values.length * rowRatio).floor()]; + paint.strokeWidth = 1.0 + rowRatio; + paint.strokeMiterLimit = rowRatio; + return paint; + } else { + return _staticPaint; + } + } + @override void onDrawFrame(SceneBuilder sceneBuilder) { final PictureRecorder pictureRecorder = PictureRecorder(); final Canvas canvas = Canvas(pictureRecorder); - final Paint paint = Paint()..color = const Color.fromARGB(255, 255, 0, 0); final Size windowSize = window.physicalSize; final Size cellSize = Size( @@ -44,7 +80,7 @@ class BenchDrawRect extends SceneBuilderRecorder { for (int col = 0; col < kColumns; col++) { canvas.drawRect( Offset((wobbleCounter - 5).abs(), 0) & rectSize, - paint, + makePaint(row, col), ); canvas.translate(cellSize.width, 0); } diff --git a/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart b/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart index 4b672adcd9..9afaea681d 100644 --- a/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart +++ b/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart @@ -41,7 +41,8 @@ final Map benchmarks = { BenchCardInfiniteScroll.benchmarkName: () => BenchCardInfiniteScroll.forward(), BenchCardInfiniteScroll.benchmarkNameBackward: () => BenchCardInfiniteScroll.backward(), BenchClippedOutPictures.benchmarkName: () => BenchClippedOutPictures(), - BenchDrawRect.benchmarkName: () => BenchDrawRect(), + BenchDrawRect.benchmarkName: () => BenchDrawRect.staticPaint(), + BenchDrawRect.variablePaintBenchmarkName: () => BenchDrawRect.variablePaint(), BenchPathRecording.benchmarkName: () => BenchPathRecording(), BenchTextOutOfPictureBounds.benchmarkName: () => BenchTextOutOfPictureBounds(), BenchSimpleLazyTextScroll.benchmarkName: () => BenchSimpleLazyTextScroll(),