Files
cherrypick/benchmark_di/lib/cli/runner.dart
Sergey Penkovsky c91e15319b style: reformat codebase using melos format
Applied consistent code formatting across all packages using \$ melos format
  └> dart format .
     └> RUNNING (in 8 packages)

--------------------------------------------------------------------------------
benchmark_di:
Formatted 18 files (0 changed) in 0.30 seconds.
benchmark_di: SUCCESS
--------------------------------------------------------------------------------
cherrypick:
Formatted 24 files (0 changed) in 0.34 seconds.
cherrypick: SUCCESS
--------------------------------------------------------------------------------
cherrypick_annotations:
Formatted 11 files (0 changed) in 0.14 seconds.
cherrypick_annotations: SUCCESS
--------------------------------------------------------------------------------
cherrypick_flutter:
Formatted 3 files (0 changed) in 0.15 seconds.
cherrypick_flutter: SUCCESS
--------------------------------------------------------------------------------
cherrypick_generator:
Formatted 17 files (0 changed) in 0.27 seconds.
cherrypick_generator: SUCCESS
--------------------------------------------------------------------------------
client_app:
Formatted 4 files (0 changed) in 0.14 seconds.
client_app: SUCCESS
--------------------------------------------------------------------------------
postly:
Formatted lib/router/app_router.gr.dart
Formatted 23 files (1 changed) in 0.33 seconds.
postly: SUCCESS
--------------------------------------------------------------------------------
talker_cherrypick_logger:
Formatted 4 files (0 changed) in 0.18 seconds.
talker_cherrypick_logger: SUCCESS
--------------------------------------------------------------------------------

$ melos format
  └> dart format .
     └> SUCCESS. No functional or logic changes included.
2025-08-13 15:38:44 +03:00

103 lines
3.3 KiB
Dart

import 'dart:io';
import 'dart:math';
import 'package:benchmark_di/benchmarks/universal_chain_benchmark.dart';
import 'package:benchmark_di/benchmarks/universal_chain_async_benchmark.dart';
/// Holds the results for a single benchmark execution.
class BenchmarkResult {
/// List of timings for each run (in microseconds).
final List<num> timings;
/// Difference in memory (RSS, in KB) after running.
final int memoryDiffKb;
/// Difference between peak RSS and initial RSS (in KB).
final int deltaPeakKb;
/// Peak RSS memory observed (in KB).
final int peakRssKb;
BenchmarkResult({
required this.timings,
required this.memoryDiffKb,
required this.deltaPeakKb,
required this.peakRssKb,
});
/// Computes a BenchmarkResult instance from run timings and memory data.
factory BenchmarkResult.collect({
required List<num> timings,
required List<int> rssValues,
required int memBefore,
}) {
final memAfter = ProcessInfo.currentRss;
final memDiffKB = ((memAfter - memBefore) / 1024).round();
final peakRss = [...rssValues, memBefore].reduce(max);
final deltaPeakKb = ((peakRss - memBefore) / 1024).round();
return BenchmarkResult(
timings: timings,
memoryDiffKb: memDiffKB,
deltaPeakKb: deltaPeakKb,
peakRssKb: (peakRss / 1024).round(),
);
}
}
/// Static methods to execute and time benchmarks for DI containers.
class BenchmarkRunner {
/// Runs a synchronous benchmark ([UniversalChainBenchmark]) for a given number of [warmups] and [repeats].
/// Collects execution time and observed memory.
static Future<BenchmarkResult> runSync({
required UniversalChainBenchmark benchmark,
required int warmups,
required int repeats,
}) async {
final timings = <num>[];
final rssValues = <int>[];
for (int i = 0; i < warmups; i++) {
benchmark.setup();
benchmark.run();
benchmark.teardown();
}
final memBefore = ProcessInfo.currentRss;
for (int i = 0; i < repeats; i++) {
benchmark.setup();
final sw = Stopwatch()..start();
benchmark.run();
sw.stop();
timings.add(sw.elapsedMicroseconds);
rssValues.add(ProcessInfo.currentRss);
benchmark.teardown();
}
return BenchmarkResult.collect(
timings: timings, rssValues: rssValues, memBefore: memBefore);
}
/// Runs an asynchronous benchmark ([UniversalChainAsyncBenchmark]) for a given number of [warmups] and [repeats].
/// Collects execution time and observed memory.
static Future<BenchmarkResult> runAsync({
required UniversalChainAsyncBenchmark benchmark,
required int warmups,
required int repeats,
}) async {
final timings = <num>[];
final rssValues = <int>[];
for (int i = 0; i < warmups; i++) {
await benchmark.setup();
await benchmark.run();
await benchmark.teardown();
}
final memBefore = ProcessInfo.currentRss;
for (int i = 0; i < repeats; i++) {
await benchmark.setup();
final sw = Stopwatch()..start();
await benchmark.run();
sw.stop();
timings.add(sw.elapsedMicroseconds);
rssValues.add(ProcessInfo.currentRss);
await benchmark.teardown();
}
return BenchmarkResult.collect(
timings: timings, rssValues: rssValues, memBefore: memBefore);
}
}