refactor(structure): move benchmarks, scenarios, adapters, utils to dedicated folders; update imports/project layout

This commit is contained in:
Sergey Penkovsky
2025-08-06 16:21:31 +03:00
parent 553dbb6539
commit b27a7df161
27 changed files with 332 additions and 327 deletions

View File

@@ -15,6 +15,7 @@ include: package:lints/recommended.yaml
analyzer:
errors:
deprecated_member_use: ignore
depend_on_referenced_packages: ignore
# Uncomment the following section to specify additional rules.

View File

@@ -1,8 +1,10 @@
import 'package:benchmark_cherrypick/cherrypick_benchmark.dart';
import 'package:benchmark_cherrypick/complex_bindings_benchmark.dart';
import 'package:benchmark_cherrypick/async_chain_benchmark.dart';
import 'package:benchmark_cherrypick/di_adapter.dart';
import 'package:benchmark_cherrypick/scope_override_benchmark.dart';
import 'package:benchmark_cherrypick/benchmarks/register_and_resolve_benchmark.dart';
import 'package:benchmark_cherrypick/benchmarks/chain_singleton_benchmark.dart';
import 'package:benchmark_cherrypick/benchmarks/chain_factory_benchmark.dart';
import 'package:benchmark_cherrypick/benchmarks/named_resolve_benchmark.dart';
import 'package:benchmark_cherrypick/benchmarks/scope_override_benchmark.dart';
import 'package:benchmark_cherrypick/benchmarks/async_chain_benchmark.dart';
import 'package:benchmark_cherrypick/di_adapters/cherrypick_adapter.dart';
import 'package:args/args.dart';
import 'dart:io';
import 'dart:math';

View File

@@ -1,52 +0,0 @@
// ignore: depend_on_referenced_packages
import 'package:benchmark_cherrypick/di_adapter.dart';
// ignore: depend_on_referenced_packages
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:cherrypick/cherrypick.dart';
class AsyncA {}
class AsyncB {
final AsyncA a;
AsyncB(this.a);
}
class AsyncC {
final AsyncB b;
AsyncC(this.b);
}
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();
}
}
class AsyncChainBenchmark extends AsyncBenchmarkBase {
final DIAdapter di;
AsyncChainBenchmark(this.di) : super('AsyncChain (A->B->C, async)');
@override
Future<void> setup() async {
di.setupModules([AsyncChainModule()]);
}
@override
Future<void> teardown() async {
di.teardown();
}
@override
Future<void> run() async {
await di.resolveAsync<AsyncC>();
}
}

View File

@@ -0,0 +1,23 @@
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:benchmark_cherrypick/di_adapters/di_adapter.dart';
import 'package:benchmark_cherrypick/scenarios/async_chain_module.dart';
class AsyncChainBenchmark extends AsyncBenchmarkBase {
final DIAdapter di;
AsyncChainBenchmark(this.di) : super('AsyncChain (A->B->C, async)');
@override
Future<void> setup() async {
di.setupModules([AsyncChainModule()]);
}
@override
Future<void> teardown() async {
di.teardown();
}
@override
Future<void> run() async {
await di.resolveAsync<AsyncC>();
}
}

View File

@@ -0,0 +1,38 @@
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:benchmark_cherrypick/di_adapters/di_adapter.dart';
import 'package:benchmark_cherrypick/scenarios/chain_factory_module.dart';
import 'package:benchmark_cherrypick/scenarios/service.dart';
class ChainFactoryBenchmark extends BenchmarkBase {
final DIAdapter di;
final int chainCount;
final int nestingDepth;
ChainFactoryBenchmark(
this.di, {
this.chainCount = 1,
this.nestingDepth = 3,
}) : super(
'ChainFactory (A->B->C, factory). '
'C/D = $chainCount/$nestingDepth. ',
);
@override
void setup() {
di.setupModules([
ChainFactoryModule(
chainCount: chainCount,
nestingDepth: nestingDepth,
),
]);
}
@override
void teardown() => di.teardown();
@override
void run() {
final serviceName = '${chainCount.toString()}_${nestingDepth.toString()}';
di.resolve<Service>(named: serviceName);
}
}

View File

