perf(benchmarks): measure cost of Injector init with a variety of bindings
This commit is contained in:
parent
8499cf84c3
commit
0012caa4d5
|
@ -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);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -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$
|
||||||
|
|
|
@ -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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue