【问题标题】:Puppeteer slow execution on Google Cloud FunctionsPuppeteer 在 Google Cloud Functions 上执行缓慢
【发布时间】:2019-04-04 16:46:26
【问题描述】:

我在 Google Cloud Functions 上使用 Puppeteer。

经过几次测试,我注意到我的代码在 Google Cloud Functions 基础架构上部署时平均需要大约 56 秒,而在本地测试的相同功能只需要 13 秒。

index.js

const chromium = require('chrome-aws-lambda');
const puppeteer = require('puppeteer-core');
const functions = require('firebase-functions');

exports.check = functions.https.onRequest(async (req, res) => {
    const License = req.query.License;

    browser = await puppeteer.launch({
        args: chromium.args,
        defaultViewport: chromium.defaultViewport,
        executablePath: await chromium.executablePath,
        headless: chromium.headless,
      });
    const page = await browser.newPage();

    await page.goto('http://www.example.com', {waitUntil: 'networkidle2'});
    await page.focus('#txtUserName');
    await page.keyboard.type('testUsername');
    await page.focus('#txtPassword');
    await page.keyboard.type('123123');
    await page.click('#btnLogin');
    await page.waitForSelector('#ctl00_400_header_400')
    //console.log("[✓]login successfully.")
    await page.evaluate(() => document.querySelector('#ctl00_400_header_400').click());
    await page.waitForSelector('#__tab_ctl00_ContentPlaceHolder1_tabQuickSearch_vehicleSerachClaim')
    //console.log("[✓]Enquriy page loaded successfully")
    await page.evaluate(() => document.querySelector('#__tab_ctl00_ContentPlaceHolder1_tabQuickSearch_vehicleSerachClaim').click());
    await page.waitForSelector('#ctl00_ContentPlaceHolder1_tabQuickSearch_vehicleSerachClaim_rdvehicleSearchLicense')
    //console.log("[✓]Claim section loaded successfully")
    await page.evaluate(() => document.querySelector('#ctl00_ContentPlaceHolder1_tabQuickSearch_vehicleSerachClaim_rdvehicleSearchLicense').click());
    //console.log("[✓]License tap loaded successfully")
    await page.waitForSelector('#ctl00_ContentPlaceHolder1_tabQuickSearch_vehicleSerachClaim_txtclaimSearchPersonLicNo');
    await page.focus('#ctl00_ContentPlaceHolder1_tabQuickSearch_vehicleSerachClaim_txtclaimSearchPersonLicNo');
    await page.keyboard.type(License);
    await page.evaluate(() => document.querySelector('#ctl00_ContentPlaceHolder1_tabQuickSearch_vehicleSerachClaim_btnVheicleSearchButtonClaim').click());    

    try {
        await page.waitForSelector('#ctl00_ContentPlaceHolder1_lblErrMessage')
        const textContent = await page.evaluate(() => document.querySelector('#ctl00_ContentPlaceHolder1_lblErrMessage').textContent);
        res.status(200).send( 'Result => ' + textContent );
        await browser.close();
    } catch (error) {
        //console.log("The element didn't appear.")
    }    

    try {
        await page.waitForSelector('#ctl00_ContentPlaceHolder1_tabQuickSearch_vehicleSerachClaim_grdClaimDraftSp > tbody > tr:nth-child(3) > td')
        const textContent = await page.evaluate(() => document.querySelector('#ctl00_ContentPlaceHolder1_tabQuickSearch_vehicleSerachClaim_grdClaimDraftSp > tbody > tr:nth-child(3) > td').textContent);
        res.status(200).send( 'Result => ' + textContent );
        await browser.close();
    } catch (error) {
        //console.log("The element didn't appear.")
    }   

});

包.json

{
    "name": "functions",
    "version": "0.0.1",
    "description": "Cloud Functions for Firebase",
    "dependencies": {
      "chrome-aws-lambda": "1.14.0",
      "firebase-functions": "2.2.0",
      "iltorb": "2.4.2",
      "puppeteer-core": "1.14.0",
      "firebase-admin": "7.2.0"
    },
    "engines": {
      "node": "8"
    },
    "private": true
  }

使用分配的 NodeJS 8 和 2 GB 内存与 Firebase 函数一起部署。

如何改进我的代码以加快执行时间?

【问题讨论】:

  • 您无法将云功能与本地执行进行比较。函数的开始时间与执行时间有多长?也可以在 github 上查看 this issue。您还应该衡量脚本的哪些部分运行缓慢。

标签: javascript web-scraping google-cloud-functions chromium puppeteer


【解决方案1】:

我认为期望任何给定的代码在 Cloud Functions 中的运行速度都与在任何现代桌面上一样快是不好的,尤其是不像 Puppeteer(它本质上是运行 Chrome)那么复杂。

GCF 只为任何给定的服务器实例分配一个 CPU。它没有GPU。 GCF 适用于不需要大量计算的简单工作。台式机通常有 4-8 个内核(或更多)和一个可帮助 Chrome 快速运行的 GPU。这两种情况真的没有可比性。

底线是,对于此代码,您无法加快速度使其与桌面体验保持一致。

【讨论】:

    猜你喜欢
    • 2022-11-16
    • 2020-03-19
    • 2020-11-03
    • 1970-01-01
    • 2018-08-18
    • 2019-09-09
    • 2020-05-11
    • 2019-12-22
    • 2019-06-04
    相关资源
    最近更新 更多