Skip to content

Commit

Permalink
fix: cleanup runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Ferreiro Val committed Jun 19, 2019
1 parent f4f385b commit 57223c0
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 185 deletions.
2 changes: 0 additions & 2 deletions packages/@best/console-stream/src/runner-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ interface RunnerConfig {
maxDuration: number;
minSampleCount: number,
iterations: number,
iterateOnClient: boolean
}

interface RunnerState {
executedTime: number,
executedIterations: number,
results: any[],
iterateOnClient: boolean,
}
interface BenchmarkProgress {
executedIterations: number,
Expand Down
64 changes: 54 additions & 10 deletions packages/@best/runner-abstract/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import { dirname, basename } from 'path';
import express from 'express';
import { getSystemInfo } from '@best/utils';
import { RunnerOutputStream } from "@best/console-stream";
import { FrozenGlobalConfig, FrozenProjectConfig, BenchmarkInfo, BenchmarkRuntimeConfig } from '@best/types';
import { FrozenGlobalConfig, FrozenProjectConfig, BenchmarkInfo, BenchmarkRuntimeConfig, BenchmarkResultsSnapshot, BrowserConfig, EnvironmentConfig } from '@best/types';

export interface BenchmarkResultsState {
executedTime: number,
executedIterations: number,
results: any[],
iterateOnClient: boolean,
}
export default abstract class AbstractRunner {
abstract async run({ benchmarkEntry }: BenchmarkInfo, projectConfig: FrozenProjectConfig, globalConfig: FrozenGlobalConfig, runnerLogStream: RunnerOutputStream): Promise<BenchmarkResultsSnapshot>;

export interface BenchmarkResults {}
initializeServer(benchmarkEntry: string, useHttp: boolean): Promise<{ terminate:Function, url: string }> {
if (!useHttp) {
return Promise.resolve({ url: `file://${benchmarkEntry}`, terminate: () => {}});
}

export default abstract class AbstractRunner {
abstract async run({ benchmarkEntry }: BenchmarkInfo, projectConfig: FrozenProjectConfig, globalConfig: FrozenGlobalConfig, runnerLogStream: RunnerOutputStream): Promise<BenchmarkResults>;
return new Promise((resolve) => {
const app = express();
app.use(express.static(dirname(benchmarkEntry)));
const server = app.listen(() => {
const { port }: any = server.address();
resolve({
url: `http://127.0.0.1:${port}/${basename(benchmarkEntry)}`,
terminate: () => { server.close(); }
});
});
});
}

getRuntimeOptions(projectConfig: FrozenProjectConfig): BenchmarkRuntimeConfig {
const { benchmarkIterations, benchmarkOnClient, benchmarkMaxDuration, benchmarkMinIterations } = projectConfig;
Expand All @@ -37,4 +48,37 @@ export default abstract class AbstractRunner {
container: { load }
}
}

async normalizeEnvironment(browser: BrowserConfig, projectConfig: FrozenProjectConfig, globalConfig: FrozenGlobalConfig): Promise<EnvironmentConfig> {
const {
benchmarkOnClient,
benchmarkRunner,
benchmarkEnvironment,
benchmarkIterations,
projectName,
} = projectConfig;

const { system, cpu, os, load } = await getSystemInfo();

return {
hardware: { system, cpu, os },
container: { load },
browser,
configuration: {
project: {
projectName,
benchmarkOnClient,
benchmarkRunner,
benchmarkEnvironment,
benchmarkIterations,
},
global: {
gitCommitHash: globalConfig.gitInfo.lastCommit.hash,
gitHasLocalChanges: globalConfig.gitInfo.localChanges,
gitBranch: globalConfig.gitInfo.branch,
gitRepository: globalConfig.gitInfo.repo,
},
},
};
}
}
63 changes: 63 additions & 0 deletions packages/@best/runner-headless/src/headless.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import puppeteer from 'puppeteer';

const BROWSER_ARGS = [
'--no-sandbox',
`--js-flags=--expose-gc`,
'--disable-infobars',
'--disable-background-networking',
'--disable-extensions',
'--disable-translate',
'--no-first-run',
'--ignore-certificate-errors',
'--enable-precise-memory-info',
];

const PUPPETEER_OPTIONS = { args: BROWSER_ARGS };

export default class HeadlessBrowser {
pageUrl: string;
browser?: puppeteer.Browser;
page?: puppeteer.Page;
pageError?: Error;

constructor(url: string) {
this.pageUrl = url;
}

async initialize() {
this.browser = await puppeteer.launch(PUPPETEER_OPTIONS);
this.page = await this.browser.newPage();
this.page.on('pageerror', (error: Error) => this.pageError = error);
await this.page.goto(this.pageUrl);
this.checkForErrors();
}
checkForErrors() {
const pageError = this.pageError;
if (pageError) {
pageError.message = 'Benchmark parse error.\n' + pageError.message;
throw pageError;
}
}

close() {
if (this.browser) {
return this.browser.close();
}
}

reloadPage() {
if (this.page) {
return this.page.reload();
}
}

async evaluate(fn: any, payload: any) {
let result;
if (this.page) {
result = await this.page.evaluate(fn, payload);
this.checkForErrors();
}

return result;
}
}
Loading

0 comments on commit 57223c0

Please sign in to comment.