mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
impr: BENCHMARK - complex benchmark improvements.
This commit is contained in:
@@ -3,72 +3,179 @@ import 'package:benchmark_harness/benchmark_harness.dart';
|
|||||||
import 'package:cherrypick/cherrypick.dart';
|
import 'package:cherrypick/cherrypick.dart';
|
||||||
|
|
||||||
// === DI graph: A -> B -> C (singleton) ===
|
// === DI graph: A -> B -> C (singleton) ===
|
||||||
class ServiceA {}
|
abstract class Service {
|
||||||
class ServiceB {
|
final dynamic value;
|
||||||
final ServiceA a;
|
final Service? dependency;
|
||||||
ServiceB(this.a);
|
|
||||||
|
Service({
|
||||||
|
required this.value,
|
||||||
|
this.dependency,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
class ServiceC {
|
|
||||||
final ServiceB b;
|
class ServiceImpl extends Service {
|
||||||
ServiceC(this.b);
|
ServiceImpl({
|
||||||
|
required super.value,
|
||||||
|
super.dependency,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
class ChainSingletonModule extends Module {
|
class ChainSingletonModule extends Module {
|
||||||
|
// количество независимых цепочек
|
||||||
|
final int chainCount;
|
||||||
|
|
||||||
|
// глубина вложенности
|
||||||
|
final int nestingDepth;
|
||||||
|
|
||||||
|
ChainSingletonModule({
|
||||||
|
required this.chainCount,
|
||||||
|
required this.nestingDepth,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void builder(Scope currentScope) {
|
void builder(Scope currentScope) {
|
||||||
bind<ServiceA>().toProvide(() => ServiceA()).singleton();
|
for (var chainIndex = 0; chainIndex < chainCount; chainIndex++) {
|
||||||
bind<ServiceB>().toProvide((() => ServiceB(currentScope.resolve<ServiceA>()))).singleton();
|
for (var levelIndex = 0; levelIndex < nestingDepth; levelIndex++) {
|
||||||
bind<ServiceC>().toProvide((() => ServiceC(currentScope.resolve<ServiceB>()))).singleton();
|
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 {
|
class ChainSingletonBenchmark extends BenchmarkBase {
|
||||||
ChainSingletonBenchmark() : super('ChainSingleton (A->B->C, singleton)');
|
final int chainCount;
|
||||||
|
final int nestingDepth;
|
||||||
|
|
||||||
|
ChainSingletonBenchmark({
|
||||||
|
this.chainCount = 1,
|
||||||
|
this.nestingDepth = 3,
|
||||||
|
}) : super(
|
||||||
|
'ChainSingleton (A->B->C, singleton). '
|
||||||
|
'C/D = $chainCount/$nestingDepth. ',
|
||||||
|
);
|
||||||
late Scope scope;
|
late Scope scope;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void setup() {
|
void setup() {
|
||||||
scope = CherryPick.openRootScope();
|
scope = CherryPick.openRootScope();
|
||||||
scope.installModules([ChainSingletonModule()]);
|
scope.installModules([
|
||||||
|
ChainSingletonModule(
|
||||||
|
chainCount: chainCount,
|
||||||
|
nestingDepth: nestingDepth,
|
||||||
|
),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void teardown() => CherryPick.closeRootScope();
|
void teardown() => CherryPick.closeRootScope();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void run() {
|
void run() {
|
||||||
scope.resolve<ServiceC>();
|
final serviceName = '${chainCount.toString()}_${nestingDepth.toString()}';
|
||||||
|
scope.resolve<Service>(named: serviceName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// === DI graph: A -> B -> C (factory/no singleton) ===
|
// === DI graph: A -> B -> C (factory/no singleton) ===
|
||||||
class ChainFactoryModule extends Module {
|
class ChainFactoryModule extends Module {
|
||||||
|
// количество независимых цепочек
|
||||||
|
final int chainCount;
|
||||||
|
|
||||||
|
// глубина вложенности
|
||||||
|
final int nestingDepth;
|
||||||
|
|
||||||
|
ChainFactoryModule({
|
||||||
|
required this.chainCount,
|
||||||
|
required this.nestingDepth,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void builder(Scope currentScope) {
|
void builder(Scope currentScope) {
|
||||||
bind<ServiceA>().toProvide(() => ServiceA());
|
for (var chainIndex = 0; chainIndex < chainCount; chainIndex++) {
|
||||||
bind<ServiceB>().toProvide((() => ServiceB(currentScope.resolve<ServiceA>())));
|
for (var levelIndex = 0; levelIndex < nestingDepth; levelIndex++) {
|
||||||
bind<ServiceC>().toProvide((() => ServiceC(currentScope.resolve<ServiceB>())));
|
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 {
|
class ChainFactoryBenchmark extends BenchmarkBase {
|
||||||
ChainFactoryBenchmark() : super('ChainFactory (A->B->C, factory)');
|
// количество независимых цепочек
|
||||||
|
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;
|
late Scope scope;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void setup() {
|
void setup() {
|
||||||
CherryPick.disableGlobalCycleDetection();
|
CherryPick.disableGlobalCycleDetection();
|
||||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||||
|
|
||||||
scope = CherryPick.openRootScope();
|
scope = CherryPick.openRootScope();
|
||||||
scope.installModules([ChainFactoryModule()]);
|
scope.installModules([
|
||||||
|
ChainFactoryModule(
|
||||||
|
chainCount: chainCount,
|
||||||
|
nestingDepth: nestingDepth,
|
||||||
|
),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void teardown() => CherryPick.closeRootScope();
|
void teardown() => CherryPick.closeRootScope();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void run() {
|
void run() {
|
||||||
scope.resolve<ServiceC>();
|
final serviceName = '${chainCount.toString()}_${nestingDepth.toString()}';
|
||||||
|
scope.resolve<Service>(named: serviceName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// === Named bindings: Multiple implementations ===
|
// === Named bindings: Multiple implementations ===
|
||||||
class Impl1 {}
|
class Impl1 {}
|
||||||
|
|
||||||
class Impl2 {}
|
class Impl2 {}
|
||||||
|
|
||||||
class NamedModule extends Module {
|
class NamedModule extends Module {
|
||||||
@override
|
@override
|
||||||
void builder(Scope currentScope) {
|
void builder(Scope currentScope) {
|
||||||
@@ -80,16 +187,20 @@ class NamedModule extends Module {
|
|||||||
class NamedResolveBenchmark extends BenchmarkBase {
|
class NamedResolveBenchmark extends BenchmarkBase {
|
||||||
NamedResolveBenchmark() : super('NamedResolve (by name)');
|
NamedResolveBenchmark() : super('NamedResolve (by name)');
|
||||||
late Scope scope;
|
late Scope scope;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void setup() {
|
void setup() {
|
||||||
scope = CherryPick.openRootScope();
|
scope = CherryPick.openRootScope();
|
||||||
scope.installModules([NamedModule()]);
|
scope.installModules([NamedModule()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void teardown() => CherryPick.closeRootScope();
|
void teardown() => CherryPick.closeRootScope();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void run() {
|
void run() {
|
||||||
// Switch name for comparison
|
// Switch name for comparison
|
||||||
scope.resolve<Object>(named: 'impl2');
|
scope.resolve<Object>(named: 'impl2');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user