UNPKG

7.56 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2019 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt The complete set of authors may be found
7 * at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
8 * be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
9 * Google as part of the polymer project is also subject to an additional IP
10 * rights grant found at http://polymer.github.io/PATENTS.txt
11 */
12var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
13 if (k2 === undefined) k2 = k;
14 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
15}) : (function(o, m, k, k2) {
16 if (k2 === undefined) k2 = k;
17 o[k2] = m[k];
18}));
19var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20 Object.defineProperty(o, "default", { enumerable: true, value: v });
21}) : function(o, v) {
22 o["default"] = v;
23});
24var __importStar = (this && this.__importStar) || function (mod) {
25 if (mod && mod.__esModule) return mod;
26 var result = {};
27 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28 __setModuleDefault(result, mod);
29 return result;
30};
31Object.defineProperty(exports, "__esModule", { value: true });
32exports.specUrl = exports.specsFromOpts = void 0;
33const path = __importStar(require("path"));
34const browser_1 = require("./browser");
35const config_1 = require("./config");
36const defaults = __importStar(require("./defaults"));
37const util_1 = require("./util");
38const versions_1 = require("./versions");
39/**
40 * Derive the set of benchmark specifications we should run according to the
41 * given options, which may require checking the layout on disk of the
42 * benchmarks/ directory.
43 */
44async function specsFromOpts(opts) {
45 let windowSize;
46 if (opts['window-size']) {
47 const match = opts['window-size'].match(/^(\d+),(\d+)$/);
48 if (match === null) {
49 throw new Error(`Invalid --window-size flag, must match "width,height, " ` +
50 `but was "${opts['window-size']}"`);
51 }
52 windowSize = {
53 width: Number(match[1]),
54 height: Number(match[2]),
55 };
56 }
57 else {
58 windowSize = {
59 width: defaults.windowWidth,
60 height: defaults.windowHeight,
61 };
62 }
63 const browserStrings = new Set((opts.browser || defaults.browserName)
64 .replace(/\s+/, '')
65 .split(',')
66 .filter((b) => b !== ''));
67 if (browserStrings.size === 0) {
68 throw new Error('At least one --browser must be specified');
69 }
70 const browsers = [...browserStrings].map((str) => {
71 const config = Object.assign(Object.assign({}, browser_1.parseBrowserConfigString(str)), { windowSize });
72 browser_1.validateBrowserConfig(config);
73 return config;
74 });
75 const specs = [];
76 const versions = versions_1.parsePackageVersions(opts['package-version']);
77 if (versions.length === 0) {
78 versions.push(undefined);
79 }
80 let measurement;
81 if (opts.measure === 'callback') {
82 measurement = {
83 mode: 'callback',
84 };
85 }
86 else if (opts.measure === 'fcp') {
87 measurement = {
88 mode: 'performance',
89 entryName: 'first-contentful-paint',
90 };
91 }
92 else if (opts.measure === 'global') {
93 measurement = {
94 mode: 'expression',
95 expression: opts['measurement-expression'] || defaults.measurementExpression,
96 };
97 }
98 else if (opts.measure !== undefined) {
99 util_1.throwUnreachable(opts.measure, `Internal error: unknown measure ${JSON.stringify(opts.measure)}`);
100 }
101 // Benchmark paths/URLs are the bare arguments not associated with a flag, so
102 // they are found in _unknown.
103 for (const argStr of opts._unknown || []) {
104 const arg = parseBenchmarkArgument(argStr);
105 if (arg.kind === 'remote') {
106 const url = {
107 kind: 'remote',
108 url: arg.url,
109 };
110 for (const browser of browsers) {
111 const spec = {
112 name: arg.alias || arg.url,
113 browser,
114 measurement: [measurement === undefined ? defaults.measurement(url) :
115 measurement],
116 url,
117 };
118 specs.push(spec);
119 }
120 }
121 else {
122 const root = opts.root || defaults.root;
123 const urlPath = await config_1.urlFromLocalPath(root, arg.diskPath);
124 let name = arg.alias;
125 if (name === undefined) {
126 const serverRelativePath = path.relative(root, arg.diskPath);
127 name = serverRelativePath.replace(/\\/g, '/');
128 }
129 for (const browser of browsers) {
130 for (const version of versions) {
131 const url = {
132 kind: 'local',
133 urlPath,
134 queryString: arg.queryString,
135 version,
136 };
137 const spec = {
138 name,
139 browser,
140 measurement: [measurement === undefined ? defaults.measurement(url) :
141 measurement],
142 url,
143 };
144 specs.push(spec);
145 }
146 }
147 }
148 }
149 return specs;
150}
151exports.specsFromOpts = specsFromOpts;
152function parseBenchmarkArgument(str) {
153 if (util_1.isHttpUrl(str)) {
154 // http://example.com
155 return {
156 kind: 'remote',
157 url: str,
158 };
159 }
160 if (str.includes('=')) {
161 const eq = str.indexOf('=');
162 const maybeUrl = str.substring(eq + 1);
163 if (util_1.isHttpUrl(maybeUrl)) {
164 // foo=http://example.com
165 return {
166 kind: 'remote',
167 url: maybeUrl,
168 alias: str.substring(0, eq),
169 };
170 }
171 }
172 let queryString = '';
173 if (str.includes('?')) {
174 // a/b.html?a=b
175 // foo=a/b.html?a=b
176 const q = str.indexOf('?');
177 queryString = str.substring(q);
178 str = str.substring(0, q);
179 }
180 let alias = undefined;
181 if (str.includes('=')) {
182 // foo=a/b.html?a=b
183 // foo=a/b.html
184 const eq = str.indexOf('=');
185 alias = str.substring(0, eq);
186 str = str.substring(eq + 1);
187 }
188 // a/b.html
189 // a/b.html?a=b
190 // foo=a/b.html
191 // foo=a/b.html?a=b
192 return {
193 kind: 'local',
194 alias,
195 diskPath: str,
196 queryString: queryString,
197 };
198}
199function specUrl(spec, servers, config) {
200 if (spec.url.kind === 'remote') {
201 return spec.url.url;
202 }
203 const server = servers.get(spec);
204 if (server === undefined) {
205 throw new Error('Internal error: no server for spec');
206 }
207 if (config.remoteAccessibleHost !== '' &&
208 spec.browser.remoteUrl !== undefined) {
209 return 'http://' + config.remoteAccessibleHost + ':' + server.port +
210 spec.url.urlPath + spec.url.queryString;
211 }
212 return server.url + spec.url.urlPath + spec.url.queryString;
213}
214exports.specUrl = specUrl;
215//# sourceMappingURL=specs.js.map
\No newline at end of file