blob: 37c22a661d3998f224d448bab59d79543dca236d [file] [log] [blame] [edit]
"use strict";
/*
* Copyright (C) 2025 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
class Params {
// Enable a detailed developer menu to change the current Params.
developerMode = false;
startAutomatically = false;
shouldReport = false;
startDelay = undefined;
testList = [];
testIterationCount = undefined;
testWorstCaseCount = undefined;
prefetchResources = true;
RAMification = false;
dumpJSONResults = false;
testIterationCountMap = new Map();
testWorstCaseCountMap = new Map();
customPreIterationCode = undefined;
customPostIterationCode = undefined;
constructor(sourceParams = undefined) {
if (sourceParams)
this._copyFromSearchParams(sourceParams);
if (!this.developerMode)
Object.freeze(this);
}
_copyFromSearchParams(sourceParams) {
this.startAutomatically = this._parseBooleanParam(sourceParams, "startAutomatically");
this.developerMode = this._parseBooleanParam(sourceParams, "developerMode");
this.shouldReport = this._parseBooleanParam(sourceParams, "report");
this.prefetchResources = this._parseBooleanParam(sourceParams, "prefetchResources");
this.RAMification = this._parseBooleanParam(sourceParams, "RAMification");
this.dumpJSONResults = this._parseBooleanParam(sourceParams, "dumpJSONResults");
this.customPreIterationCode = this._parseStringParam(sourceParams, "customPreIterationCode");
this.customPostIterationCode = this._parseStringParam(sourceParams, "customPostIterationCode");
this.startDelay = this._parseIntParam(sourceParams, "startDelay", 0);
if (this.shouldReport && !this.startDelay)
this.startDelay = 4000;
for (const paramKey of ["tag", "tags", "test", "tests"])
this.testList = this._parseTestListParam(sourceParams, paramKey);
this.testIterationCount = this._parseIntParam(sourceParams, "iterationCount", 1);
this.testWorstCaseCount = this._parseIntParam(sourceParams, "worstCaseCount", 1);
const unused = Array.from(sourceParams.keys());
if (unused.length > 0)
console.error("Got unused source params", unused);
}
_parseTestListParam(sourceParams, key) {
if (!sourceParams.has(key))
return this.testList;
let testList = [];
if (sourceParams?.getAll) {
testList = sourceParams?.getAll(key);
} else {
// fallback for cli sourceParams which is just a Map;
testList = sourceParams.get(key).split(",");
}
sourceParams.delete(key);
if (this.testList.length > 0 && testList.length > 0)
throw new Error(`Overriding previous testList='${this.testList.join()}' with ${key} url-parameter.`);
return testList;
}
_parseStringParam(sourceParams, paramKey) {
if (!sourceParams.has(paramKey))
return DefaultJetStreamParams[paramKey];
const value = sourceParams.get(paramKey);
sourceParams.delete(paramKey);
return value;
}
_parseBooleanParam(sourceParams, paramKey) {
if (!sourceParams.has(paramKey))
return DefaultJetStreamParams[paramKey];
const value = sourceParams.get(paramKey).toLowerCase();
sourceParams.delete(paramKey);
return !(value === "false" || value === "0");
}
_parseIntParam(sourceParams, paramKey, minValue) {
if (!sourceParams.has(paramKey))
return DefaultJetStreamParams[paramKey];
const parsedValue = this._parseInt(sourceParams.get(paramKey), paramKey);
if (parsedValue < minValue)
throw new Error(`Invalid ${paramKey} param: '${parsedValue}', value must be >= ${minValue}.`);
sourceParams.delete(paramKey);
return parsedValue;
}
_parseInt(value, errorMessage) {
const number = Number(value);
if (!Number.isInteger(number) && errorMessage)
throw new Error(`Invalid ${errorMessage} param: '${value}', expected int.`);
return parseInt(number);
}
get isDefault() {
return this === DefaultJetStreamParams;
}
}
const DefaultJetStreamParams = new Params();
let maybeCustomParams = DefaultJetStreamParams;
if (globalThis?.JetStreamParamsSource) {
try {
maybeCustomParams = new Params(globalThis?.JetStreamParamsSource);
} catch (e) {
console.error("Invalid Params", e, "\nUsing defaults as fallback:", maybeCustomParams);
}
}
const JetStreamParams = maybeCustomParams;