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

add example for pooling browsers #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rahulkumar66
Copy link

this is an example for pooling browsers resulting in faster load times of pages since we not don't have to open new browser for each request .
This results in improve load times especially in case of SPAs since cached resources can be reused.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

@googlebot
Copy link

CLAs look good, thanks!

@rahulkumar66
Copy link
Author

I signed it.

@rahulkumar66
Copy link
Author

This fixes #14

Copy link
Contributor

@ebidel ebidel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be interesting to have a flag that controls using a pool and not using a pool. And then throw in some console.time to show the difference in perf.

const genericPool = require('generic-pool');
const puppeteer = require('puppeteer');

const VIEWPORT = { width: 1028, height: 800, deviceScaleFactor: 2 };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, we need linting.

  • Can you remove the surrounding whitespaces after { and the first key name and the space before }.
  • Use 2-space indentation everywhere.


const factory = {
create: async () => {
const browser = await puppeteer.launch(puppeteerArgs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where does puppeteerArgs come from?

const HACKER_NEWS_QUERY_SPA_URL = 'https://hn.algolia.com/?query=angular';


const factory = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use a better name?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no


const VIEWPORT = { width: 1028, height: 800, deviceScaleFactor: 2 };
const HACKER_NEWS_SPA_URL = 'https://hn.algolia.com/';
const HACKER_NEWS_QUERY_SPA_URL = 'https://hn.algolia.com/?query=angular';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use https://hn.algolia.com/?query=chrome

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also build off the base url.

return page;
},
destroy: (browser) => {
browser.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return browser.close();

// this request will use the instance from the current browser pool resulting in faster response
const responseHackerNewsWithQuery = await getContent(HACKER_NEWS_QUERY_SPA_URL);
console.log(responseHackerNewsWithQuery);
})();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline

@ebidel
Copy link
Contributor

ebidel commented Aug 23, 2018

Is it possible to add a few more examples to the demo that helps show why a pool is necessary in some cases. Otherwise it's easy just to use the same browser instance to open 2 tabs for the URLs you're using.

@rahulkumar66
Copy link
Author

The reason we are parallelizing work through browsers and not pages is because one page crash might bring down the whole browser and also each page isn't guaranteed to be totally clean (cookies and storage might bleed-through as seen here).

@ebidel
Copy link
Contributor

ebidel commented Aug 24, 2018 via email

@rahulkumar66
Copy link
Author

i have used single browser in production and it was not stable over long periods of time maybe because i was using --single process flag.
Even browserless.io recommends running multiple browsers opposed to running multiple pages

@ebidel
Copy link
Contributor

ebidel commented Aug 27, 2018 via email

@rahulkumar66
Copy link
Author

Ok i will add it

return page;
},
destroy: (browser) => {
return browser.close();
Copy link

@SebastiyanKasurov SebastiyanKasurov Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You receive page from (create), in destroy you receive bro page, so page will be closed not browser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants