forked from discourse/ember-performance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.gts
128 lines (103 loc) · 3.17 KB
/
render.gts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
// Isn't exposed until Ember v5 something
// import { renderSettled } from '@ember/renderer';
import { renderSettled } from '@ember/-internals/glimmer';
import { assert } from '@ember/debug';
import { inject as service } from '@ember/service';
import { use } from 'ember-resources';
import { OneOffTinyBench, type Options } from './bench.ts';
import { Layout } from './layout.gts';
import { store } from './storage.ts';
import { onRenderIsh } from './utils.ts';
import type ForAppVersion from '../services/runner/for-app-at-version.ts';
/**
* renderSettled doesn't exist in earlier versions of ember.
*/
// async function renderSettled() {
// await new Promise((resolve) => schedule('afterRender', resolve));
// await Promise.resolve();
// }
export class RenderBenchmark extends Component<{
Args: {
name: string;
} & Omit<Options, 'test'>;
Blocks: {
default: [];
};
}> {
@service('runner/for-app-at-version') declare forApp: ForAppVersion;
@tracked status = '';
@tracked remaining = '';
@tracked progress = '';
@tracked showContents = false;
#continue?: (x?: unknown) => void;
@use bench = OneOffTinyBench(() => {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let renderComponent = this;
return {
options: {
...this.args,
setup: async () => {
if ('gc' in globalThis) {
// eslint-disable-next-line no-undef
gc();
}
},
beforeEach: async () => {
renderComponent.showContents = false;
await renderSettled();
await new Promise((resolve) => requestAnimationFrame(resolve));
},
test: async () => {
assert(
`showContents must be false. It was: ${renderComponent.showContents}`,
false === renderComponent.showContents
);
renderComponent.showContents = true;
await renderSettled();
},
},
updateStatus: (msg: string) => (this.status = msg),
};
});
isRunning = false;
run = async () => {
if (this.isRunning) {
console.info('Already running');
return;
}
this.isRunning = true;
let bench = this.bench;
let version = this.forApp.emberVersion;
let altName = this.forApp.name;
this.status = 'Warming up...';
this.showContents = false;
requestAnimationFrame(async () => {
await bench.warmup();
this.status = 'Running...';
requestAnimationFrame(async () => {
await bench.run();
let result = bench.results[0];
if (result?.error) {
throw result.error;
}
store(this.args.name, version, altName, result);
window.top.postMessage('finish');
});
});
};
<template>
{{onRenderIsh this.run}}
<Layout @name={{@name}} @version={{this.forApp.emberVersion}}>
<:remaining>{{this.remaining}}</:remaining>
<:status>{{this.status}}</:status>
<:progress>{{this.progress}}</:progress>
<:scratch>
{{#if this.showContents}}
{{yield}}
{{/if}}
</:scratch>
</Layout>
</template>
}