mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
- 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.
194 lines
4.6 KiB
Dart
194 lines
4.6 KiB
Dart
// 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 {
|
|
final dynamic value;
|
|
final Service? dependency;
|
|
|
|
Service({
|
|
required this.value,
|
|
this.dependency,
|
|
});
|
|
}
|
|
|
|
class ServiceImpl extends Service {
|
|
ServiceImpl({
|
|
required super.value,
|
|
super.dependency,
|
|
});
|
|
}
|
|
|
|
class ChainSingletonModule extends Module {
|
|
// количество независимых цепочек
|
|
final int chainCount;
|
|
|
|
// глубина вложенности
|
|
final int nestingDepth;
|
|
|
|
ChainSingletonModule({
|
|
required this.chainCount,
|
|
required this.nestingDepth,
|
|
});
|
|
|
|
@override
|
|
void builder(Scope currentScope) {
|
|
for (var chainIndex = 0; chainIndex < chainCount; chainIndex++) {
|
|
for (var levelIndex = 0; levelIndex < nestingDepth; levelIndex++) {
|
|
final chain = chainIndex + 1;
|
|
final level = levelIndex + 1;
|
|
|
|
final prevDepName = '${chain.toString()}_${(level - 1).toString()}';
|
|
final depName = '${chain.toString()}_${level.toString()}';
|
|
|
|
bind<Service>()
|
|
.toProvide(
|
|
() => ServiceImpl(
|
|
value: depName,
|
|
dependency: currentScope.tryResolve<Service>(
|
|
named: prevDepName,
|
|
),
|
|
),
|
|
)
|
|
.withName(depName)
|
|
.singleton();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class ChainSingletonBenchmark extends BenchmarkBase with BenchmarkWithScope {
|
|
final int chainCount;
|
|
final int nestingDepth;
|
|
|
|
ChainSingletonBenchmark({
|
|
this.chainCount = 1,
|
|
this.nestingDepth = 3,
|
|
}) : super(
|
|
'ChainSingleton (A->B->C, singleton). '
|
|
'C/D = $chainCount/$nestingDepth. ',
|
|
);
|
|
|
|
@override
|
|
void setup() {
|
|
setupScope([
|
|
ChainSingletonModule(
|
|
chainCount: chainCount,
|
|
nestingDepth: nestingDepth,
|
|
),
|
|
]);
|
|
}
|
|
|
|
@override
|
|
void teardown() => teardownScope();
|
|
|
|
@override
|
|
void run() {
|
|
final serviceName = '${chainCount.toString()}_${nestingDepth.toString()}';
|
|
scope.resolve<Service>(named: serviceName);
|
|
}
|
|
}
|
|
|
|
// === DI graph: A -> B -> C (factory/no singleton) ===
|
|
class ChainFactoryModule extends Module {
|
|
// количество независимых цепочек
|
|
final int chainCount;
|
|
|
|
// глубина вложенности
|
|
final int nestingDepth;
|
|
|
|
ChainFactoryModule({
|
|
required this.chainCount,
|
|
required this.nestingDepth,
|
|
});
|
|
|
|
@override
|
|
void builder(Scope currentScope) {
|
|
for (var chainIndex = 0; chainIndex < chainCount; chainIndex++) {
|
|
for (var levelIndex = 0; levelIndex < nestingDepth; levelIndex++) {
|
|
final chain = chainIndex + 1;
|
|
final level = levelIndex + 1;
|
|
|
|
final prevDepName = '${chain.toString()}_${(level - 1).toString()}';
|
|
final depName = '${chain.toString()}_${level.toString()}';
|
|
|
|
bind<Service>()
|
|
.toProvide(
|
|
() => ServiceImpl(
|
|
value: depName,
|
|
dependency: currentScope.tryResolve<Service>(
|
|
named: prevDepName,
|
|
),
|
|
),
|
|
)
|
|
.withName(depName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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. ',
|
|
);
|
|
|
|
@override
|
|
void setup() {
|
|
setupScope([
|
|
ChainFactoryModule(
|
|
chainCount: chainCount,
|
|
nestingDepth: nestingDepth,
|
|
),
|
|
]);
|
|
}
|
|
|
|
@override
|
|
void teardown() => teardownScope();
|
|
|
|
@override
|
|
void run() {
|
|
final serviceName = '${chainCount.toString()}_${nestingDepth.toString()}';
|
|
scope.resolve<Service>(named: serviceName);
|
|
}
|
|
}
|
|
|
|
// === Named bindings: Multiple implementations ===
|
|
class Impl1 {}
|
|
|
|
class Impl2 {}
|
|
|
|
class NamedModule extends Module {
|
|
@override
|
|
void builder(Scope currentScope) {
|
|
bind<Object>().toProvide(() => Impl1()).withName('impl1');
|
|
bind<Object>().toProvide(() => Impl2()).withName('impl2');
|
|
}
|
|
}
|
|
|
|
class NamedResolveBenchmark extends BenchmarkBase with BenchmarkWithScope {
|
|
NamedResolveBenchmark() : super('NamedResolve (by name)');
|
|
|
|
@override
|
|
void setup() {
|
|
setupScope([NamedModule()]);
|
|
}
|
|
|
|
@override
|
|
void teardown() => teardownScope();
|
|
|
|
@override
|
|
void run() {
|
|
// Switch name for comparison
|
|
scope.resolve<Object>(named: 'impl2');
|
|
}
|
|
}
|