【问题标题】:Firestore operations in Cloud functions slow compared to web client与 Web 客户端相比,云功能中的 Firestore 操作速度较慢
【发布时间】:2020-09-11 16:14:42
【问题描述】:

我没有在网上找到很多关于这个问题的信息,感觉就像我是唯一遇到这个问题的用户。 我阅读了一些关于人们如何衡量性能的 cmets,并在下面包含了代码和测量结果。

我正在测试 Firestore 的 3 个操作:

  • 阅读
  • onSnapshot(写调用和通知通知之间的时间) 改变)

在 3 个环境中:Web 客户端、模拟器、云功能生产 (nodejs10)。

网络客户端的性能非常好,即时。 模拟器很棒,即时(见下面的结果)。

生产缓慢,数字四舍五入。

  • 读取:3700 毫秒
  • 写入:2900 毫秒
  • 快照:2704 毫秒

这是在臭名昭著的冷启动之后。

我在这里遗漏了什么明显的东西吗?

代码:

function epochms() {
  return Math.floor(new Date());
}

async function run10Times() {
  for (var i = 0; i < 10; ++i)
    await updateRecord("Tests/manager/Updates", i.toString().padStart(2, '0'));
}

async function updateRecord(colpath, id) {
  let obj = {id};
  let startRead = epochms();
  await db.collection("Tests").doc("manager").get();
  let startWrite = epochms();
  await db.collection(colpath).doc(obj.id).set({startRead, endRead:startWrite, startWrite});
  let endWrite = epochms();
  await db.collection(colpath).doc(obj.id).update({endWrite});
  db.collection(colpath).doc(obj.id).onSnapshot(async (doc) => {
    log.log(`[${id}] test.onSnapshot`);
    let snapshotReceived = epochms();
    obj = { id:doc.id, ...doc.data() };
    if (obj.snapshotStarted && !obj.snapshotReceived)
      await db.collection(colpath).doc(obj.id).update({snapshotReceived});
    else 
      log.log(`[${id}] test.onSnapshot late, ignore`);
  });
  let snapshotStarted = epochms();
  await db.collection(colpath).doc(obj.id).update({snapshotStarted});
}

exports.emulatorScheduledEventUpdate = functions.https.onCall(() => {
  run10Times();
  return 0;
});

云函数中的输出

00
Read: 91192ms
Write: 5301ms
Snapshot: 16000ms

01
Read: 12400ms
Write: 2700ms
Snapshot: 4501ms

02
Read: 4201ms
Write: 2700ms
Snapshot: 3901ms

03
Read: 4301ms
Write: 900ms
Snapshot: 3300ms

04
Read: 4599ms
Write: 1901ms
Snapshot: 4901ms

05
Read: 4400ms
Write: 2302ms
Snapshot: 2700ms

06
Read: 4200ms
Write: 900ms
Snapshot: 2396ms

07
Read: 4900ms
Write: 1099ms
Snapshot: 4602ms

08
Read: 2300ms
Write: 1500ms
Snapshot: 2500ms

09
Read: 3300ms
Write: 1900ms
Snapshot: 2400ms

模拟器输出

00
Read: 238ms
Write: 27ms
Snapshot: 18ms

01
Read: 7ms
Write: 13ms
Snapshot: 17ms

02
Read: 5ms
Write: 12ms
Snapshot: 12ms

03
Read: 5ms
Write: 16ms
Snapshot: 16ms

04
Read: 4ms
Write: 24ms
Snapshot: 18ms

05
Read: 4ms
Write: 26ms
Snapshot: 16ms

06
Read: 4ms
Write: 31ms
Snapshot: 20ms

07
Read: 6ms
Write: 28ms
Snapshot: 17ms

08
Read: 5ms
Write: 34ms
Snapshot: 26ms

09
Read: 5ms
Write: 10ms
Snapshot: 18ms

package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "main": "dist/index.js",
  "scripts": {
    "lint": "xo",
    "build": "babel 'src' --out-dir 'dist' --source-maps",
    "watch": "yarn build --watch",
    "serve": "yarn watch & yarn firebase emulators:start --only firestore,functions --inspect-functions",
    "predeploy": "yarn build",
    "deploy": "yarn firebase deploy --only functions",
    "fdeploy": "yarn firebase deploy --only functions:tests-scheduledEventUpdate",
    "fdelete": "firebase functions:delete tests-scheduledEventUpdate",
    "logs": "watch -n 5 firebase functions:log -n 90"
  },
  "engines": {
    "node": "10"
  },
  "dependencies": {
    "@grpc/grpc-js": "^1.0.3",
    "core-js": "^3.6.5",
    "debug": "^4.1.1",
    "firebase": "^7.14.2",
    "firebase-admin": "^8.11.0",
    "firebase-functions": "^3.6.1",
    "google-gax": "^2.3.1",
    "grpc": "^1.24.2",
    "request": "^2.88.2"
  },
  "devDependencies": {
    "@babel/cli": "^7.4.3",
    "@babel/core": "^7.4.3",
    "@babel/preset-env": "^7.4.3",
    "firebase-functions-test": "^0.2.1",
    "firebase-tools": "^8.1.1",
    "xo": "^0.24.0"
  },
  "private": true
}

谢谢,

【问题讨论】:

  • 您使用的是哪个运行时:节点 8、10 或 12?如果添加 package.json 文件也会很有用。
  • 我使用的是节点 10。

标签: javascript firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

我在某处读到,如果代码在函数范围之外运行,资源访问会大大减少。所以我尝试更改此代码:

exports.emulatorScheduledEventUpdate = functions.https.onCall(() => {
  run10Times();
  return 0;
});

exports.emulatorScheduledEventUpdate = functions.https.onCall(() => {
  return run10Times();
});

这个函数现在返回一个promise,而且数字很不一样:

00
Read: 52ms
Write: 75ms
Snapshot: 74ms

01
Read: 35ms
Write: 98ms
Snapshot: 72ms

02
Read: 33ms
Write: 114ms
Snapshot: 68ms

03
Read: 198ms
Write: 37ms
Snapshot: 72ms

04
Read: 37ms
Write: 68ms
Snapshot: 76ms

05
Read: 32ms
Write: 121ms
Snapshot: 74ms

06
Read: 44ms
Write: 54ms
Snapshot: 81ms

07
Read: 35ms
Write: 71ms
Snapshot: 67ms

08
Read: 33ms
Write: 93ms
Snapshot: 71ms

09
Read: 49ms
Write: 103ms
Snapshot: 41ms

firebase 团队注意:警告会很有用(尤其是在模拟器中),并且可能会在执行后设置 1 秒的硬限制,而不是令人困惑的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 2015-08-14
    • 1970-01-01
    • 2010-12-16
    相关资源
    最近更新 更多