-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathparams.mjs
More file actions
165 lines (149 loc) · 6.99 KB
/
params.mjs
File metadata and controls
165 lines (149 loc) · 6.99 KB
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
class Params {
viewport = {
width: 800,
height: 600,
};
// Enable a detailed developer menu to change the current Params.
developerMode = false;
startAutomatically = false;
iterationCount = 10;
suites = [];
// A list of tags to filter suites
tags = [];
// Toggle running a dummy suite once before the normal test suites.
useWarmupSuite = false;
// Change how a test measurement is triggered and async time is measured:
// "timer": The classic (as in Speedometer 2.x) way using setTimeout
// "raf": Using rAF callbacks, both for triggering the sync part and for measuring async time.
measurementMethod = "raf"; // or "timer"
// Wait time before the sync step in ms.
waitBeforeSync = 0;
// Warmup time before the sync step in ms.
warmupBeforeSync = 0;
// Seed for shuffling the execution order of suites.
// "off": do not shuffle
// "generate": generate a random seed
// <integer>: use the provided integer as a seed
shuffleSeed = "off";
// Param to tweak the relative complexity of all suites.
// The default is 1.0, and for suites supporting this param, the duration
// roughly scales wit the complexity.
complexity = 1.0;
constructor(searchParams = undefined) {
if (searchParams)
this._copyFromSearchParams(searchParams);
if (!this.developerMode) {
Object.freeze(this.viewport);
Object.freeze(this);
}
}
_parseNumber(value, errorMessage) {
const number = Number(value);
if (!Number.isFinite(number) && errorMessage)
throw new Error(`Invalid ${errorMessage} param: '${value}', expected Number.`);
return number;
}
_parseInt(value, errorMessage) {
const number = this._parseNumber(value);
if (!Number.isInteger(number) && errorMessage)
throw new Error(`Invalid ${errorMessage} param: '${value}', expected int.`);
return parseInt(number);
}
_copyFromSearchParams(searchParams) {
if (searchParams.has("viewport")) {
const viewportParam = searchParams.get("viewport");
const [width, height] = viewportParam.split("x");
this.viewport.width = this._parseInt(width, "viewport.width");
this.viewport.height = this._parseInt(height, "viewport.height");
if (this.viewport.width < 800 || this.viewport.height < 600)
throw new Error(`Invalid viewport param: ${viewportParam}`);
searchParams.delete("viewport");
}
this.startAutomatically = searchParams.has("startAutomatically");
searchParams.delete("startAutomatically");
if (searchParams.has("iterationCount")) {
this.iterationCount = this._parseInt(searchParams.get("iterationCount"), "iterationCount");
if (this.iterationCount < 1)
throw new Error(`Invalid iterationCount param: '${this.iterationCount}', must be > 1.`);
searchParams.delete("iterationCount");
}
if (searchParams.has("suite") || searchParams.has("suites")) {
if (searchParams.has("suite") && searchParams.has("suites"))
throw new Error("Params 'suite' and 'suites' can not be used together.");
const value = searchParams.get("suite") || searchParams.get("suites");
this.suites = value.split(",");
if (this.suites.length === 0)
throw new Error("No suites selected");
searchParams.delete("suite");
searchParams.delete("suites");
}
if (searchParams.has("tags")) {
if (this.suites.length)
throw new Error("'suites' and 'tags' cannot be used together.");
this.tags = searchParams.get("tags").split(",");
searchParams.delete("tags");
}
this.developerMode = searchParams.has("developerMode");
searchParams.delete("developerMode");
if (searchParams.has("useWarmupSuite")) {
this.useWarmupSuite = true;
searchParams.delete("useWarmupSuite");
}
if (searchParams.has("waitBeforeSync")) {
this.waitBeforeSync = this._parseInt(searchParams.get("waitBeforeSync"), "waitBeforeSync");
if (this.waitBeforeSync < 0)
throw new Error(`Invalid waitBeforeSync param: '${this.waitBeforeSync}', must be >= 0.`);
searchParams.delete("waitBeforeSync");
}
if (searchParams.has("warmupBeforeSync")) {
this.warmupBeforeSync = this._parseInt(searchParams.get("warmupBeforeSync"), "warmupBeforeSync");
if (this.warmupBeforeSync < 0)
throw new Error(`Invalid warmupBeforeSync param: '${this.warmupBeforeSync}', must be >= 0.`);
searchParams.delete("warmupBeforeSync");
}
if (searchParams.has("measurementMethod")) {
this.measurementMethod = searchParams.get("measurementMethod");
if (this.measurementMethod !== "timer" && this.measurementMethod !== "raf")
throw new Error(`Invalid measurement method: '${this.measurementMethod}', must be either 'raf' or 'timer'.`);
searchParams.delete("measurementMethod");
}
if (searchParams.has("shuffleSeed")) {
this.shuffleSeed = searchParams.get("shuffleSeed");
if (this.shuffleSeed !== "off") {
if (this.shuffleSeed === "generate") {
this.shuffleSeed = Math.floor((Math.random() * 1) << 16);
console.log(`Generated a random suite order seed: ${this.shuffleSeed}`);
} else {
this.shuffleSeed = parseInt(this.shuffleSeed);
}
if (!Number.isInteger(this.shuffleSeed))
throw new Error(`Invalid shuffle seed: '${this.shuffleSeed}', must be either 'off', 'generate' or an integer.`);
}
searchParams.delete("shuffleSeed");
}
if (searchParams.has("complexity")) {
this.complexity = this._parseNumber(searchParams.get("complexity"));
if (this.complexity <= 0)
throw new Error(`Invalid complexity value: ${this.complexity}, must be > 0.0`);
searchParams.delete("complexity");
}
const unused = Array.from(searchParams.keys());
if (unused.length > 0)
console.error("Got unused search params", unused);
}
toSearchParams() {
const rawParams = { ...this };
rawParams["viewport"] = `${this.viewport.width}x${this.viewport.height}`;
return new URLSearchParams(rawParams).toString();
}
}
export const defaultParams = new Params();
const searchParams = new URLSearchParams(window.location.search);
let maybeCustomParams = new Params();
try {
maybeCustomParams = new Params(searchParams);
} catch (e) {
console.error("Invalid URL Param", e, "\nUsing defaults as fallback:", maybeCustomParams);
alert(`Invalid URL Param: ${e}`);
}
export const params = maybeCustomParams;