node.js - Google Cloud 上的 GCS 功能比本地主机慢

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

我在 Google 云端存储中有一个文件。我需要从使用它来响应 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!");
    });
};

我的电脑上的典型执行:

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 上的典型执行: GCF logs

最佳答案

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

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!");
    });
};

关于node.js - Google Cloud 上的 GCS 功能比本地主机慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59011731/

相关文章:

java - 创建 Google Cloud Bucket (Java API) 时未找到项目参数

java - 我们是否需要在系统中安装gsutil工具来访问google Cloud

java.lang.NoSuchMethodError : com. google.common.base.CharMatcher.ascii() 错误

javascript - 如何询问 Meteor 为旧的 Nodejs 构建代码

node.js - 如何创建特定于用户的实体?

javascript - Twilio 不在 firebase 云功能内发送短信

FireBase sendMessage 函数更新到 v1 Google Cloud Endpoint

android - 在 Firebase Cloud Function 中获取 Android 用户数据

javascript - 为什么在 Node.js 中使用方法覆盖包来隐藏字段?

javascript - 为什么我在 console 中打印 mongoose 的模型对象时看到额外的字段。我正在将 Node JS 与 Mongoose 一起使用。我怎样才能隐藏这些属性