@@ -0,0 +1,38 @@
import 'package:benchmark_cherrypick/di_adapters/di_adapter.dart';
import 'package:benchmark_harness/benchmark_harness.dart';
import '../scenarios/chain_singleton_module.dart';
import '../scenarios/service.dart';
class ChainSingletonBenchmark extends BenchmarkBase {
final DIAdapter di;
final int chainCount;
final int nestingDepth;
ChainSingletonBenchmark(
this.di, {
this.chainCount = 1,
this.nestingDepth = 3,
}) : super(
'ChainSingleton (A->B->C, singleton). '
'C/D = $chainCount/$nestingDepth. ',
);
@override
void setup() {
di.setupModules([
ChainSingletonModule(
chainCount: chainCount,
nestingDepth: nestingDepth,
),
]);
}
@override
void teardown() => di.teardown();
@override
void run() {
final serviceName = '${chainCount.toString()}_${nestingDepth.toString()}';
di.resolve<Service>(named: serviceName);
}
}

View File

@@ -0,0 +1,22 @@
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:benchmark_cherrypick/di_adapters/di_adapter.dart';
import 'package:benchmark_cherrypick/scenarios/named_module.dart';
class NamedResolveBenchmark extends BenchmarkBase {
final DIAdapter di;
NamedResolveBenchmark(this.di) : super('NamedResolve (by name)');
@override
void setup() {
di.setupModules([NamedModule()]);
}
@override
void teardown() => di.teardown();
@override
void run() {
di.resolve<Object>(named: 'impl2');
}
}

View File

@@ -1,18 +1,7 @@
// ignore: depend_on_referenced_packages
import 'package:benchmark_cherrypick/di_adapter.dart';
// ignore: depend_on_referenced_packages
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:cherrypick/cherrypick.dart';
class AppModule extends Module {
@override
void builder(Scope currentScope) {
bind<FooService>().toProvide(() => FooService());
}
}
// Dummy service for DI
class FooService {}
import 'package:benchmark_cherrypick/di_adapters/di_adapter.dart';
import 'package:benchmark_cherrypick/scenarios/app_module.dart';
import 'package:benchmark_cherrypick/scenarios/foo_service.dart';
class RegisterAndResolveBenchmark extends BenchmarkBase {
final DIAdapter di;

View File

@@ -0,0 +1,29 @@
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:benchmark_cherrypick/di_adapters/di_adapter.dart';
import 'package:benchmark_cherrypick/scenarios/parent_module.dart';
import 'package:benchmark_cherrypick/scenarios/child_override_module.dart';
import 'package:benchmark_cherrypick/scenarios/shared.dart';
import 'package:benchmark_cherrypick/scenarios/child_impl.dart';
class ScopeOverrideBenchmark extends BenchmarkBase {
final DIAdapter di;
late DIAdapter childDi;
ScopeOverrideBenchmark(this.di) : super('ScopeOverride (child overrides parent)');
@override
void setup() {
di.setupModules([ParentModule()]);
childDi = di.openSubScope('child');
childDi.setupModules([ChildOverrideModule()]);
}
@override
void teardown() => di.teardown();
@override
void run() {
final resolved = childDi.resolve<Shared>();
assert(resolved is ChildImpl);
}
}

View File

@@ -1,199 +0,0 @@
// ignore: depend_on_referenced_packages
import 'package:benchmark_cherrypick/di_adapter.dart';
// ignore: depend_on_referenced_packages
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:cherrypick/cherrypick.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 {
final DIAdapter di;
final int chainCount;
final int nestingDepth;
ChainSingletonBenchmark(
this.di, {
this.chainCount = 1,
this.nestingDepth = 3,
}) : super(
'ChainSingleton (A->B->C, singleton). '
'C/D = $chainCount/$nestingDepth. ',
);
@override
void setup() {
di.setupModules([
ChainSingletonModule(
chainCount: chainCount,
nestingDepth: nestingDepth,
),
]);
}
@override
void teardown() => di.teardown();
@override
void run() {
final serviceName = '${chainCount.toString()}_${nestingDepth.toString()}';
di.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 {
final DIAdapter di;
final int chainCount;
final int nestingDepth;
ChainFactoryBenchmark(
this.di, {
this.chainCount = 1,
this.nestingDepth = 3,
}) : super(
'ChainFactory (A->B->C, factory). '
'C/D = $chainCount/$nestingDepth. ',
);
@override
void setup() {
di.setupModules([
ChainFactoryModule(
chainCount: chainCount,
nestingDepth: nestingDepth,
),
]);
}
@override
void teardown() => di.teardown();
@override
void run() {
final serviceName = '${chainCount.toString()}_${nestingDepth.toString()}';
di.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 {
final DIAdapter di;
NamedResolveBenchmark(this.di) : super('NamedResolve (by name)');
@override
void setup() {
di.setupModules([NamedModule()]);
}
@override
void teardown() => di.teardown();
@override
void run() {
di.resolve<Object>(named: 'impl2');
}
}

View File

@@ -1,12 +1,5 @@
import 'package:cherrypick/cherrypick.dart';
abstract class DIAdapter {
void setupModules(List<Module> modules);
T resolve<T>({String? named});
Future<T> resolveAsync<T>({String? named});
void teardown();
DIAdapter openSubScope(String name);
}
import 'di_adapter.dart';
class CherrypickDIAdapter implements DIAdapter {
Scope? _scope;

View File

@@ -0,0 +1,9 @@
import 'package:cherrypick/cherrypick.dart';
abstract class DIAdapter {
void setupModules(List<Module> modules);
T resolve<T>({String? named});
Future<T> resolveAsync<T>({String? named});
void teardown();
DIAdapter openSubScope(String name);
}

View File

@@ -0,0 +1,9 @@
import 'package:cherrypick/cherrypick.dart';
import 'foo_service.dart';
class AppModule extends Module {
@override
void builder(Scope currentScope) {
bind<FooService>().toProvide(() => FooService());
}
}

View File

@@ -0,0 +1,20 @@
import 'package:cherrypick/cherrypick.dart';
class AsyncA {}
class AsyncB {
final AsyncA a;
AsyncB(this.a);
}
class AsyncC {
final AsyncB b;
AsyncC(this.b);
}
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();
}
}

View File

@@ -0,0 +1,42 @@
// === DI graph: A -> B -> C (factory/no singleton) ===
import 'package:cherrypick/cherrypick.dart';
import 'service.dart';
import 'service_impl.dart';
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);
}
}
}
}

