-
-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
test: Add basic component rendering benchmark
1 parent
f4a75d5
commit b79c1c4
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
); | ||
} | ||
} | ||
} |