refactor(benchmarks): unify benchmark structure, enable CLI parameterization, run matrix, add CSV/JSON/pretty output

- All benchmarks now use a unified base mixin for setup/teardown (BenchmarkWithScope).
- Added args package support: CLI flags for choosing benchmarks, chain counts, nesting depths, output format.
- Support for running benchmarks in matrix mode (multiple parameter sets).
- Machine-readable output: csv, json, pretty-table.
- Loop and naming lint fixes, unused imports removed.
This commit is contained in:
Sergey Penkovsky
2025-08-06 13:29:23 +03:00
parent a5ef0dc437
commit 926bbf15f4
8 changed files with 211 additions and 71 deletions

View File

@@ -1,12 +1,15 @@
// ignore: depend_on_referenced_packages
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:cherrypick/cherrypick.dart';
import 'benchmark_utils.dart';
class AsyncA {}
class AsyncB {
final AsyncA a;
AsyncB(this.a);
}
class AsyncC {
final AsyncB b;
AsyncC(this.b);
@@ -16,26 +19,30 @@ class AsyncChainModule extends Module {
@override
void builder(Scope currentScope) {
bind<AsyncA>().toProvideAsync(() async => AsyncA()).singleton();
bind<AsyncB>().toProvideAsync(() async => AsyncB(await currentScope.resolveAsync<AsyncA>())).singleton();
bind<AsyncC>().toProvideAsync(() async => AsyncC(await currentScope.resolveAsync<AsyncB>())).singleton();
bind<AsyncB>()
.toProvideAsync(
() async => AsyncB(await currentScope.resolveAsync<AsyncA>()))
.singleton();
bind<AsyncC>()
.toProvideAsync(
() async => AsyncC(await currentScope.resolveAsync<AsyncB>()))
.singleton();
}
}
class AsyncChainBenchmark extends AsyncBenchmarkBase {
class AsyncChainBenchmark extends AsyncBenchmarkBase with BenchmarkWithScope {
AsyncChainBenchmark() : super('AsyncChain (A->B->C, async)');
late Scope scope;
@override
Future<void> setup() async {
CherryPick.disableGlobalCycleDetection();
CherryPick.disableGlobalCrossScopeCycleDetection();
scope = CherryPick.openRootScope();
scope.installModules([AsyncChainModule()]);
setupScope([AsyncChainModule()]);
}
@override
Future<void> teardown() async {
CherryPick.closeRootScope();
teardownScope();
}
@override
Future<void> run() async {
await scope.resolveAsync<AsyncC>();

View File

@@ -0,0 +1,29 @@
import 'package:cherrypick/cherrypick.dart';
/// Миксин для упрощения работы с CherryPick Scope в бенчмарках.
mixin BenchmarkWithScope {
Scope? _scope;
/// Отключить глобальные проверки циклов и создать корневой scope с модулями.
void setupScope(List<Module> modules,
{bool disableCycleDetection = true,
bool disableCrossScopeCycleDetection = true}) {
if (disableCycleDetection) {
CherryPick.disableGlobalCycleDetection();
}
if (disableCrossScopeCycleDetection) {
CherryPick.disableGlobalCrossScopeCycleDetection();
}
_scope = CherryPick.openRootScope();
_scope!.installModules(modules);
}
/// Закрывает текущий scope.
void teardownScope() {
CherryPick.closeRootScope();
_scope = null;
}
/// Получить текущий scope. Не null после setupScope.
Scope get scope => _scope!;
}

View File

@@ -5,9 +5,8 @@ import 'package:cherrypick/cherrypick.dart';
class AppModule extends Module {
@override
void builder(Scope currentScope) {
bind<FooService>().toProvide(() => FooService());
bind<FooService>().toProvide(() => FooService());
}
}
// Dummy service for DI
@@ -23,7 +22,6 @@ class RegisterAndResolveBenchmark extends BenchmarkBase {
CherryPick.disableGlobalCrossScopeCycleDetection();
scope = CherryPick.openRootScope();
scope.installModules([AppModule()]);
}
@override

View File

@@ -1,6 +1,7 @@
// ignore: depend_on_referenced_packages
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:cherrypick/cherrypick.dart';
import 'benchmark_utils.dart';
// === DI graph: A -> B -> C (singleton) ===
abstract class Service {
@@ -45,12 +46,12 @@ class ChainSingletonModule extends Module {
bind<Service>()
.toProvide(
() => ServiceImpl(
value: depName,
dependency: currentScope.tryResolve<Service>(
named: prevDepName,
),
),
)
value: depName,
dependency: currentScope.tryResolve<Service>(
named: prevDepName,
),
),
)
.withName(depName)
.singleton();
}
@@ -58,7 +59,7 @@ class ChainSingletonModule extends Module {
}
}
class ChainSingletonBenchmark extends BenchmarkBase {
class ChainSingletonBenchmark extends BenchmarkBase with BenchmarkWithScope {
final int chainCount;
final int nestingDepth;
@@ -66,15 +67,13 @@ class ChainSingletonBenchmark extends BenchmarkBase {
this.chainCount = 1,
this.nestingDepth = 3,
}) : super(
'ChainSingleton (A->B->C, singleton). '
'C/D = $chainCount/$nestingDepth. ',
);
late Scope scope;
'ChainSingleton (A->B->C, singleton). '
'C/D = $chainCount/$nestingDepth. ',
);
@override
void setup() {
scope = CherryPick.openRootScope();
scope.installModules([
setupScope([
ChainSingletonModule(
chainCount: chainCount,
nestingDepth: nestingDepth,
@@ -83,7 +82,7 @@ class ChainSingletonBenchmark extends BenchmarkBase {
}
@override
void teardown() => CherryPick.closeRootScope();
void teardown() => teardownScope();
@override
void run() {
@@ -118,42 +117,33 @@ class ChainFactoryModule extends Module {
bind<Service>()
.toProvide(
() => ServiceImpl(
value: depName,
dependency: currentScope.tryResolve<Service>(
named: prevDepName,
),
),
)
value: depName,
dependency: currentScope.tryResolve<Service>(
named: prevDepName,
),
),
)
.withName(depName);
}
}
}
}
class ChainFactoryBenchmark extends BenchmarkBase {
// количество независимых цепочек
class ChainFactoryBenchmark extends BenchmarkBase with BenchmarkWithScope {
final int chainCount;
// глубина вложенности
final int nestingDepth;
ChainFactoryBenchmark({
this.chainCount = 1,
this.nestingDepth = 3,
}) : super(
'ChainFactory (A->B->C, factory). '
'C/D = $chainCount/$nestingDepth. ',
);
late Scope scope;
'ChainFactory (A->B->C, factory). '
'C/D = $chainCount/$nestingDepth. ',
);
@override
void setup() {
CherryPick.disableGlobalCycleDetection();
CherryPick.disableGlobalCrossScopeCycleDetection();
scope = CherryPick.openRootScope();
scope.installModules([
setupScope([
ChainFactoryModule(
chainCount: chainCount,
nestingDepth: nestingDepth,
@@ -162,7 +152,7 @@ class ChainFactoryBenchmark extends BenchmarkBase {
}
@override
void teardown() => CherryPick.closeRootScope();
void teardown() => teardownScope();
@override
void run() {
@@ -184,18 +174,16 @@ class NamedModule extends Module {
}
}
class NamedResolveBenchmark extends BenchmarkBase {
class NamedResolveBenchmark extends BenchmarkBase with BenchmarkWithScope {
NamedResolveBenchmark() : super('NamedResolve (by name)');
late Scope scope;
@override
void setup() {
scope = CherryPick.openRootScope();
scope.installModules([NamedModule()]);
setupScope([NamedModule()]);
}
@override
void teardown() => CherryPick.closeRootScope();
void teardown() => teardownScope();
@override
void run() {
@@ -203,4 +191,3 @@ class NamedResolveBenchmark extends BenchmarkBase {
scope.resolve<Object>(named: 'impl2');
}
}

View File

@@ -1,9 +1,12 @@
// ignore: depend_on_referenced_packages
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:cherrypick/cherrypick.dart';
import 'benchmark_utils.dart';
class Shared {}
class ParentImpl extends Shared {}
class ChildImpl extends Shared {}
class ParentModule extends Module {
@@ -20,23 +23,22 @@ class ChildOverrideModule extends Module {
}
}
class ScopeOverrideBenchmark extends BenchmarkBase {
class ScopeOverrideBenchmark extends BenchmarkBase with BenchmarkWithScope {
ScopeOverrideBenchmark() : super('ScopeOverride (child overrides parent)');
late Scope parent;
late Scope child;
@override
void setup() {
CherryPick.disableGlobalCycleDetection();
CherryPick.disableGlobalCrossScopeCycleDetection();
parent = CherryPick.openRootScope();
parent.installModules([ParentModule()]);
child = parent.openSubScope('child');
setupScope([ParentModule()]);
child = scope.openSubScope('child');
child.installModules([ChildOverrideModule()]);
}
@override
void teardown() {
CherryPick.closeRootScope();
teardownScope();
}
@override
void run() {
// Должен возвращать ChildImpl, а не ParentImpl