Skip to content

Commit

Permalink
test: Add very basic benchmark infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
luanpotter committed Jan 5, 2025
1 parent 8ca31a3 commit be9c7a7
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/benchmark.yml
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: "examples/"
ignore-tag: "no-benchmark"
is-flutter: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions examples/benchmark/main.dart
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();
}
96 changes: 96 additions & 0 deletions examples/benchmark/update_components_benchmark.dart
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();
for (var i = 0; i < _amountComponents; i++) {
_game.add(_BenchmarkComponent(i));
}

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]';
}
1 change: 1 addition & 0 deletions examples/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies:
trex_game: ^0.1.0

dev_dependencies:
benchmark_harness: ^2.3.1
flame_lint: ^1.2.1
test: any

Expand Down

0 comments on commit be9c7a7

Please sign in to comment.