async_benchmark
is a Dart package for running and analyzing benchmarks with support for isolated environments and
performance tracking.
import 'dart:math';
import 'package:async_benchmark/async_benchmark.dart';
void main() async {
final profile =
BenchmarkProfile('custom', warmup: 10, interactions: 100, rounds: 3);
await BenchmarkPrime(seed: 12345).run(profile: profile, verbose: true);
}
class BenchmarkPrime extends Benchmark<int, Prime> {
final int? seed;
BenchmarkPrime({this.seed}) : super('PrimeCounter(seed: ${seed ?? '*'})');
@override
BenchmarkSetupResult<int, Prime> setup() {
var prime = Prime();
var rand = Random(seed);
var limit = rand.nextInt(999999);
return (setup: limit, service: prime);
}
@override
void job(int setup, Prime? service) {
var prime = service ?? Prime();
prime.countPrimes(setup);
}
}
class Prime {
int countPrimes(int limit) {
var count = 0;
for (var n = 2; n <= limit; ++n) {
if (isPrime(n)) {
++count;
}
}
return count;
}
bool isPrime(int n) {
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
var sqr = sqrt(n);
for (var b = 3; b <= sqr; b += 2) {
if (n % b == 0) return false;
}
return true;
}
@override
String toString() => 'Prime{}';
}
OUTPUT:
╔═══════════════════════════╗
║ PrimeCounter(seed: 12345) ║
╠═══════════════════════════╝
║ BenchmarkProfile[custom]{warmup: 10, interactions: 100, rounds: 3}
╠────────────────────────────
║ Setup...
║ ─ (service: Prime{}, setup: 155253)
║ Warmup (10)...
╠────────────────────────────
║ ROUND: 1/3
║
║ ─ Running (100)...
║ ─ Teardown...
║
║ »» Duration: 0:00:01.356317
║ »» Speed: 73.7291 Hz
║ »» Interaction Time: 13.563 ms
╠────────────────────────────
║ ROUND: 2/3
║
║ ─ Running (100)...
║ ─ Teardown...
║
║ »» Duration: 0:00:01.352660
║ »» Speed: 73.9284 Hz
║ »» Interaction Time: 13.527 ms
╠────────────────────────────
║ ROUND: 3/3
║
║ ─ Running (100)...
║ ─ Teardown...
║
║ »» Duration: 0:00:01.359452
║ »» Speed: 73.5591 Hz
║ »» Interaction Time: 13.595 ms
╠────────────────────────────
║ BEST ROUND: 2
║
║ »» Duration: 0:00:01.352660
║ »» Speed: 73.9284 Hz
║ »» Interaction Time: 13.527 ms
╠────────────────────────────
║ Shutdown...
╚════════════════════════════
The official source code is hosted @ GitHub:
Please file feature requests and bugs at the issue tracker.
Any help from the open-source community is always welcome and needed:
- Found an issue?
- Please fill a bug report with details.
- Wish a feature?
- Open a feature request with use cases.
- Are you using and liking the project?
- Promote the project: create an article, do a post or make a donation.
- Are you a developer?
- Fix a bug and send a pull request.
- Implement a new feature.
- Improve the Unit Tests.
- Have you already helped in any way?
- Many thanks from me, the contributors and everybody that uses this project!
If you donate 1 hour of your time, you can contribute a lot, because others will do the same, just be part and start with your 1 hour.
Graciliano M. Passos: gmpassos@GitHub.
Don't be shy, show some love, and become our GitHub Sponsor. Your support means the world to us, and it keeps the code caffeinated! ☕✨
Thanks a million! 🚀😄