-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRoot.tsx
302 lines (266 loc) · 7.44 KB
/
Root.tsx
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import * as dapper from '@convoy/dapper';
import benchmarks from '../../../benchmarks';
import clients from '../../../clients';
import examples from '../../../examples';
import { Summary, SuiteSummaryCollector } from '../../reporting';
import { runSuite, SuitePromise } from '../../execution';
import { SuiteSummary } from './SuiteSummary';
import { RawExample } from '../../Example';
import { ExampleEditor } from './ExampleEditor';
// Update at a lowish framerate to give priority to benchmarks.
const UPDATE_DELAY_MS = 50;
enum RunState {
PENDING,
RUNNING,
CANCELED,
COMPLETE,
}
interface RootState {
runState: RunState;
editing: boolean;
examples: RawExample[];
exampleIndex: number;
}
const MODES: { [key: string]: (args: { state?: RootState }) => boolean } = {
pending: ({ state }) => !state || state.runState === RunState.PENDING,
running: ({ state }) => state && state.runState === RunState.RUNNING,
canceled: ({ state }) => state && state.runState === RunState.CANCELED,
complete: ({ state }) => state && state.runState === RunState.COMPLETE,
idle: ({ state }) => !state || state.runState !== RunState.RUNNING,
};
const STYLES = dapper.compile({
global: {
fontFamily: `"Proxima Nova", ProximaNova, "Helvetica Neue", Helvetica, sans-serif`,
fontSize: 16,
lineHeight: '22px',
},
root: {
border: `3px solid #15455a`,
backgroundColor: '#0b2127',
color: '#ffffff',
},
header: {
display: 'flex',
background: 'linear-gradient(to bottom, #15455a, #0b2127)',
color: '#ffffff',
padding: 10,
fontSize: 20,
lineHeight: '28px',
'button, select': {
fontSize: 'inherit',
lineHeight: 'inherit',
backgroundColor: 'transparent',
color: '#ffffff',
},
},
status: {
$running: {
color: '#5b666b',
},
$complete: {
color: '#4aa54f',
},
$canceled: {
color: '#f8cc1c',
},
},
about: {
flex: 1,
},
controls: {
display: 'flex',
flexDirection: 'column',
alignSelf: 'stretch',
alignItems: 'flex-end',
},
buttons: {
button: {
marginLeft: 14,
},
},
exampleSelectorContainer: {
flex: 1,
marginBottom: 14,
},
title: {
fontSize: 24,
lineHeight: '34px',
},
toggle: {
$idle: {
backgroundColor: '#f7f9fa',
color: '#125571',
},
},
summary: {
$canceled: {
opacity: 0.5,
},
},
});
export class Root extends React.PureComponent<{}, RootState> {
private _suitePromise: SuitePromise;
// We collect summary updates as they come in…
private _collector?: SuiteSummaryCollector;
private _summary?: Summary;
// …and display those at a slower framerate
private _updateTimer: any;
styles = dapper.reactTo(this, STYLES, MODES);
state = {
runState: RunState.PENDING,
editing: false,
examples,
exampleIndex: 0,
};
componentDidMount() {
if (module.hot) {
module.hot.dispose(() => {
this._suitePromise.cancel();
});
}
}
render() {
if (this._updateTimer) {
clearTimeout(this._updateTimer);
this._updateTimer = null;
}
return (
<div className={this.styles.global}>
<div className={this.styles.root}>
{this._renderHeader()}
{!!this._summary && <SuiteSummary {...this._summary} className={this.styles.summary} />}
</div>
{this._maybeRenderEditor()}
</div>
);
}
private _renderHeader() {
const { runState } = this.state;
return (
<header className={this.styles.header}>
<div className={this.styles.about}>
<div className={this.styles.title}>GraphQL Client Benchmark Suite</div>
<div className={this.styles.status}>{this._statusLabel()}</div>
</div>
<div className={this.styles.controls}>
<div className={this.styles.exampleSelectorContainer}>
Example Query: {this._renderExampleSelector()}
</div>
<div className={this.styles.buttons}>
<button onClick={this._onEditCurrentExample}>Edit</button>
<button onClick={this._onToggleSuite} className={this.styles.toggle}>
{runState === RunState.RUNNING ? 'Stop' : 'Start'}
</button>
</div>
</div>
</header>
);
}
private _statusLabel() {
const { runState } = this.state;
if (runState === RunState.RUNNING) {
return 'running…';
} else if (runState === RunState.CANCELED) {
return 'canceled';
} else if (runState === RunState.COMPLETE) {
return 'benchmark suite complete';
} else {
return null;
}
}
private _renderExampleSelector() {
const { examples, exampleIndex } = this.state;
return (
<select className={this.styles.exampleSelector} value={exampleIndex} onChange={this._onChangeExample}>
{examples.map((example, index) => (
<option value={index} key={index}>
{example.title}
</option>
))}
<option value={examples.length}>Add Query…</option>
</select>
);
}
private _maybeRenderEditor() {
const { editing, examples, exampleIndex } = this.state;
if (!editing) return null;
const example: RawExample = examples[exampleIndex] || {
title: `New Example`,
operation: '',
schema: '',
response: {},
partials: [],
};
return (
<ExampleEditor
example={example}
onChange={this._onCurrentExampleEdited}
onCancel={this._onEditorCanceled}
/>
);
}
start() {
const { examples, exampleIndex } = this.state;
if (this._suitePromise) {
// Cancel, wait for it, and have the user press again.
this.stop();
return;
}
this._collector = new SuiteSummaryCollector(
summary => {
this._summary = summary;
if (this._updateTimer) return;
this._updateTimer = setTimeout(() => this.forceUpdate(), UPDATE_DELAY_MS);
},
({ benchmark, client, phase, error }) => {
console.error(`Error during ${phase} of`, { benchmark, client }, error);
},
);
const example = examples[exampleIndex];
this._suitePromise = runSuite(this._collector.consumeEvent, benchmarks, clients, example);
this._suitePromise.then(canceled => {
this.setState({
runState: canceled ? RunState.CANCELED : RunState.COMPLETE,
});
this._suitePromise = null;
});
this.setState({
runState: RunState.RUNNING,
});
}
stop() {
if (!this._suitePromise) return;
this._suitePromise.cancel();
}
private _onToggleSuite = () => {
const { runState } = this.state;
if (runState === RunState.RUNNING) {
this.stop();
} else {
this.start();
}
};
private _onEditCurrentExample = () => {
this.setState({ editing: true });
};
private _onCurrentExampleEdited = (newExample: RawExample) => {
const { examples, exampleIndex } = this.state;
const newExamples = [...examples];
newExamples[exampleIndex] = newExample;
this.setState({ examples: newExamples, editing: false });
};
private _onEditorCanceled = () => {
this.setState({ editing: false });
};
private _onChangeExample = event => {
const { examples } = this.state;
this.stop(); // Always.
const exampleIndex = parseInt(event.target.value);
// Are we adding a new one?
if (exampleIndex >= examples.length) {
this.setState({ exampleIndex, editing: true });
} else {
this.setState({ exampleIndex });
}
};
}