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

perf: use context isolation for inidividual journeys #274

Merged
merged 1 commit into from
May 13, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions __tests__/core/browser-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ describe('BrowserService', () => {
});

it('should create browser pages', async () => {
const driver = await Gatherer.setupDriver({
const { page } = await Gatherer.setupDriver({
wsEndpoint: 'ws://localhost:9323',
});
await driver.page.goto(server.TEST_PAGE);
await Gatherer.dispose(driver);
await page.goto(server.TEST_PAGE);
await Gatherer.stop();
});
});
12 changes: 10 additions & 2 deletions __tests__/core/gatherer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ import { wsEndpoint } from '../utils/test-config';
jest.mock('../../src/plugins/network');

describe('Gatherer', () => {
it('boot and dispose driver', async () => {
it('boot and close browser', async () => {
const driver = await Gatherer.setupDriver({ wsEndpoint });
expect(typeof driver.page.goto).toBe('function');
await Gatherer.stop();
});

it('setup and dispose driver', async () => {
const driver = await Gatherer.setupDriver({ wsEndpoint });
await Gatherer.dispose(driver);
expect(Gatherer.browser).toBeDefined();
await Gatherer.stop();
expect(Gatherer.browser).toBeNull();
});

it('begin recording based on flags', async () => {
Expand All @@ -45,6 +53,6 @@ describe('Gatherer', () => {
expect(pluginManager).toBeInstanceOf(PluginManager);
const network = pluginManager.get(NetworkManager);
expect(network.start).toHaveBeenCalled();
await Gatherer.dispose(driver);
await Gatherer.stop();
});
});
10 changes: 5 additions & 5 deletions __tests__/core/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('runner', () => {
const context = await Runner.createContext(runOptions);
await runner.registerJourney(j1, context);
const result = await runner.runSteps(j1, context, runOptions);
await Gatherer.dispose(context.driver);
await Gatherer.stop();
expect(result).toEqual([
{
status: 'succeeded',
Expand All @@ -178,7 +178,7 @@ describe('runner', () => {
const context = await Runner.createContext(runOptions);
await runner.registerJourney(j1, context);
const result = await runner.runSteps(j1, context, runOptions);
await Gatherer.dispose(context.driver);
await Gatherer.stop();
expect(result).toEqual([
{
status: 'failed',
Expand All @@ -199,7 +199,7 @@ describe('runner', () => {
const context = await Runner.createContext({});
await runner.registerJourney(j1, context);
const result = await runner.runSteps(j1, context, {});
await Gatherer.dispose(context.driver);
await Gatherer.stop();
expect(result).toEqual([
{
status: 'failed',
Expand All @@ -218,7 +218,7 @@ describe('runner', () => {
const context = await Runner.createContext({});
await runner.registerJourney(j1, context);
const result = await runner.runSteps(j1, context, {});
await Gatherer.dispose(context.driver);
await Gatherer.stop();
expect(result).toEqual([
{
status: 'failed',
Expand All @@ -242,7 +242,7 @@ describe('runner', () => {
const context = await Runner.createContext(runOptions);
await runner.registerJourney(j1, context);
const [step1, step2] = await runner.runSteps(j1, context, runOptions);
await Gatherer.dispose(context.driver);
await Gatherer.stop();
expect(step1).toEqual({
status: 'succeeded',
url: server.TEST_PAGE,
Expand Down
10 changes: 5 additions & 5 deletions __tests__/plugins/browser-console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ describe('BrowserConsole', () => {
});

it('should capture browser console logs', async () => {
const driver = await Gatherer.setupDriver({ wsEndpoint });
const browserConsole = new BrowserConsole(driver.page);
const { page } = await Gatherer.setupDriver({ wsEndpoint });
const browserConsole = new BrowserConsole(page);
browserConsole.start();
await driver.page.goto(server.TEST_PAGE);
await page.goto(server.TEST_PAGE);
browserConsole._currentStep = { name: 'step-name', index: 0 };
await driver.page.evaluate(() =>
await page.evaluate(() =>
console.warn('test-message', 1, { test: 'test' })
);

Expand All @@ -53,6 +53,6 @@ describe('BrowserConsole', () => {
expect(testMessage.type).toEqual('warning');
expect(testMessage.timestamp).toBeDefined();
expect(testMessage.step).toEqual({ name: 'step-name', index: 0 });
await Gatherer.dispose(driver);
await Gatherer.stop();
});
});
13 changes: 7 additions & 6 deletions __tests__/plugins/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('network', () => {
responseReceivedTime: expect.any(Number),
timings: expect.any(Object),
});
await Gatherer.dispose(driver);
await Gatherer.stop();
});

it('not include data URL in network info', async () => {
Expand All @@ -72,7 +72,7 @@ describe('network', () => {
const netinfo = await network.stop();
expect(await driver.page.content()).toContain('Data URI test');
expect(netinfo).toEqual([]);
await Gatherer.dispose(driver);
await Gatherer.stop();
});

it('produce distinct events for redirects', async () => {
Expand All @@ -93,7 +93,7 @@ describe('network', () => {
expect(netinfo[0].status).toBe(302);
expect(netinfo[1].status).toBe(302);
expect(netinfo[2].status).toBe(200);
await Gatherer.dispose(driver);
await Gatherer.stop();
});

it('measure resource and transfer size', async () => {
Expand All @@ -109,7 +109,7 @@ describe('network', () => {
resourceSize: 10,
transferSize: expect.any(Number),
});
await Gatherer.dispose(driver);
await Gatherer.stop();
});

it('timings for aborted requests', async () => {
Expand All @@ -129,7 +129,7 @@ describe('network', () => {

await driver.page.goto(server.PREFIX + '/index');
await driver.page.waitForLoadState();
await Gatherer.dispose(driver);
await Gatherer.stop();
const netinfo = await network.stop();
expect(netinfo.length).toBe(2);
expect(netinfo[1]).toMatchObject({
Expand Down Expand Up @@ -163,8 +163,9 @@ describe('network', () => {
res.end(`<script src=${server.PREFIX}/chunked />`);
});

await driver.page.waitForLoadState();
await driver.page.goto(server.PREFIX + '/index');
await Gatherer.dispose(driver);
await Gatherer.stop();
const netinfo = await network.stop();
expect(netinfo.length).toBe(2);
expect(netinfo[1]).toMatchObject({
Expand Down
2 changes: 1 addition & 1 deletion __tests__/plugins/performance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ describe('performance', () => {
'JSHeapTotalSize',
]);
await performance.stop();
await Gatherer.dispose(driver);
await Gatherer.stop();
});
});
2 changes: 1 addition & 1 deletion __tests__/plugins/tracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('tracing', () => {
*/
await driver.page.waitForTimeout(100);
const events = await tracer.stop(driver.client);
await Gatherer.dispose(driver);
await Gatherer.stop();
const filmstrips = filterFilmstrips(events);
expect(filmstrips.length).toBeGreaterThan(0);
expect(filmstrips[0]).toMatchObject({
Expand Down
38 changes: 22 additions & 16 deletions src/core/gatherer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,25 @@ export type Driver = {
* related capabilities for the runner to run all journeys
*/
export class Gatherer {
static browser: ChromiumBrowser;

static async setupDriver(options: RunOptions): Promise<Driver> {
let browser: ChromiumBrowser;
const { wsEndpoint, headless, sandbox = false } = options;
if (wsEndpoint) {
log(`Gatherer: connecting to WS endpoint: ${wsEndpoint}`);
browser = await chromium.connect({ wsEndpoint });
} else {
log('Gatherer: launching chrome');
if (!sandbox) {
log('Gatherer: chromium sandboxing is disabled');
if (Gatherer.browser == null) {
const { wsEndpoint, headless, sandbox = false } = options;
if (wsEndpoint) {
log(`Gatherer: connecting to WS endpoint: ${wsEndpoint}`);
Gatherer.browser = await chromium.connect({ wsEndpoint });
} else {
Gatherer.browser = await chromium.launch({
headless,
chromiumSandbox: sandbox,
});
}
browser = await chromium.launch({
headless,
chromiumSandbox: sandbox,
});
}
const context = await browser.newContext();
const context = await Gatherer.browser.newContext();
const page = await context.newPage();
const client = await context.newCDPSession(page);
return { browser, context, page, client };
return { browser: Gatherer.browser, context, page, client };
}

/**
Expand All @@ -84,6 +83,13 @@ export class Gatherer {
}

static async dispose(driver: Driver) {
await driver.browser.close();
await driver.context.close();
}

static async stop() {
if (Gatherer.browser) {
await Gatherer.browser.close();
Gatherer.browser = null;
}
}
}
1 change: 1 addition & 0 deletions src/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ export default class Runner {
const journeyResult = await this.runJourney(journey, options);
result[journey.name] = journeyResult;
}
await Gatherer.stop();
await this.runAfterAllHook();
this.reset();
this.emit('end', {});
Expand Down