tachometer
Version:
Web benchmark runner
193 lines • 6.94 kB
JavaScript
/**
* @license
* Copyright (c) 2019 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt The complete set of authors may be found
* at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
* be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
* Google as part of the polymer project is also subject to an additional IP
* rights grant found at http://polymer.github.io/PATENTS.txt
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.specUrl = exports.specsFromOpts = void 0;
const path = require("path");
const browser_1 = require("./browser");
const config_1 = require("./config");
const defaults = require("./defaults");
const util_1 = require("./util");
const versions_1 = require("./versions");
/**
* Derive the set of benchmark specifications we should run according to the
* given options, which may require checking the layout on disk of the
* benchmarks/ directory.
*/
async function specsFromOpts(opts) {
let windowSize;
if (opts['window-size']) {
const match = opts['window-size'].match(/^(\d+),(\d+)$/);
if (match === null) {
throw new Error(`Invalid --window-size flag, must match "width,height, " ` +
`but was "${opts['window-size']}"`);
}
windowSize = {
width: Number(match[1]),
height: Number(match[2]),
};
}
else {
windowSize = {
width: defaults.windowWidth,
height: defaults.windowHeight,
};
}
const browserStrings = new Set((opts.browser || defaults.browserName)
.replace(/\s+/, '')
.split(',')
.filter((b) => b !== ''));
if (browserStrings.size === 0) {
throw new Error('At least one --browser must be specified');
}
const browsers = [...browserStrings].map((str) => {
const config = Object.assign(Object.assign({}, browser_1.parseBrowserConfigString(str)), { windowSize });
browser_1.validateBrowserConfig(config);
return config;
});
const specs = [];
const versions = versions_1.parsePackageVersions(opts['package-version']);
if (versions.length === 0) {
versions.push(undefined);
}
// Benchmark paths/URLs are the bare arguments not associated with a flag, so
// they are found in _unknown.
for (const argStr of opts._unknown || []) {
const arg = parseBenchmarkArgument(argStr);
if (arg.kind === 'remote') {
const url = {
kind: 'remote',
url: arg.url,
};
const measurement = opts.measure !== undefined ? opts.measure : defaults.measurement(url);
const measurementExpression = measurement === 'global' ?
(opts['measurement-expression'] !== undefined ?
opts['measurement-expression'] :
defaults.measurementExpression) :
undefined;
for (const browser of browsers) {
const spec = {
name: arg.alias || arg.url,
browser,
measurement,
url,
};
if (measurementExpression) {
spec.measurementExpression = measurementExpression;
}
specs.push(spec);
}
}
else {
const root = opts.root || defaults.root;
const urlPath = await config_1.urlFromLocalPath(root, arg.diskPath);
let name = arg.alias;
if (name === undefined) {
const serverRelativePath = path.relative(root, arg.diskPath);
name = serverRelativePath.replace(path.win32.sep, '/');
}
for (const browser of browsers) {
for (const version of versions) {
const url = {
kind: 'local',
urlPath,
queryString: arg.queryString,
version,
};
const measurement = opts.measure !== undefined ?
opts.measure :
defaults.measurement(url);
const measurementExpression = measurement === 'global' ?
(opts['measurement-expression'] !== undefined ?
opts['measurement-expression'] :
defaults.measurementExpression) :
undefined;
const spec = {
name,
browser,
measurement,
url,
};
if (measurementExpression) {
spec.measurementExpression = measurementExpression;
}
specs.push(spec);
}
}
}
}
return specs;
}
exports.specsFromOpts = specsFromOpts;
function parseBenchmarkArgument(str) {
if (util_1.isHttpUrl(str)) {
// http://example.com
return {
kind: 'remote',
url: str,
};
}
if (str.includes('=')) {
const eq = str.indexOf('=');
const maybeUrl = str.substring(eq + 1);
if (util_1.isHttpUrl(maybeUrl)) {
// foo=http://example.com
return {
kind: 'remote',
url: maybeUrl,
alias: str.substring(0, eq),
};
}
}
let queryString = '';
if (str.includes('?')) {
// a/b.html?a=b
// foo=a/b.html?a=b
const q = str.indexOf('?');
queryString = str.substring(q);
str = str.substring(0, q);
}
let alias = undefined;
if (str.includes('=')) {
// foo=a/b.html?a=b
// foo=a/b.html
const eq = str.indexOf('=');
alias = str.substring(0, eq);
str = str.substring(eq + 1);
}
// a/b.html
// a/b.html?a=b
// foo=a/b.html
// foo=a/b.html?a=b
return {
kind: 'local',
alias,
diskPath: str,
queryString: queryString,
};
}
function specUrl(spec, servers, config) {
if (spec.url.kind === 'remote') {
return spec.url.url;
}
const server = servers.get(spec);
if (server === undefined) {
throw new Error('Internal error: no server for spec');
}
if (config.remoteAccessibleHost !== '' &&
spec.browser.remoteUrl !== undefined) {
return 'http://' + config.remoteAccessibleHost + ':' + server.port +
spec.url.urlPath + spec.url.queryString;
}
return server.url + spec.url.urlPath + spec.url.queryString;
}
exports.specUrl = specUrl;
//# sourceMappingURL=specs.js.map
;