【问题标题】:GCS on Google Cloud Functions slower than localhostGoogle Cloud Functions 上的 GCS 比 localhost 慢
【发布时间】:2020-03-19 13:23:56
【问题描述】:

我在 Google Cloud Storage 中有一个文件。我需要从使用它来响应 http 请求的 Google Cloud Function 访问它。

代码在我的电脑上运行起来就像一个带有“功能框架”的魅力,并在大约半秒内检索到文件。

当我将代码放在与存储相同的区域的 Google Cloud Functions 上时,我预计它会运行得更快(至少节省了从我在米兰的家到 Google 在比利时的位置的网络旅行)。令我惊讶的是,同样的在 GCF 上运行的代码平均需要 5 秒(有时更长)。

我尝试了我能想象到的一切,但没有任何显着影响。 我已经尝试过的一些事情:

  • 更改 Node 运行时版本
  • 导入 fast-crc32c 依赖项
  • 请求 ReadableStream 并将其直接传送到 res 对象

现在我的想法不多了……

相关代码是(为了测试和清晰而简化):

const { Storage } = require("@google-cloud/storage");

exports.helloWorld = (req, res) => {
  const storage = new Storage({ projectId: "my project ID" });
  const bucketName = "my-bucket-name";
  const srcFilename = "my-file-name.json";

  let file = storage.bucket(bucketName).file(srcFilename);
  file
    .download()
    .then(function(data) {
      const d = data;
    })
    .then(() => {
      res.status(200).send("Ciao!");
    });
};

非常感谢任何帮助或想法!

谢谢,

菲利波


PS 我添加了用于测量时间的代码的检测版本:

const { Storage } = require("@google-cloud/storage");

exports.helloWorld = (req, res) => {
  var hrstart = process.hrtime();
  var timedEvents = [];
  function addTimedEvent(msg) {
    const event = { msg: msg, hrend: process.hrtime(hrstart) };
    timedEvents = [...timedEvents, event];
  }
  function printTimeEvents() {
    for (var e of timedEvents) {
      console.info(
        "Execution time (hr): %ds %dms Message:%s",
        e.hrend[0],
        e.hrend[1] / 1000000,
        e.msg
      );
    }
  }
  addTimedEvent("gcs 1");
  const storage = new Storage({ projectId: "my project ID" });
  const bucketName = "my-bucket-name";
  const srcFilename = "my-file-name.json";

  addTimedEvent("gcs 2");
  let file = storage.bucket(bucketName).file(srcFilename);
  addTimedEvent("gcs 3");
  file
    .download()
    .then(function(data) {
      addTimedEvent("gcs 4");
      const d = data;
      addTimedEvent("gcs 5");
    })
    .then(() => {
      printTimeEvents();
      res.status(200).send("Ciao world!");
    });
};

在我的 PC 上的典型执行:

Execution time (hr): 0s 0.0059ms Message:gcs 1
Execution time (hr): 0s 0.1911ms Message:gcs 2
Execution time (hr): 0s 0.7464ms Message:gcs 3
Execution time (hr): 0s 338.6312ms Message:gcs 4
Execution time (hr): 0s 338.6361ms Message:gcs 5

GCF 上的典型执行:

【问题讨论】:

  • 每次调用都很慢还是第一次?如果需要创建新实例来处理请求(例如,通常在部署后立即创建),云函数将承受cold start 惩罚。
  • @robsiemb:每次调用。可以肯定的是,我测量了file.download() 的时间,以消除冷启动的任何影响,但不......不幸的是,这不是冷启动问题。
  • 请编辑问题,准确显示您用于基准时间的内容。如果您不在代码本身中执行此操作,那么您测量的可能不仅仅是传输本身。
  • @DougStevenson 我添加了检测代码,希望对您有所帮助
  • 您的指标是否与 stackdriver Monitoring 上的指标相匹配?

标签: node.js google-cloud-functions google-cloud-storage


【解决方案1】:

设置可以在函数外部的调用之间共享的任何内容,例如(如果存储桶没有更改):

const { Storage } = require("@google-cloud/storage");
const storage = new Storage({ projectId: "my project ID" });
const bucketName = "my-bucket-name";
const bucket = storage.bucket(bucketName);

exports.helloWorld = (req, res) => {
  const srcFilename = "my-file-name.json";

  let file = bucket.file(srcFilename);
  file
    .download()
    .then(function(data) {
      const d = data;
    })
    .then(() => {
      res.status(200).send("Ciao!");
    });
};

【讨论】:

  • 非常感谢 Jak 的建议。我做了你的建议,并尽可能多地投入到静态缺点中。它有所帮助,尤其是当我将最大实例设置为 1 以减少冷启动时。现在我在冷启动时得到 5 秒,在随后的调用中得到 2 秒、2.5 秒、3 秒。不幸的是,这仍然比我在 PC 上得到的 0.4s、0.6s、0.8s 长得多。我不确定是否还有其他可以尝试的方法,非常感谢。菲利波
猜你喜欢
  • 1970-01-01
  • 2020-11-03
  • 1970-01-01
  • 2020-04-22
  • 2017-11-29
  • 2018-06-21
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多