View File

@@ -0,0 +1,42 @@
import 'package:cherrypick/cherrypick.dart';
import 'service.dart';
import 'service_impl.dart';
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();
}
}
}
}

View File

@@ -0,0 +1,2 @@
import 'shared.dart';
class ChildImpl extends Shared {}

View File

@@ -0,0 +1,10 @@
import 'package:cherrypick/cherrypick.dart';
import 'child_impl.dart';
import 'shared.dart';
class ChildOverrideModule extends Module {
@override
void builder(Scope currentScope) {
bind<Shared>().toProvide(() => ChildImpl()).singleton();
}
}

View File

@@ -0,0 +1 @@
class FooService {}

View File

@@ -0,0 +1,12 @@
import 'package:cherrypick/cherrypick.dart';
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');
}
}

View File

@@ -0,0 +1,2 @@
import 'shared.dart';
class ParentImpl extends Shared {}

View File

@@ -0,0 +1,10 @@
import 'package:cherrypick/cherrypick.dart';
import 'parent_impl.dart';
import 'shared.dart';
class ParentModule extends Module {
@override
void builder(Scope currentScope) {
bind<Shared>().toProvide(() => ParentImpl()).singleton();
}
}

View File

@@ -0,0 +1,5 @@
abstract class Service {
final dynamic value;
final Service? dependency;
Service({required this.value, this.dependency});
}

View File

@@ -0,0 +1,5 @@
import 'service.dart';
class ServiceImpl extends Service {
ServiceImpl({required super.value, super.dependency});
}

View File

@@ -0,0 +1 @@
class Shared {}

View File

@@ -1,49 +0,0 @@
// ignore: depend_on_referenced_packages
import 'package:benchmark_cherrypick/di_adapter.dart';
// ignore: depend_on_referenced_packages
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:cherrypick/cherrypick.dart';
class Shared {}
class ParentImpl extends Shared {}
class ChildImpl extends Shared {}
class ParentModule extends Module {
@override
void builder(Scope currentScope) {
bind<Shared>().toProvide(() => ParentImpl()).singleton();
}
}
class ChildOverrideModule extends Module {
@override
void builder(Scope currentScope) {
bind<Shared>().toProvide(() => ChildImpl()).singleton();
}
}
class ScopeOverrideBenchmark extends BenchmarkBase {
final DIAdapter di;
late DIAdapter childDi;
ScopeOverrideBenchmark(this.di) : super('ScopeOverride (child overrides parent)');
@override
void setup() {
di.setupModules([ParentModule()]);
childDi = di.openSubScope('child');
childDi.setupModules([ChildOverrideModule()]);
}
@override
void teardown() => di.teardown();
@override
void run() {
// Should return ChildImpl, not ParentImpl
final resolved = childDi.resolve<Shared>();
assert(resolved is ChildImpl);
}
}