X Tutup
Skip to content

Commit 0012caa

Browse files
committed
perf(benchmarks): measure cost of Injector init with a variety of bindings
1 parent 8499cf8 commit 0012caa

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

modules/benchmarks/e2e_test/di_perf.es6

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,22 @@ describe('ng2 di benchmark', function () {
6262
}).then(done, done.fail);
6363
});
6464

65+
/**
66+
* This benchmark measures the cost of creating a new injector with a mix
67+
* of binding types: Type, unresolved, unflattened.
68+
*/
69+
it('should log the stats for createVariety', function(done) {
70+
perfUtil.runClickBenchmark({
71+
url: URL,
72+
buttons: ['#createVariety'],
73+
id: 'ng2.di.createVariety',
74+
params: [{
75+
name: 'iterations', value: 10000, scale: 'linear'
76+
}],
77+
microMetrics: {
78+
'injectAvg': 'avg time for createVariety (in ms)'
79+
}
80+
}).then(done, done.fail);
81+
});
82+
6583
});

modules/benchmarks/src/di/di_benchmark.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ <h2>Actions</h2>
1616
<button id="getByKey">getByKey</button>
1717
<button id="getChild">getChild</button>
1818
<button id="instantiate">instantiate</button>
19+
<button id="createVariety">createVariety</button>
1920
</div>
2021

2122
$SCRIPTS$

modules/benchmarks/src/di/di_benchmark.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Injectable, Injector, Key} from "angular2/di";
1+
import {Injectable, Injector, Key, bind} from "angular2/di";
22
import {reflector} from 'angular2/src/reflection/reflection';
33
import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities';
44
import {getIntParameter, bindAction, microBenchmark} from 'angular2/src/test_lib/benchmark_util';
@@ -27,7 +27,14 @@ export function main() {
2727
createChild([]).
2828
createChild([]);
2929

30-
function getByToken () {
30+
var variousBindings = [
31+
A,
32+
bind(B).toClass(C),
33+
[D, [E]],
34+
bind(F).toValue(6)
35+
];
36+
37+
function getByToken() {
3138
for (var i = 0; i < iterations; ++i) {
3239
injector.get(D);
3340
injector.get(E);
@@ -40,20 +47,29 @@ export function main() {
4047
}
4148
}
4249

43-
function getChild () {
50+
function getChild() {
4451
for (var i = 0; i < iterations; ++i) {
4552
childInjector.get(D);
4653
childInjector.get(E);
4754
}
4855
}
4956

50-
function instantiate () {
57+
function instantiate() {
5158
for (var i = 0; i < iterations; ++i) {
5259
var child = injector.createChild([E]);
5360
child.get(E);
5461
}
5562
}
5663

64+
/**
65+
* Creates an injector with a variety of binding types.
66+
*/
67+
function createVariety() {
68+
for (var i = 0; i < iterations; ++i) {
69+
new Injector(variousBindings);
70+
}
71+
}
72+
5773
bindAction(
5874
'#getByToken',
5975
() => microBenchmark('injectAvg', iterations, getByToken)
@@ -70,6 +86,10 @@ export function main() {
7086
'#instantiate',
7187
() => microBenchmark('injectAvg', iterations, instantiate)
7288
);
89+
bindAction(
90+
'#createVariety',
91+
() => microBenchmark('injectAvg', iterations, createVariety)
92+
);
7393
}
7494

7595

@@ -108,3 +128,10 @@ class E {
108128
count++;
109129
}
110130
}
131+
132+
@Injectable()
133+
class F {
134+
constructor(e:E, d:D) {
135+
count++;
136+
}
137+
}

0 commit comments

Comments
 (0)
X Tutup