Skip to content

Commit

Permalink
chore: add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed May 26, 2024
1 parent 725b76c commit 21590cf
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
62 changes: 62 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Equatable Benchmarks

Benchmarks used to measure the performance of equality comparisons using `package:equatable`.

## Quick Start

1. Install dependencies
`dart pub get`
1. Run the benchmarks
`dart run main.dart`

## Results

```
EmptyEquatable
total runs: 2 076 295
total time: 2.0000 s
average run: 0 μs
runs/second: Infinity
units: 100
units/second: Infinity
time per unit: 0.0000 μs
PrimitiveEquatable
total runs: 810 588
total time: 2.0000 s
average run: 2 μs
runs/second: 500 000
units: 100
units/second: 50 000 000
time per unit: 0.0200 μs
CollectionEquatable (small)
total runs: 443 978
total time: 2.0000 s
average run: 4 μs
runs/second: 250 000
units: 100
units/second: 25 000 000
time per unit: 0.0400 μs
CollectionEquatable (medium)
total runs: 442 368
total time: 2.0000 s
average run: 4 μs
runs/second: 250 000
units: 100
units/second: 25 000 000
time per unit: 0.0400 μs
CollectionEquatable (large)
total runs: 450 915
total time: 2.0000 s
average run: 4 μs
runs/second: 250 000
units: 100
units/second: 25 000 000
time per unit: 0.0400 μs
```

*Run on a MacBook Pro (M1 Pro, 16GB RAM)*
*Last Updated: May 26, 2024 using `725b76c9ef072695f3ae4f036c4fa5e015528f13`*
97 changes: 97 additions & 0 deletions benchmarks/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:benchmarking/benchmarking.dart';
import 'package:equatable/equatable.dart';

class EmptyEquatable extends Equatable {
@override
List<Object> get props => [];
}

class PrimitiveEquatable extends Equatable {
const PrimitiveEquatable({
required this.integer,
required this.string,
required this.boolean,
});

final int integer;
final String string;
final bool boolean;

@override
List<Object> get props => [integer, string, boolean];
}

class CollectionEquatable extends Equatable {
const CollectionEquatable({
required this.list,
required this.map,
required this.set,
});

final List<int> list;
final Map<String, int> map;
final Set<int> set;

@override
List<Object> get props => [list, map, set];
}

void main() {
_runBenchmark('EmptyEquatable', (_) => EmptyEquatable());

_runBenchmark(
'PrimitiveEquatable',
(index) => PrimitiveEquatable(
integer: index,
string: '$index',
boolean: index.isEven,
),
);

_runBenchmark(
'CollectionEquatable (small)',
(index) => CollectionEquatable(
list: List.generate(1, (i) => index + i),
map: Map.fromEntries(
List.generate(1, (i) => MapEntry('${index + i}', index + i)),
),
set: Set.from(List.generate(1, (i) => index + i)),
),
);

_runBenchmark(
'CollectionEquatable (medium)',
(index) => CollectionEquatable(
list: List.generate(10, (i) => index + i),
map: Map.fromEntries(
List.generate(10, (i) => MapEntry('${index + i}', index + i)),
),
set: Set.from(List.generate(10, (i) => index + i)),
),
);

_runBenchmark(
'CollectionEquatable (large)',
(index) => CollectionEquatable(
list: List.generate(100, (i) => index + i),
map: Map.fromEntries(
List.generate(100, (i) => MapEntry('${index + i}', index + i)),
),
set: Set.from(List.generate(100, (i) => index + i)),
),
);
}

void _runBenchmark(String name, Object Function(int index) create) {
const poolSize = 100;
final pool = List.generate(poolSize, create);
final poolA = [...pool]..shuffle();
final poolB = [...pool]..shuffle();
bool? result; // so that the loop isn't optimized out
syncBenchmark(name, () {
for (var i = 0; i < poolSize; i++) {
result = poolA[i] == poolB[i];
}
}).report(units: poolSize);
assert(result != null, 'result should be defined.');
}
12 changes: 12 additions & 0 deletions benchmarks/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: equatable_benchmarks
publish_to: none

environment:
sdk: ">=2.12.0 <3.0.0"

dependencies:
equatable: ^2.0.0

dev_dependencies:
benchmarking: ^0.6.1
test: ^1.16.0
3 changes: 3 additions & 0 deletions benchmarks/pubspec_overrides.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependency_overrides:
equatable:
path: ../

0 comments on commit 21590cf

Please sign in to comment.