Skip to content

Commit

Permalink
Reduce time for fidelity test CI locally by 50s, Github CI by 130s (#…
Browse files Browse the repository at this point in the history
…4543)

* reuse browser, and create page ahead of time.

* fix log messages, now that browser only launches once.
  • Loading branch information
bhouston authored Oct 26, 2023
1 parent d76e411 commit df26025
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
55 changes: 39 additions & 16 deletions packages/render-fidelity-tools/src/artifact-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import {promises as fs} from 'fs';
import mkdirp from 'mkdirp';
import {join, resolve} from 'path';
import pngjs from 'pngjs';
import puppeteer from 'puppeteer';
import puppeteer, { Browser } from 'puppeteer';

import {DEVICE_PIXEL_RATIO, Dimensions, FIDELITY_TEST_THRESHOLD, FidelityRegressionResults, GoldenConfig, ImageComparator, ImageComparisonAnalysis, ImageComparisonConfig, ScenarioConfig, toDecibel} from './common.js';
import {ConfigReader} from './config-reader.js';


const $configReader = Symbol('configReader');

export type AnalysisResults = Array<ImageComparisonAnalysis>;
Expand All @@ -32,13 +33,27 @@ export interface ScenarioRecord extends ScenarioConfig {

export class ArtifactCreator {
private[$configReader]: ConfigReader = new ConfigReader(this.config);
private browser: Browser | undefined = undefined;
private pagePromise: Promise<any> | undefined = undefined;

constructor(
protected config: ImageComparisonConfig, protected rootDirectory: string,
protected baseUrl: string) {
console.log('🌈 Preparing to capture screenshots for fidelity comparison');
}

async close() {
if( this.pagePromise !== undefined ) {
const page = await this.pagePromise;
await page.close();
this.pagePromise = undefined;
}

if (this.browser !== undefined) {
await this.browser.close();
this.browser = undefined;
}
}
protected get outputDirectory(): string {
return join(resolve(this.rootDirectory), 'results');
}
Expand Down Expand Up @@ -248,6 +263,7 @@ export class ArtifactCreator {

return analysis;
}


async captureScreenshot(
renderer: string, scenarioName: string, dimensions: Dimensions,
Expand All @@ -263,21 +279,26 @@ export class ArtifactCreator {
return;
}

console.log(`🚀 Launching browser`);

const browser = await puppeteer.launch({
defaultViewport: {
width: scaledWidth,
height: scaledHeight,
deviceScaleFactor: DEVICE_PIXEL_RATIO
},
headless: false
});

if( this.browser == undefined) {
console.log(`🚀 Launching browser`);
this.browser = await puppeteer.launch({
headless: false,
});
this.pagePromise = this.browser.newPage();
}

const page = await browser.newPage();
const page = await this.pagePromise;
this.pagePromise = undefined;
const url = `${this.baseUrl}?hide-ui&config=../../config.json&scenario=${
encodeURIComponent(scenarioName)}`;

await page.setViewport({
width: scaledWidth,
height: scaledHeight,
deviceScaleFactor: DEVICE_PIXEL_RATIO
});

page.on('error', (error: any) => {
console.log(`🚨 ${error}`);
});
Expand All @@ -303,7 +324,7 @@ export class ArtifactCreator {
// variables are captured in its closure scope. TypeScript compiler
// currently has no mechanism to detect this and will happily tell you
// your code is correct when it isn't.
const evaluateError = await page.evaluate(async (maxTimeInSec) => {
const evaluateError = await page.evaluate(async (maxTimeInSec: number) => {
const modelBecomesReady = new Promise<void>((resolve, reject) => {
let timeout: NodeJS.Timeout;
if (maxTimeInSec > 0) {
Expand Down Expand Up @@ -331,7 +352,8 @@ export class ArtifactCreator {

if (evaluateError) {
console.log(evaluateError);
await browser.close();
await this.browser.close();
this.browser = undefined;
throw new Error(evaluateError);
}

Expand All @@ -346,8 +368,9 @@ export class ArtifactCreator {
const screenshot =
await page.screenshot({path: outputPath, omitBackground: true});

await browser.close();

page.close();
this.pagePromise = this.browser.newPage();

return screenshot;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ screenshotCreator.fidelityTest(scenarioWhitelist)
.then(() => {
console.log(`✅ Results recorded to ${outputDirectory}`);
server.close();
screenshotCreator.close();

const fidelityRegressionPath =
join(outputDirectory, 'fidelityRegressionResults.json');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ export const rendererScreenshot = async(
renderer, scenarioName, dimensions, outputFile);
} finally {
server.close();
screenshotCreator.close();
}
};

0 comments on commit df26025

Please sign in to comment.