perf(benchmarks): measure cost of Injector init with a variety of bindings

This commit is contained in:
Yegor Jbanov 2015-04-11 14:03:03 -07:00
parent 8499cf84c3
commit 0012caa4d5
3 changed files with 50 additions and 4 deletions

View File

@ -62,4 +62,22 @@ describe('ng2 di benchmark', function () {
}).then(done, done.fail); }).then(done, done.fail);
}); });
/**
* This benchmark measures the cost of creating a new injector with a mix
* of binding types: Type, unresolved, unflattened.
*/
it('should log the stats for createVariety', function(done) {
perfUtil.runClickBenchmark({
url: URL,
buttons: ['#createVariety'],
id: 'ng2.di.createVariety',
params: [{
name: 'iterations', value: 10000, scale: 'linear'
}],
microMetrics: {
'injectAvg': 'avg time for createVariety (in ms)'
}
}).then(done, done.fail);
});
}); });

View File

@ -16,6 +16,7 @@
<button id="getByKey">getByKey</button> <button id="getByKey">getByKey</button>
<button id="getChild">getChild</button> <button id="getChild">getChild</button>
<button id="instantiate">instantiate</button> <button id="instantiate">instantiate</button>
<button id="createVariety">createVariety</button>
</div> </div>
$SCRIPTS$ $SCRIPTS$

View File

@ -1,4 +1,4 @@
import {Injectable, Injector, Key} from "angular2/di"; import {Injectable, Injector, Key, bind} from "angular2/di";
import {reflector} from 'angular2/src/reflection/reflection'; import {reflector} from 'angular2/src/reflection/reflection';
import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities'; import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities';
import {getIntParameter, bindAction, microBenchmark} from 'angular2/src/test_lib/benchmark_util'; import {getIntParameter, bindAction, microBenchmark} from 'angular2/src/test_lib/benchmark_util';
@ -27,7 +27,14 @@ export function main() {
createChild([]). createChild([]).
createChild([]); createChild([]);
function getByToken () { var variousBindings = [
A,
bind(B).toClass(C),
[D, [E]],
bind(F).toValue(6)
];
function getByToken() {
for (var i = 0; i < iterations; ++i) { for (var i = 0; i < iterations; ++i) {
injector.get(D); injector.get(D);
injector.get(E); injector.get(E);
@ -40,20 +47,29 @@ export function main() {
} }
} }
function getChild () { function getChild() {
for (var i = 0; i < iterations; ++i) { for (var i = 0; i < iterations; ++i) {
childInjector.get(D); childInjector.get(D);
childInjector.get(E); childInjector.get(E);
} }
} }
function instantiate () { function instantiate() {
for (var i = 0; i < iterations; ++i) { for (var i = 0; i < iterations; ++i) {
var child = injector.createChild([E]); var child = injector.createChild([E]);
child.get(E); child.get(E);
} }
} }
/**
* Creates an injector with a variety of binding types.
*/
function createVariety() {
for (var i = 0; i < iterations; ++i) {
new Injector(variousBindings);
}
}
bindAction( bindAction(
'#getByToken', '#getByToken',
() => microBenchmark('injectAvg', iterations, getByToken) () => microBenchmark('injectAvg', iterations, getByToken)
@ -70,6 +86,10 @@ export function main() {
'#instantiate', '#instantiate',
() => microBenchmark('injectAvg', iterations, instantiate) () => microBenchmark('injectAvg', iterations, instantiate)
); );
bindAction(
'#createVariety',
() => microBenchmark('injectAvg', iterations, createVariety)
);
} }
@ -108,3 +128,10 @@ class E {
count++; count++;
} }
} }
@Injectable()
class F {
constructor(e:E, d:D) {
count++;
}
}