I am trying to generate images of web pages in under a second in a server-side environment. The requests could come in parallel, at the same time from the web. To that end, I am using Puppeteer-Sharp library which works pretty well. On the back end its using Chromium to load the page and then screenshot it.
The problem is that it takes a while to get started. For instance, note the timings (from my pc) from the readme.md sample code:
var options = new new LaunchOptions {Headless = true, ExecutablePath = @"c:\foo\chrome.exe"};
var browser = await Puppeteer.LaunchAsync(options).Result; // ~500ms
var page = browser.NewPageAsync().Result; // ~215ms
var webPage = page.GoToAsync("http://www.google.com").Result; // ~500ms
var screenshot = page.ScreenshotAsync(outputFile);
screenshot.wait(); // ~300ms
As you can see, it easily goes over a second. I don't know how Chromium works internally, so I have a couple of questions pertaining to solutions that I am thinking of.
- Is the
PuppeteerSharp.Browser
object thread-safe and/or re-entrant? Can I use the same browser object from different threads? I am thinking not, because it's tied to a specific instance of Chromium in memory. - If I cut out
.LaunchAsync
and.NetPageAsync
from every request that will significantly speed up the operation. Will pool ofPuppeteerSharp.Browser
objects work? For instance, I can pre-allocate 5 of these and execute.NetPageAsync
on them. Then the incoming requests would use the objects from the pool. Is that a viable approach?