perf(benchmarks): benchmark that measure cost of dynamic components

This commit is contained in:
Yegor Jbanov 2015-04-26 12:15:42 -07:00
parent 9fc9d53566
commit 427f0d021c
3 changed files with 48 additions and 4 deletions

View File

@ -3,6 +3,8 @@ var perfUtil = require('angular2/src/test_lib/perf_util');
describe('ng2 cost benchmark', function () { describe('ng2 cost benchmark', function () {
var URL = 'benchmarks/src/costs/index.html'; var URL = 'benchmarks/src/costs/index.html';
// Number of components to create in a single iteration
var benchmarkSize = 200; var benchmarkSize = 200;
afterEach(perfUtil.verifyNoBrowserErrors); afterEach(perfUtil.verifyNoBrowserErrors);
@ -13,7 +15,7 @@ describe('ng2 cost benchmark', function () {
buttons: ['#reset', '#createPlainComponents'], buttons: ['#reset', '#createPlainComponents'],
id: 'ng2.costs.baseline', id: 'ng2.costs.baseline',
params: [{ params: [{
name: 'size', value: 200, scale: 'linear' name: 'size', value: benchmarkSize, scale: 'linear'
}] }]
}).then(done, done.fail); }).then(done, done.fail);
}); });
@ -24,7 +26,18 @@ describe('ng2 cost benchmark', function () {
buttons: ['#reset', '#createComponentsWithDecorators'], buttons: ['#reset', '#createComponentsWithDecorators'],
id: 'ng2.costs.decorators', id: 'ng2.costs.decorators',
params: [{ params: [{
name: 'size', value: 200, scale: 'linear' name: 'size', value: benchmarkSize, scale: 'linear'
}]
}).then(done, done.fail);
});
it('should log stats for dynamic components', function(done) {
perfUtil.runClickBenchmark({
url: URL,
buttons: ['#reset', '#createDynamicComponents'],
id: 'ng2.costs.dynamic',
params: [{
name: 'size', value: benchmarkSize, scale: 'linear'
}] }]
}).then(done, done.fail); }).then(done, done.fail);
}); });

View File

@ -21,6 +21,10 @@
<td>Cost of decorators</td> <td>Cost of decorators</td>
<td><button id="createComponentsWithDecorators">Run</button></td> <td><button id="createComponentsWithDecorators">Run</button></td>
</tr> </tr>
<tr>
<td>Cost of dynamic components</td>
<td><button id="createDynamicComponents">Run</button></td>
</tr>
</table> </table>
<app></app> <app></app>

View File

@ -2,7 +2,10 @@ import {
bootstrap, bootstrap,
Component, Component,
Decorator, Decorator,
View View,
DynamicComponentLoader,
ElementRef,
DynamicComponent
} from 'angular2/angular2'; } from 'angular2/angular2';
import {LifeCycle} from 'angular2/src/core/life_cycle/life_cycle'; import {LifeCycle} from 'angular2/src/core/life_cycle/life_cycle';
import {List, ListWrapper} from 'angular2/src/facade/collection'; import {List, ListWrapper} from 'angular2/src/facade/collection';
@ -39,12 +42,18 @@ export function main() {
app.createComponentsWithDecorators(); app.createComponentsWithDecorators();
lifeCycle.tick(); lifeCycle.tick();
}); });
// Components with decorators
bindAction('#createDynamicComponents', function() {
app.createDynamicComponents();
lifeCycle.tick();
});
}); });
} }
@Component({selector: 'app'}) @Component({selector: 'app'})
@View({ @View({
directives: [If, For, DummyComponent, DummyDecorator], directives: [If, For, DummyComponent, DummyDecorator, DynamicDummy],
template: ` template: `
<div *if="testingPlainComponents"> <div *if="testingPlainComponents">
<dummy *for="#i of list"></dummy> <dummy *for="#i of list"></dummy>
@ -53,12 +62,17 @@ export function main() {
<div *if="testingWithDecorators"> <div *if="testingWithDecorators">
<dummy dummy-decorator *for="#i of list"></dummy> <dummy dummy-decorator *for="#i of list"></dummy>
</div> </div>
<div *if="testingDynamicComponents">
<dynamic-dummy *for="#i of list"></dynamic-dummy>
</div>
` `
}) })
class AppComponent { class AppComponent {
list:List; list:List;
testingPlainComponents:boolean; testingPlainComponents:boolean;
testingWithDecorators:boolean; testingWithDecorators:boolean;
testingDynamicComponents:boolean;
constructor() { constructor() {
this.reset(); this.reset();
@ -68,6 +82,7 @@ class AppComponent {
this.list = []; this.list = [];
this.testingPlainComponents = false; this.testingPlainComponents = false;
this.testingWithDecorators = false; this.testingWithDecorators = false;
this.testingDynamicComponents = false;
} }
createPlainComponents():void { createPlainComponents():void {
@ -79,6 +94,11 @@ class AppComponent {
this.list = testList; this.list = testList;
this.testingWithDecorators = true; this.testingWithDecorators = true;
} }
createDynamicComponents():void {
this.list = testList;
this.testingDynamicComponents = true;
}
} }
@Component({selector: 'dummy'}) @Component({selector: 'dummy'})
@ -87,3 +107,10 @@ class DummyComponent {}
@Decorator({selector: '[dummy-decorator]'}) @Decorator({selector: '[dummy-decorator]'})
class DummyDecorator {} class DummyDecorator {}
@DynamicComponent({selector: 'dynamic-dummy'})
class DynamicDummy {
constructor(loader:DynamicComponentLoader, location:ElementRef) {
loader.loadIntoExistingLocation(DummyComponent, location);
}
}