Puppeteer performance issue

Viewed 119

I'm testing Puppeteer for large scale PDF generation, I'm testing 5 requests per second for 1 min, the problem is that the puppeteer is causing the VM to consume all resources and crash.

code

async generatePdf({
url,

displayHeaderFooter,
headerContent,
footerContent,

width,
height,
marginTop,
marginRight,
marginBottom,
marginLeft}: IPDFRepositoryDTO): Promise<Buffer> {
if (!global.browser) {
  await launchPuppeteer();
}

const page = await global.browser.newPage();

await page.goto(url, {
  waitUntil: 'networkidle0',
});

const isDisplayHeaderFooter =
  displayHeaderFooter != undefined ? displayHeaderFooter : true;

const pageOptions = {
  printBackground: true,
  format: 'Letter',
  displayHeaderFooter: isDisplayHeaderFooter,
  headerTemplate: headerContent,
  footerTemplate: footerContent,
  width,
  height,
  margin: {
    top: marginTop || '80px',
    bottom: marginBottom || '80px',
    left: marginLeft || '20px',
    right: marginRight || '20px',
  },
} as PDFOptions;

const pdf = await page.pdf(pageOptions);

await page.close();

return pdf;}

function launch puppeteer:

 export const launchPuppeteer = async () => {
    global.browser = await launch({
     args: [
      // '--no-sandbox',
      // '--disable-setuid-sandbox',
      // '--disable-dev-shm-usage',
      // '--disable-accelerated-2d-canvas',
      // '--no-first-run',
      // '--no-zygote',
      // '--single-process',
      // '--disable-gpu',

      '--no-sandbox',
      '--disable-accelerated-2d-canvas',
      '--no-zygote',
       '--single-process',
       '--disable-gpu',

      '--disable-canvas-aa', // Disable antialiasing on 2d canvas
      '--disable-2d-canvas-clip-aa', // Disable antialiasing on 2d canvas clips
      '--disable-gl-drawing-for-tests', // BEST OPTION EVER! Disables GL drawing operations which produce pixel output. With this the GL output will not be correct but tests will run faster.
      '--disable-dev-shm-usage',
      '--use-gl=swiftshader', // better cpu usage with --use-gl=desktop rather than --use-gl=swiftshader, still needs more testing.
      '--enable-webgl',
      '--hide-scrollbars',
      '--mute-audio',
      '--no-first-run',
      '--disable-infobars',
      '--disable-breakpad',
      //'--ignore-gpu-blacklist',
      // '--window-size=1280,1024', // see defaultViewport
      // '--user-data-dir=./chromeData', // created in index.js, guess cache folder ends up inside too.
      '--disable-setuid-sandbox'
    ],
  });

  console.log('browser ready');
};

For the tests I'm using a google cloud VM that has 4 cores and 4GB of ram, e2-highcpu-4 , running Ubuntu

enter image description here

0 Answers
Related