From b79c1c4336bfeda6c83a3902e8af68c3f9174d4c Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Wed, 8 Jan 2025 19:14:59 -0500 Subject: [PATCH] test: Add basic component rendering benchmark --- packages/flame/benchmark/main.dart | 2 + .../render_components_benchmark.dart | 72 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 packages/flame/benchmark/render_components_benchmark.dart diff --git a/packages/flame/benchmark/main.dart b/packages/flame/benchmark/main.dart index cc6d8f98da8..61eb9b073e1 100644 --- a/packages/flame/benchmark/main.dart +++ b/packages/flame/benchmark/main.dart @@ -1,5 +1,7 @@ +import 'render_components_benchmark.dart'; import 'update_components_benchmark.dart'; Future main() async { + await RenderComponentsBenchmark.main(); await UpdateComponentsBenchmark.main(); } diff --git a/packages/flame/benchmark/render_components_benchmark.dart b/packages/flame/benchmark/render_components_benchmark.dart new file mode 100644 index 00000000000..8f8bb172f00 --- /dev/null +++ b/packages/flame/benchmark/render_components_benchmark.dart @@ -0,0 +1,72 @@ +import 'dart:async'; +import 'dart:math'; +import 'dart:ui'; + +import 'package:benchmark_harness/benchmark_harness.dart'; +import 'package:canvas_test/canvas_test.dart'; +import 'package:flame/components.dart'; +import 'package:flame/game.dart'; + +const _amountComponents = 10; +const _amountTicks = 1000; + +class RenderComponentsBenchmark extends AsyncBenchmarkBase { + final Random random; + + late final Canvas _canvas; + late final FlameGame _game; + + RenderComponentsBenchmark(this.random) + : super('Render Components Benchmark'); + + static Future main() async { + final r = Random(69420); + await RenderComponentsBenchmark(r).report(); + } + + @override + Future setup() async { + _canvas = MockCanvas(); + + _game = FlameGame(); + _game.onGameResize(Vector2.all(100.0)); + + await _game.addAll( + List.generate( + _amountComponents, + (_) => _BenchmarkComponent(random: random, level: 1), + ), + ); + + await _game.ready(); + } + + @override + Future exercise() async { + for (var i = 0; i < _amountTicks; i++) { + _game.render(_canvas); + } + } +} + +class _BenchmarkComponent extends PositionComponent { + final Random random; + final double level; + + _BenchmarkComponent({ + required this.random, + required this.level, + }); + + @override + Future onLoad() async { + if (random.nextDouble() <= level) { + await addAll( + List.generate( + random.nextInt(2) + 1, + (_) => _BenchmarkComponent(random: random, level: level / 2), + ), + ); + } + } +}