-
Notifications
You must be signed in to change notification settings - Fork 57
DI 2.0.0 #135
Changes from all commits
79be3d6
57523ea
10e471b
f878cfe
279be49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import 'package:benchmark_harness/benchmark_harness.dart'; | ||
import 'package:di/dynamic_injector.dart'; | ||
import 'package:di/src/reflector_dynamic.dart'; | ||
|
||
import 'injector_benchmark_common.dart'; | ||
|
||
|
||
main() { | ||
new InjectorBenchmark('DynamicInjectorBenchmark', | ||
(m) => new DynamicInjector(modules: m)).report(); | ||
} | ||
new DynamicTypeFactories()).report(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,32 +6,43 @@ import 'package:di/di.dart'; | |
int count = 0; | ||
|
||
class InjectorBenchmark extends BenchmarkBase { | ||
var injectorFactory; | ||
var module; | ||
var typeReflector; | ||
Key KEY_A; | ||
Key KEY_B; | ||
Key KEY_C; | ||
Key KEY_D; | ||
Key KEY_E; | ||
|
||
InjectorBenchmark(name, this.injectorFactory) : super(name); | ||
InjectorBenchmark(name, this.typeReflector) : super(name); | ||
|
||
void run() { | ||
Injector injector = injectorFactory([module]); | ||
injector.get(A); | ||
injector.get(B); | ||
Injector injector = new ModuleInjector([module]); | ||
injector.getByKey(KEY_A); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move these benchmark changes to a PR on top of DI 1 |
||
injector.getByKey(KEY_B); | ||
|
||
var childInjector = injector.createChild([module]); | ||
childInjector.get(A); | ||
childInjector.get(B); | ||
var childInjector = new ModuleInjector([module], injector); | ||
childInjector.getByKey(KEY_A); | ||
childInjector.getByKey(KEY_B); | ||
} | ||
|
||
setup() { | ||
module = new Module() | ||
..type(A) | ||
..type(B) | ||
..type(C) | ||
..type(C, withAnnotation: AnnOne, implementedBy: COne ) | ||
..type(D) | ||
..type(E) | ||
..type(E, withAnnotation: AnnTwo, implementedBy: ETwo ) | ||
..type(F) | ||
..type(G); | ||
module = new Module.withReflector(typeReflector) | ||
..bind(A) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move these benchmark changes to a PR on top of DI 1 |
||
..bind(B) | ||
..bind(C) | ||
..bind(C, withAnnotation: AnnOne, toImplementation: COne ) | ||
..bind(D) | ||
..bind(E) | ||
..bind(E, withAnnotation: AnnTwo, toImplementation: ETwo ) | ||
..bind(F) | ||
..bind(G); | ||
|
||
KEY_A = new Key(A); | ||
KEY_B = new Key(B); | ||
KEY_C = new Key(C); | ||
KEY_D = new Key(D); | ||
KEY_E = new Key(E); | ||
} | ||
|
||
teardown() { | ||
|
@@ -96,7 +107,31 @@ class F { | |
} | ||
|
||
class G { | ||
G(@AnnTwo() E) { | ||
G(@AnnTwo() E e) { | ||
count++; | ||
} | ||
} | ||
|
||
var typeFactories = { | ||
A: (a1, a2) => new A(a1, a2), | ||
B: (a1, a2) => new B(a1, a2), | ||
C: () => new C(), | ||
D: () => new D(), | ||
E: () => new E(), | ||
COne: () => new COne(), | ||
ETwo: () => new ETwo(), | ||
F: (a1, a2) => new F(a1, a2), | ||
G: (a1) => new G(a1), | ||
}; | ||
|
||
var paramKeys = { | ||
A: [new Key(B), new Key(C)], | ||
B: [new Key(D), new Key(E)], | ||
C: const [], | ||
D: const [], | ||
E: const [], | ||
COne: const [], | ||
ETwo: const [], | ||
F: [new Key(C, AnnOne), new Key(D)], | ||
G: [new Key(G, AnnTwo)], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,22 @@ | ||
import 'package:di/static_injector.dart'; | ||
import 'package:di/di.dart'; | ||
import 'package:di/src/reflector_static.dart'; | ||
|
||
import 'injector_benchmark_common.dart'; | ||
|
||
// tests the speed of cached getInstanceByKey requests | ||
class InstanceBenchmark extends InjectorBenchmark{ | ||
InstanceBenchmark(name, injectorFactory) : super(name, injectorFactory); | ||
InstanceBenchmark(name, typeReflector) : super(name, typeReflector); | ||
|
||
void run(){ | ||
Injector injector = injectorFactory([module]); | ||
Injector injector = new ModuleInjector([module]); | ||
for (var i = 0; i < 30; i++) { | ||
injector.get(A); | ||
} | ||
} | ||
} | ||
|
||
main() { | ||
var typeFactories = { | ||
A: (f) => new A(f(B), f(C)), | ||
B: (f) => new B(f(D), f(E)), | ||
C: (f) => new C(), | ||
D: (f) => new D(), | ||
E: (f) => new E(), | ||
}; | ||
|
||
new InstanceBenchmark('InstanceBenchmark', | ||
(m) => new StaticInjector(modules: m, typeFactories: typeFactories) | ||
new GeneratedTypeFactories(typeFactories, paramKeys) | ||
).report(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:benchmark_harness/benchmark_harness.dart'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create a PR to add this benchmark to DI 1 (this PR will still need changes to upgrade it to DI 2) |
||
import 'package:di/di.dart'; | ||
import 'package:di/src/reflector_static.dart'; | ||
import 'generated_files/factories.dart'; | ||
|
||
import 'dart:math'; | ||
|
||
class LargeBenchmark extends BenchmarkBase { | ||
var injectorFactory; | ||
Injector rootInjector; | ||
Injector leafInjector; | ||
var leafKey; | ||
var rng = new Random(); | ||
var numInjectors = 1; | ||
var allLeaves = []; | ||
|
||
LargeBenchmark(name, this.injectorFactory) : super("Large" + name); | ||
|
||
setup() { | ||
var rootModule = new Module() | ||
..bindByKey(key999) | ||
..bindByKey(key998) | ||
..bindByKey(key997) | ||
..bindByKey(key996) | ||
..bindByKey(key995); | ||
rootInjector = injectorFactory([rootModule]); | ||
|
||
createChildren (injector, depth, width) { | ||
if (depth <= 0){ | ||
allLeaves.add(injector); | ||
return; | ||
} | ||
for (var i=0; i<width; i++){ | ||
var module = new Module(); | ||
for (var j=0; j<5; j++){ | ||
leafKey = allKeys[rng.nextInt(995)]; | ||
module.bindByKey(leafKey); | ||
} | ||
leafInjector = injector.createChild([module]); | ||
numInjectors++; | ||
createChildren(leafInjector, depth-1, width); | ||
} | ||
} | ||
|
||
createChildren(rootInjector, 5, 5); | ||
print("$numInjectors injectors created."); | ||
} | ||
} | ||
|
||
class GetFromRoot extends LargeBenchmark { | ||
GetFromRoot() : super('FromRoot', (m) => new ModuleInjector(m)); | ||
|
||
run() { | ||
leafInjector.getByKey(key999); | ||
} | ||
} | ||
|
||
class GetFromLeaf extends LargeBenchmark { | ||
GetFromLeaf() : super('FromLeaf', (m) => new ModuleInjector(m)); | ||
|
||
run() { | ||
leafInjector.getByKey(leafKey); | ||
} | ||
} | ||
|
||
main() { | ||
Module.DEFAULT_REFLECTOR = new GeneratedTypeFactories(typeFactories, parameterKeys); | ||
new GetFromRoot().report(); | ||
new GetFromLeaf().report(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,21 +3,26 @@ import 'package:di/di.dart'; | |
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the changes in this file to a DI 1 PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
import 'injector_benchmark_common.dart'; | ||
|
||
/** | ||
* tests the speed of looking up typeFactories and binding them to | ||
* a module. Mirroring time is not counted because DynamicTypeFactories | ||
* caches the results. | ||
*/ | ||
class ModuleBenchmark extends BenchmarkBase { | ||
var injectorFactory; | ||
|
||
ModuleBenchmark() : super('ModuleBenchmark'); | ||
|
||
void run() { | ||
var m = new Module() | ||
..type(A) | ||
..type(B) | ||
..type(C) | ||
..type(D) | ||
..type(E); | ||
..bind(A) | ||
..bind(B) | ||
..bind(C) | ||
..bind(D) | ||
..bind(E); | ||
} | ||
} | ||
|
||
main() { | ||
new ModuleBenchmark().report(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ModuleInjector
still has a name field - one of the two places needs to be fixed