@best/runner-headless
Version:
Best Runner (Headless)
99 lines • 5.13 kB
JavaScript
;
/*
* Copyright (c) 2019, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const runner_abstract_1 = __importDefault(require("@best/runner-abstract"));
const headless_1 = __importDefault(require("./headless"));
const UPDATE_INTERVAL = 300;
class Runner extends runner_abstract_1.default {
async run(benchmarkBuilds, projectConfig, globalConfig, runnerLogStream, interruption) {
const snapshotResults = [];
for (const benchmarkInfo of benchmarkBuilds) {
const { benchmarkEntry, benchmarkRemoteEntry, benchmarkSignature } = benchmarkInfo;
const runtimeOptions = this.getRuntimeOptions(projectConfig);
const state = this.initializeBenchmarkState();
const { url, terminate } = await this.initializeServer(benchmarkRemoteEntry || benchmarkEntry, projectConfig);
const browser = new headless_1.default(url, projectConfig);
try {
await browser.initialize();
runnerLogStream.onBenchmarkStart(benchmarkSignature);
const { results } = await this.runIterations(benchmarkSignature, browser, state, runtimeOptions, runnerLogStream, interruption);
const version = await browser.version();
const environment = await this.getEnvironment({ version }, projectConfig, globalConfig);
snapshotResults.push({ results, environment, benchmarkInfo, projectConfig });
}
catch (e) {
runnerLogStream.onBenchmarkError(benchmarkSignature);
throw e;
}
finally {
runnerLogStream.onBenchmarkEnd(benchmarkSignature);
await browser.close();
terminate();
}
}
return snapshotResults;
}
initializeBenchmarkState() {
return { executedTime: 0, executedIterations: 0, results: [] };
}
async runIterations(benchmarkSignature, browser, state, runtimeOptions, runnnerLogStream, interruption) {
return runtimeOptions.iterateOnClient
? this.runClientIterations(benchmarkSignature, browser, state, runtimeOptions, runnnerLogStream)
: this.runServerIterations(benchmarkSignature, browser, state, runtimeOptions, runnnerLogStream, interruption);
}
async runClientIterations(benchmarkSignature, browser, state, runtimeOptions, runnerLogStream) {
// Run an iteration to estimate the time it will take
const testResult = await this.runIteration(browser, { iterations: 1 });
const estimatedIterationTime = testResult.aggregate;
const start = Date.now();
const intervalId = setInterval(() => {
const executing = Date.now() - start;
state.executedTime = executing;
state.executedIterations = Math.round(executing / estimatedIterationTime);
const updatedState = { executedIterations: state.executedIterations, executedTime: state.executedTime };
runnerLogStream.updateBenchmarkProgress(benchmarkSignature, updatedState, runtimeOptions);
}, UPDATE_INTERVAL);
await browser.reloadPage();
const { results: [root], } = await this.runIteration(browser, runtimeOptions);
state.results.push(root);
clearInterval(intervalId);
return state;
}
async runServerIterations(benchmarkSignature, browser, state, runtimeOptions, runnnerLogStream, interruption) {
if (interruption && interruption.requestedInterruption) {
throw new Error(`Halted execution: interruption`);
}
if (state.executedTime < runtimeOptions.maxDuration ||
state.executedIterations < runtimeOptions.minSampleCount) {
const start = Date.now();
const benchmarkResults = await this.runIteration(browser, runtimeOptions);
const { results: [root], } = benchmarkResults;
await browser.reloadPage();
state.executedTime += Date.now() - start;
state.executedIterations += 1;
if (root) {
state.results.push(root);
}
const updatedState = { executedIterations: state.executedIterations, executedTime: state.executedTime };
runnnerLogStream.updateBenchmarkProgress(benchmarkSignature, updatedState, runtimeOptions);
return this.runIterations(benchmarkSignature, browser, state, runtimeOptions, runnnerLogStream, interruption);
}
return state;
}
runIteration(browser, payload) {
return browser.evaluate((o) => BEST.runBenchmark(o), payload);
}
static async getBrowserSpecs() {
return headless_1.default.getSpecs();
}
}
exports.default = Runner;
//# sourceMappingURL=index.js.map