Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 傀儡程序在Google云函数上执行缓慢_Javascript_Web Scraping_Google Cloud Functions_Chromium_Puppeteer - Fatal编程技术网

Javascript 傀儡程序在Google云函数上执行缓慢

Javascript 傀儡程序在Google云函数上执行缓慢,javascript,web-scraping,google-cloud-functions,chromium,puppeteer,Javascript,Web Scraping,Google Cloud Functions,Chromium,Puppeteer,我正在谷歌云功能上使用木偶 经过几次测试后,我注意到我的代码部署在Google云功能基础设施上平均需要56秒,而本地测试的相同功能只需要13秒 index.js const chromium = require('chrome-aws-lambda'); const puppeteer = require('puppeteer-core'); const functions = require('firebase-functions'); exports.check = functions.h

我正在谷歌云功能上使用木偶

经过几次测试后,我注意到我的代码部署在Google云功能基础设施上平均需要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.")
    }   

});
Package.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和2GB内存部署Firebase函数


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

我认为,期望任何给定代码在云功能中的运行速度与在任何现代桌面上的运行速度一样快是不好的,特别是不像Puppeter(基本上运行的是Chrome)那样复杂

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


底线是,对于这段代码,您无法做太多的工作来加速它,使其与桌面体验保持一致。

您无法将云功能与本地执行进行比较。函数的开始时间与执行时间相比有多长?还可以在github上签出。您还应该测量脚本的哪些部分比较慢。