-
-
Notifications
You must be signed in to change notification settings - Fork 937
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add very basic benchmark infrastructure (#3431)
Add very basic benchmark infrastructure, using the `examples` app as a centralized benchmark playground and with first ever benchmark file just testing some components and updates (more to come). This also sets up the `dart-benchmark-action` to run, starting on this very PR!
- Loading branch information
1 parent
09fc0f2
commit 4af202f
Showing
4 changed files
with
122 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Benchmark | ||
|
||
on: [pull_request] | ||
|
||
jobs: | ||
benchmark: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: subosito/flutter-action@v2 | ||
- uses: bluefireteam/melos-action@v3 | ||
|
||
- uses: luanpotter/[email protected] | ||
with: | ||
paths: "packages/flame/" | ||
ignore-tag: "no-benchmark" | ||
is-flutter: true | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
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,5 @@ | ||
import 'update_components_benchmark.dart'; | ||
|
||
Future<void> main() async { | ||
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,96 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:benchmark_harness/benchmark_harness.dart'; | ||
import 'package:flame/components.dart'; | ||
import 'package:flame/game.dart'; | ||
|
||
const _amountComponents = 20; | ||
const _amountTicks = 1000; | ||
const _amountInputs = 100; | ||
|
||
class UpdateComponentsBenchmark extends AsyncBenchmarkBase { | ||
final Random random; | ||
|
||
late final FlameGame _game; | ||
late final List<_BenchmarkComponent> _components; | ||
late final List<double> _dts; | ||
late final Set<int> _inputTicks; | ||
|
||
UpdateComponentsBenchmark(this.random) | ||
: super('Updating Components Benchmark'); | ||
|
||
static Future<void> main() async { | ||
final r = Random(69420); | ||
await UpdateComponentsBenchmark(r).report(); | ||
} | ||
|
||
@override | ||
Future<void> setup() async { | ||
_game = FlameGame(); | ||
await _game.addAll( | ||
List.generate(_amountComponents, _BenchmarkComponent.new), | ||
); | ||
|
||
await _game.ready(); | ||
|
||
_components = | ||
_game.children.whereType<_BenchmarkComponent>().toList(growable: false); | ||
|
||
_dts = List.generate(_amountTicks, (_) => random.nextDouble()); | ||
_inputTicks = List.generate( | ||
_amountInputs, | ||
(_) => random.nextInt(_amountTicks), | ||
).toSet(); | ||
} | ||
|
||
@override | ||
Future<void> exercise() async { | ||
for (final (idx, dt) in _dts.indexed) { | ||
if (_inputTicks.contains(idx)) { | ||
_components[random.nextInt(_amountComponents)].input( | ||
xDirection: random.nextInt(3) - 1, | ||
doJump: random.nextBool(), | ||
); | ||
} | ||
_game.update(dt); | ||
} | ||
} | ||
} | ||
|
||
class _BenchmarkComponent extends PositionComponent { | ||
static const _groundY = -20.0; | ||
|
||
final int id; | ||
final Vector2 velocity = Vector2.zero(); | ||
|
||
_BenchmarkComponent(this.id); | ||
|
||
void input({ | ||
required int xDirection, | ||
required bool doJump, | ||
}) { | ||
// move x | ||
velocity.x = xDirection * 100; | ||
|
||
// jump | ||
if (position.y == _groundY) { | ||
velocity.y -= 100; | ||
} | ||
} | ||
|
||
@override | ||
void update(double dt) { | ||
super.update(dt); | ||
|
||
position.add(velocity * dt); | ||
velocity.add(Vector2(0, 10 * dt)); | ||
|
||
if (position.y > _groundY) { | ||
position.y = _groundY; | ||
velocity.setValues(0, 0); | ||
} | ||
} | ||
|
||
@override | ||
String toString() => '[Component $id]'; | ||
} |
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