Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce time for fidelity test CI locally by 50s, Github CI by 130s #4543

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
};
Loading