-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
benchmark.ts
91 lines (77 loc) 路 2.27 KB
/
benchmark.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {CommonModule} from '@angular/common';
import {AfterViewInit, Component, NgModule, ViewChild, ViewEncapsulation} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {BenchmarkModule} from './benchmark_module';
import {BenchmarkableExpandingRow} from './benchmarkable_expanding_row';
import {BenchmarkableExpandingRowModule} from './benchmarkable_expanding_row_module';
@Component({
selector: 'benchmark-root',
encapsulation: ViewEncapsulation.None,
template: `
<h2>cfc-expanding-row initialization benchmark</h2>
<section>
<button id="reset" (click)="reset()">Reset</button>
<button id="init" (click)="init()">Init</button>
<button id="run" (click)="runAll()">Run All</button>
</section>
<benchmark-area>
<benchmarkable-expanding-row></benchmarkable-expanding-row>
</benchmark-area>`,
})
export class InitializationRoot implements AfterViewInit {
@ViewChild(BenchmarkableExpandingRow, {static: true}) expandingRow!: BenchmarkableExpandingRow;
ngAfterViewInit() {}
reset() {
this.expandingRow.reset();
}
init() {
this.expandingRow.init();
}
async runAll() {
await execTimed('initialization_benchmark', async () => {
await this.doInit();
});
}
async handleInitClick() {
await this.doInit();
}
private async doInit() {
await execTimed('initial_load', async () => {
this.expandingRow.init();
});
}
}
@NgModule({
declarations: [InitializationRoot],
exports: [InitializationRoot],
imports: [
CommonModule,
BenchmarkableExpandingRowModule,
BenchmarkModule,
BrowserModule,
],
bootstrap: [InitializationRoot],
})
// Component benchmarks must export a BenchmarkModule.
export class ExpandingRowBenchmarkModule {
}
export async function execTimed(description: string, func: () => Promise<void>) {
console.time(description);
await func();
await nextTick(200);
console.timeEnd(description);
}
export async function nextTick(delay = 1) {
return new Promise((res, rej) => {
setTimeout(() => {
res();
}, delay);
});
}