-
Notifications
You must be signed in to change notification settings - Fork 307
add example for pooling browsers #14
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright 2018 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @author rahulkumar66@ (Rahul Kumar) | ||
*/ | ||
|
||
/** | ||
* Demonstrates how to reuse browser instances across multiple tasks | ||
* resulting in improved load times for subsequent requests. | ||
* This has huge load time improvements in case of SPAs | ||
*/ | ||
|
||
const genericPool = require('generic-pool'); | ||
const puppeteer = require('puppeteer'); | ||
|
||
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'; | ||
|
||
|
||
|
||
const factory = { | ||
|
||
create: async () => { | ||
const browser = await puppeteer.launch(puppeteerArgs); | ||
|
||
const page = await browser.newPage(); | ||
return page; | ||
}, | ||
destroy: (browser) => { | ||
browser.close(); | ||
|
||
}, | ||
}; | ||
|
||
const browserPool = genericPool.createPool(factory, { | ||
max: 5, // maximum number of browser instances that can be created at any given time | ||
min: 2, // minimum number of browser instances to keep in pool at any given time | ||
maxWaitingClients: 50 | ||
}); | ||
|
||
async function getContent(url) { | ||
const page = await browserPool.acquire(); | ||
await page.setViewport(VIEWPORT); | ||
|
||
const response = await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 }); | ||
// release browser instance back to pool so it can be reused | ||
await browserPool.release(page); | ||
return response.text(); | ||
} | ||
|
||
(async () => { | ||
const responseHackerNews = await getContent(HACKER_NEWS_SPA_URL); | ||
console.log(responseHackerNews); | ||
|
||
// 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); | ||
})(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, we need linting.
{
and the first key name and the space before}
.