-
-
Notifications
You must be signed in to change notification settings - Fork 125
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
Is it possible to reuse the puppeteer instance for better performance? #80
Comments
Hi @tomerb15 👋 I don't understand what is the issue you encountered. Is it too slow? If it is the case can you share your code, please? |
Hey, we use it to generate many small screenshots. Basically small HTMLs to JPEGs. We found puppeteer-cluster and with the right configuration, we manage to crack it down and go from ~900ms per image (Which grow up to 60 seconds at peak) to a steady ~350ms per image which solved our problem. Hope this will help anyone in the future. |
Here is our configuration for puppeteer-cluster which works very well:
We don't see any memory leak from the browsers. |
@tomerb15 how did you configure it ? can you please provide an example ? |
Perhaps the |
Just discovered another performance-related issue. You create a cluster for a batch of jobs, but the maxConcurrency option fails to use the full advantage of So issues currently present are:
Solutions:
I'm ready to work on implementing these solutions. |
@frinyvonnick In other words, my Dream Code: // imageMaker contains the puppeteer instance
const imageMaker = require('node-html-to-image');
imageMaker.createImage({html: html}) // as many times as you want... reuses the same browser |
**This is a breaking API change (for now)** node-html-to-image's default export is now a function which returns an instance object with a render() method which is the old nodeHtmlToImage() functionality. The reason for doing this is to allow us to instantiate a single 'image maker' object that we call render() on multiple times and reuse the same puppeteer cluster. In addition, * Update the type definitions. * Use Cluster.execute() instead of Cluster.queue() plus Cluster.idle() as this will allow better use in concurrent situations. We will only wait for our jobs to finish rather than for the entire cluster to go idle. Fixes frinyvonnick#80
@frinyvonnick The changes would be significant in terms of performance and not go beyond a few lines of code. |
@KL13NT I look at all issues and new messages. Answering takes some time. Avoid pinging me without new information. I'll answer as soon as I can. I maintain this package in my free time. Please, keep that in mind 😄 |
I'm facing this issue... it takes about 2 seconds to render per image. I've already tried to change puppeter settings, but the time didn't change... can you please help me? I generate a buffer with the image in jpeg. |
@joshk0 I would prefer to avoid breaking change. I created this package in the idea of having something simple and easy to use. Initially I thought people might directly use puppeteer for more advance use cases. I changed a bit my mind since people seems to enjoy this package. Here are the existing use cases: Single image const nodeHtmlToImage = require('node-html-to-image');
nodeHtmlToImage({
html: '<html><body>Hello world!</body></html>',
}) Batch image creation const nodeHtmlToImage = require('node-html-to-image');
nodeHtmlToImage({
html: '<html><body>Hello {{name}}!</body></html>',
content: [{ name: 'Pierre' }, { name: 'Paul' }, { name: 'Jacques' }]
})
.then(() => console.log('The images were created successfully!')) We might expose a name export that let you reuse the instance like it: const { launch } = require('node-html-to-image');
const instance = launch({ puppeteerArgs })
instance.screenshot({
html: '<html><body>Hello {{name}}!</body></html>',
content: [{ name: 'Pierre' }, { name: 'Paul' }, { name: 'Jacques' }]
})
.then(() => console.log('The images were created successfully!'))
instance.screenshot({
html: '<html><body>Hello {{name}}!</body></html>',
content: { name: 'Michel' }
})
.then(() => console.log('The image was created successfully!')) |
@frinyvonnick I think it would be a good idea to expose the instance.
And changing the waitUntil to |
This would be of great use to me as well, I'm attempting to use this to create information cards to display in Discord embeds. Unfortunately it takes a rather long time for the images to be generated (around 4-5 seconds). |
Could we get an update on the whole idea about reusing the instance? The usecase i'm facing is the following: 12 different HTML templates Right now, we have to go through them, one by one, because we have multiple different templates, which in turn means that we have to create and load a new puppeteer instance for every single image. I could live with us being able to make |
I discover that puppeteer-cluster can take a puppeteer object as parameter of the launch method (see https://github.com/thomasdondorf/puppeteer-cluster#clusterlaunchoptions). This could be an option to pass your own instance of puppeteer. Is someone interested in opening a pull request to add this option? Another performance improvement could be to use an instance of puppeteer-core with headless shell (see https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#puppeteer-vs-puppeteer-core). Could be useful in some contextes where disk space matters. Anyone interested in by this too? |
I had performance issues for a scenario where a user request would start some good (~300) amount of screenshots, so if more than a small amount of users decided to do the same thing, the machine would run out of memory. I decided to create a singleton puppeteer cluster and enqueue manually the screenshots, another improvement was to render all the html in the same page and run query selectors (all the divs were small, height around 350px). |
I'm very interested in this feature! |
Any update on this? |
What is the current workaround for performance issue |
What is your recommended settings to maximize performance in the following case:
Thanks!
The text was updated successfully, but these errors were encountered: