Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add basic component rendering benchmark
Browse files Browse the repository at this point in the history
luanpotter committed Jan 9, 2025
1 parent f4a75d5 commit b79c1c4
Showing 2 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/flame/benchmark/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'render_components_benchmark.dart';
import 'update_components_benchmark.dart';

Future<void> main() async {
await RenderComponentsBenchmark.main();
await UpdateComponentsBenchmark.main();
}
72 changes: 72 additions & 0 deletions packages/flame/benchmark/render_components_benchmark.dart
Original file line number Diff line number Diff line change
@@ -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<void> main() async {
final r = Random(69420);
await RenderComponentsBenchmark(r).report();
}

@override
Future<void> 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<void> 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<void> onLoad() async {
if (random.nextDouble() <= level) {
await addAll(
List.generate(
random.nextInt(2) + 1,
(_) => _BenchmarkComponent(random: random, level: level / 2),
),
);
}
}
}

0 comments on commit b79c1c4

Please sign in to comment.