我在Google Cloud Storage中有一个文件。我需要通过Google Cloud Function访问它以响应http请求。
代码在我的PC上像“功能框架”一样运行,并在大约半秒钟内检索了文件。
当我将代码放在存储所在区域的Google Cloud Functions中时,我希望它能运行得更快(至少可以节省从米兰家到比利时Google位置的网络旅行)。在GCF上运行的代码平均需要大约 5 秒(有时更长)。
我尽了我所能想像的一切,但没有任何明显的效果。 我已经尝试过的一些东西:
现在我的想法不多了...
相关代码为简化(为了测试和清楚起见):
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
答案 0 :(得分: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!");
});
};