mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
refactor(benchmarks): introduce DIAdapter abstraction, migrate all scenarios to use DIAdapter
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
// 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';
|
||||
import 'benchmark_utils.dart';
|
||||
|
||||
class AsyncA {}
|
||||
|
||||
@@ -30,21 +31,22 @@ class AsyncChainModule extends Module {
|
||||
}
|
||||
}
|
||||
|
||||
class AsyncChainBenchmark extends AsyncBenchmarkBase with BenchmarkWithScope {
|
||||
AsyncChainBenchmark() : super('AsyncChain (A->B->C, async)');
|
||||
class AsyncChainBenchmark extends AsyncBenchmarkBase {
|
||||
final DIAdapter di;
|
||||
AsyncChainBenchmark(this.di) : super('AsyncChain (A->B->C, async)');
|
||||
|
||||
@override
|
||||
Future<void> setup() async {
|
||||
setupScope([AsyncChainModule()]);
|
||||
di.setupModules([AsyncChainModule()]);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> teardown() async {
|
||||
teardownScope();
|
||||
di.teardown();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> run() async {
|
||||
await scope.resolveAsync<AsyncC>();
|
||||
await di.resolveAsync<AsyncC>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
// 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';
|
||||
|
||||
@@ -13,26 +15,20 @@ class AppModule extends Module {
|
||||
class FooService {}
|
||||
|
||||
class RegisterAndResolveBenchmark extends BenchmarkBase {
|
||||
RegisterAndResolveBenchmark() : super('RegisterAndResolve');
|
||||
late final Scope scope;
|
||||
final DIAdapter di;
|
||||
|
||||
RegisterAndResolveBenchmark(this.di) : super('RegisterAndResolve');
|
||||
|
||||
@override
|
||||
void setup() {
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
scope = CherryPick.openRootScope();
|
||||
scope.installModules([AppModule()]);
|
||||
di.setupModules([AppModule()]);
|
||||
}
|
||||
|
||||
@override
|
||||
void run() {
|
||||
scope.resolve<FooService>();
|
||||
di.resolve<FooService>();
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() => CherryPick.closeRootScope();
|
||||
}
|
||||
|
||||
void main() {
|
||||
RegisterAndResolveBenchmark().report();
|
||||
void teardown() => di.teardown();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
// 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';
|
||||
import 'benchmark_utils.dart';
|
||||
|
||||
// === DI graph: A -> B -> C (singleton) ===
|
||||
abstract class Service {
|
||||
@@ -59,11 +60,13 @@ class ChainSingletonModule extends Module {
|
||||
}
|
||||
}
|
||||
|
||||
class ChainSingletonBenchmark extends BenchmarkBase with BenchmarkWithScope {
|
||||
class ChainSingletonBenchmark extends BenchmarkBase {
|
||||
final DIAdapter di;
|
||||
final int chainCount;
|
||||
final int nestingDepth;
|
||||
|
||||
ChainSingletonBenchmark({
|
||||
ChainSingletonBenchmark(
|
||||
this.di, {
|
||||
this.chainCount = 1,
|
||||
this.nestingDepth = 3,
|
||||
}) : super(
|
||||
@@ -73,7 +76,7 @@ class ChainSingletonBenchmark extends BenchmarkBase with BenchmarkWithScope {
|
||||
|
||||
@override
|
||||
void setup() {
|
||||
setupScope([
|
||||
di.setupModules([
|
||||
ChainSingletonModule(
|
||||
chainCount: chainCount,
|
||||
nestingDepth: nestingDepth,
|
||||
@@ -82,12 +85,12 @@ class ChainSingletonBenchmark extends BenchmarkBase with BenchmarkWithScope {
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() => teardownScope();
|
||||
void teardown() => di.teardown();
|
||||
|
||||
@override
|
||||
void run() {
|
||||
final serviceName = '${chainCount.toString()}_${nestingDepth.toString()}';
|
||||
scope.resolve<Service>(named: serviceName);
|
||||
di.resolve<Service>(named: serviceName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,11 +132,13 @@ class ChainFactoryModule extends Module {
|
||||
}
|
||||
}
|
||||
|
||||
class ChainFactoryBenchmark extends BenchmarkBase with BenchmarkWithScope {
|
||||
class ChainFactoryBenchmark extends BenchmarkBase {
|
||||
final DIAdapter di;
|
||||
final int chainCount;
|
||||
final int nestingDepth;
|
||||
|
||||
ChainFactoryBenchmark({
|
||||
ChainFactoryBenchmark(
|
||||
this.di, {
|
||||
this.chainCount = 1,
|
||||
this.nestingDepth = 3,
|
||||
}) : super(
|
||||
@@ -143,7 +148,7 @@ class ChainFactoryBenchmark extends BenchmarkBase with BenchmarkWithScope {
|
||||
|
||||
@override
|
||||
void setup() {
|
||||
setupScope([
|
||||
di.setupModules([
|
||||
ChainFactoryModule(
|
||||
chainCount: chainCount,
|
||||
nestingDepth: nestingDepth,
|
||||
@@ -152,12 +157,12 @@ class ChainFactoryBenchmark extends BenchmarkBase with BenchmarkWithScope {
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() => teardownScope();
|
||||
void teardown() => di.teardown();
|
||||
|
||||
@override
|
||||
void run() {
|
||||
final serviceName = '${chainCount.toString()}_${nestingDepth.toString()}';
|
||||
scope.resolve<Service>(named: serviceName);
|
||||
di.resolve<Service>(named: serviceName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,20 +179,21 @@ class NamedModule extends Module {
|
||||
}
|
||||
}
|
||||
|
||||
class NamedResolveBenchmark extends BenchmarkBase with BenchmarkWithScope {
|
||||
NamedResolveBenchmark() : super('NamedResolve (by name)');
|
||||
class NamedResolveBenchmark extends BenchmarkBase {
|
||||
final DIAdapter di;
|
||||
|
||||
NamedResolveBenchmark(this.di) : super('NamedResolve (by name)');
|
||||
|
||||
@override
|
||||
void setup() {
|
||||
setupScope([NamedModule()]);
|
||||
di.setupModules([NamedModule()]);
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() => teardownScope();
|
||||
void teardown() => di.teardown();
|
||||
|
||||
@override
|
||||
void run() {
|
||||
// Switch name for comparison
|
||||
scope.resolve<Object>(named: 'impl2');
|
||||
di.resolve<Object>(named: 'impl2');
|
||||
}
|
||||
}
|
||||
|
||||
78
benchmark_cherrypick/lib/di_adapter.dart
Normal file
78
benchmark_cherrypick/lib/di_adapter.dart
Normal file
@@ -0,0 +1,78 @@
|
||||
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);
|
||||
}
|
||||
|
||||
class CherrypickDIAdapter implements DIAdapter {
|
||||
Scope? _scope;
|
||||
|
||||
@override
|
||||
void setupModules(List<Module> modules) {
|
||||
_scope = CherryPick.openRootScope();
|
||||
_scope!.installModules(modules);
|
||||
}
|
||||
|
||||
@override
|
||||
T resolve<T>({String? named}) {
|
||||
return named == null
|
||||
? _scope!.resolve<T>()
|
||||
: _scope!.resolve<T>(named: named);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<T> resolveAsync<T>({String? named}) async {
|
||||
return named == null
|
||||
? await _scope!.resolveAsync<T>()
|
||||
: await _scope!.resolveAsync<T>(named: named);
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
CherryPick.closeRootScope();
|
||||
_scope = null;
|
||||
}
|
||||
|
||||
@override
|
||||
CherrypickDIAdapter openSubScope(String name) {
|
||||
final sub = _scope!.openSubScope(name);
|
||||
return _CherrypickSubScopeAdapter(sub);
|
||||
}
|
||||
}
|
||||
|
||||
class _CherrypickSubScopeAdapter extends CherrypickDIAdapter {
|
||||
final Scope _subScope;
|
||||
_CherrypickSubScopeAdapter(this._subScope);
|
||||
@override
|
||||
void setupModules(List<Module> modules) {
|
||||
_subScope.installModules(modules);
|
||||
}
|
||||
|
||||
@override
|
||||
T resolve<T>({String? named}) {
|
||||
return named == null
|
||||
? _subScope.resolve<T>()
|
||||
: _subScope.resolve<T>(named: named);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<T> resolveAsync<T>({String? named}) async {
|
||||
return named == null
|
||||
? await _subScope.resolveAsync<T>()
|
||||
: await _subScope.resolveAsync<T>(named: named);
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
// subScope teardown убирать отдельно не требуется
|
||||
}
|
||||
|
||||
@override
|
||||
CherrypickDIAdapter openSubScope(String name) {
|
||||
return _CherrypickSubScopeAdapter(_subScope.openSubScope(name));
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
// 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';
|
||||
import 'benchmark_utils.dart';
|
||||
|
||||
class Shared {}
|
||||
|
||||
@@ -23,26 +24,26 @@ class ChildOverrideModule extends Module {
|
||||
}
|
||||
}
|
||||
|
||||
class ScopeOverrideBenchmark extends BenchmarkBase with BenchmarkWithScope {
|
||||
ScopeOverrideBenchmark() : super('ScopeOverride (child overrides parent)');
|
||||
late Scope child;
|
||||
class ScopeOverrideBenchmark extends BenchmarkBase {
|
||||
final DIAdapter di;
|
||||
late DIAdapter childDi;
|
||||
|
||||
ScopeOverrideBenchmark(this.di) : super('ScopeOverride (child overrides parent)');
|
||||
|
||||
@override
|
||||
void setup() {
|
||||
setupScope([ParentModule()]);
|
||||
child = scope.openSubScope('child');
|
||||
child.installModules([ChildOverrideModule()]);
|
||||
di.setupModules([ParentModule()]);
|
||||
childDi = di.openSubScope('child');
|
||||
childDi.setupModules([ChildOverrideModule()]);
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
teardownScope();
|
||||
}
|
||||
void teardown() => di.teardown();
|
||||
|
||||
@override
|
||||
void run() {
|
||||
// Должен возвращать ChildImpl, а не ParentImpl
|
||||
final resolved = child.resolve<Shared>();
|
||||
// Should return ChildImpl, not ParentImpl
|
||||
final resolved = childDi.resolve<Shared>();
|
||||
assert(resolved is ChildImpl);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user