-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathdeveloper-mode.mjs
288 lines (244 loc) · 9.26 KB
/
developer-mode.mjs
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
import { Suites, Tags } from "./tests.mjs";
import { params, defaultParams } from "./shared/params.mjs";
export function createDeveloperModeContainer() {
const container = document.createElement("div");
container.className = "developer-mode";
const details = document.createElement("details");
const summary = document.createElement("summary");
summary.textContent = "Developer Mode";
details.append(summary);
const content = document.createElement("div");
content.className = "developer-mode-content";
content.append(createUIForSuites());
const settings = document.createElement("div");
settings.className = "settings";
settings.append(createUIForIterationCount());
settings.append(createUIForWarmupSuite());
settings.append(createUIForWarmupBeforeSync());
settings.append(createUIForSyncStepDelay());
settings.append(createUIForAsyncSteps());
content.append(document.createElement("hr"));
content.append(settings);
content.append(document.createElement("hr"));
content.append(createUIForRun());
details.append(content);
container.append(details);
return container;
}
function span(text) {
const span = document.createElement("span");
span.textContent = text;
return span;
}
function createUIForWarmupSuite() {
return createCheckboxUI("Use Warmup Suite", params.useWarmupSuite, (isChecked) => {
params.useWarmupSuite = isChecked;
});
}
function createUIForAsyncSteps() {
return createCheckboxUI("Use Async Steps", params.useAsyncSteps, (isChecked) => {
params.useAsyncSteps = isChecked;
});
}
function createCheckboxUI(labelValue, initialValue, paramsUpdateCallback) {
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = !!initialValue;
checkbox.onchange = () => {
paramsUpdateCallback(checkbox.checked);
updateURL();
};
const label = document.createElement("label");
label.append(checkbox, " ", span(labelValue));
return label;
}
function createUIForIterationCount() {
return createTimeRangeUI("Iterations: ", "iterationCount", "#", 1, 200);
}
function createUIForWarmupBeforeSync() {
return createTimeRangeUI("Warmup time: ", "warmupBeforeSync");
}
function createUIForSyncStepDelay() {
return createTimeRangeUI("Sync step delay: ", "waitBeforeSync");
}
function createTimeRangeUI(labelText, paramKey, unit = "ms", min = 0, max = 1000) {
const range = document.createElement("input");
range.type = "range";
range.min = min;
range.max = max;
range.value = params[paramKey];
const rangeValueAndUnit = document.createElement("span");
rangeValueAndUnit.className = "range-label-data";
const rangeValue = document.createElement("span");
rangeValue.textContent = params[paramKey];
rangeValueAndUnit.append(rangeValue, " ", unit);
const label = document.createElement("label");
label.append(span(labelText), range, rangeValueAndUnit);
range.oninput = () => {
rangeValue.textContent = range.value;
};
range.onchange = () => {
params[paramKey] = parseInt(range.value);
updateURL();
};
return label;
}
function createUIForSuites() {
const control = document.createElement("nav");
control.className = "suites";
const checkboxes = [];
const setSuiteEnabled = (suiteIndex, enabled) => {
Suites[suiteIndex].disabled = !enabled;
checkboxes[suiteIndex].checked = enabled;
};
control.appendChild(createSuitesGlobalSelectButtons(setSuiteEnabled));
const ol = document.createElement("ol");
for (const suite of Suites) {
const li = document.createElement("li");
const checkbox = document.createElement("input");
checkbox.id = suite.name;
checkbox.type = "checkbox";
checkbox.checked = !suite.disabled;
checkbox.onchange = () => {
suite.disabled = !checkbox.checked;
updateURL();
};
checkboxes.push(checkbox);
const label = document.createElement("label");
label.append(checkbox, " ", suite.name);
li.appendChild(label);
label.onclick = (event) => {
if (event?.ctrlKey || event?.metaKey) {
for (let suiteIndex = 0; suiteIndex < Suites.length; suiteIndex++) {
if (Suites[suiteIndex] !== suite)
setSuiteEnabled(suiteIndex, false);
else
setSuiteEnabled(suiteIndex, true);
}
}
};
ol.appendChild(li);
}
control.appendChild(ol);
control.appendChild(createSuitesTagsButton(setSuiteEnabled));
return control;
}
function createSuitesGlobalSelectButtons(setSuiteEnabled) {
const buttons = document.createElement("div");
buttons.className = "button-bar";
let button = document.createElement("button");
button.className = "select-all";
button.textContent = "Select all";
button.onclick = () => {
for (let suiteIndex = 0; suiteIndex < Suites.length; suiteIndex++)
setSuiteEnabled(suiteIndex, true);
updateURL();
};
buttons.appendChild(button);
button = document.createElement("button");
button.textContent = "Unselect all";
button.className = "unselect-all";
button.onclick = () => {
for (let suiteIndex = 0; suiteIndex < Suites.length; suiteIndex++)
setSuiteEnabled(suiteIndex, false);
updateURL();
};
buttons.appendChild(button);
return buttons;
}
function createSuitesTagsButton(setSuiteEnabled) {
let tags = document.createElement("div");
let buttons = tags.appendChild(document.createElement("div"));
buttons.className = "button-bar";
let i = 0;
const kTagsPerLine = 3;
for (const tag of Tags) {
if (tag === "all")
continue;
if (!(i % kTagsPerLine)) {
buttons = tags.appendChild(document.createElement("div"));
buttons.className = "button-bar";
}
i++;
const button = document.createElement("button");
button.className = "tag";
button.textContent = `#${tag}`;
button.dataTag = tag;
button.onclick = (event) => {
const extendSelection = event?.shiftKey;
const invertSelection = event?.ctrlKey || event?.metaKey;
const selectedTag = event.target.dataTag;
for (let suiteIndex = 0; suiteIndex < Suites.length; suiteIndex++) {
let enabled = Suites[suiteIndex].tags.includes(selectedTag);
if (invertSelection)
enabled = !enabled;
if (extendSelection && !enabled)
continue;
setSuiteEnabled(suiteIndex, enabled);
}
updateURL();
};
buttons.appendChild(button);
}
return tags;
}
function createUIForRun() {
const stepTestButton = document.createElement("button");
stepTestButton.textContent = "Step Test \u23EF";
stepTestButton.onclick = (event) => {
globalThis.benchmarkClient.step();
};
const startTestButton = document.createElement("button");
startTestButton.textContent = "Start Test \u23F5";
startTestButton.onclick = (event) => {
globalThis.benchmarkClient.start();
};
const buttons = document.createElement("div");
buttons.className = "button-bar";
buttons.appendChild(stepTestButton);
buttons.appendChild(startTestButton);
return buttons;
}
function updateParamsSuitesAndTags() {
params.suites = [];
params.tags = [];
// If less than all suites are selected then change the URL "Suites" GET parameter
// to comma separate only the selected
const selectedSuites = Suites.filter((suite) => !suite.disabled);
if (!selectedSuites.length)
return;
// Try finding common tags that would result in the current suite selection.
let commonTags = new Set(selectedSuites[0].tags);
for (const suite of Suites) {
if (suite.disabled)
suite.tags.forEach((tag) => commonTags.delete(tag));
else
commonTags = new Set(suite.tags.filter((tag) => commonTags.has(tag)));
}
if (selectedSuites.length > 1 && commonTags.size)
params.tags = commonTags.has("default") ? [] : [...commonTags];
else
params.suites = selectedSuites.map((suite) => suite.name);
}
function updateURL() {
updateParamsSuitesAndTags();
const url = new URL(window.location.href);
url.searchParams.delete("tags");
url.searchParams.delete("suites");
url.searchParams.delete("suite");
if (params.tags.length)
url.searchParams.set("tags", params.tags.join(","));
else if (params.suites.length)
url.searchParams.set("suites", params.suites.join(","));
const defaultParamKeys = ["iterationCount", "useWarmupSuite", "warmupBeforeSync", "waitBeforeSync", "useAsyncSteps"];
for (const paramKey of defaultParamKeys) {
if (params[paramKey] !== defaultParams[paramKey])
url.searchParams.set(paramKey, params[paramKey]);
else
url.searchParams.delete(paramKey);
}
// Only push state if changed
url.search = decodeURIComponent(url.search);
if (url.href !== window.location.href)
window.history.pushState({}, "", url);
}