Firebase云功能+ Firestore性能问题

时间:2020-06-29 09:48:21

标签: firebase google-cloud-firestore

我有一个Firebase Cloud Function,它可以处理一些用户注册信息,并且鉴于该功能的实际运行情况,我发现它非常慢。通常,此功能大约需要6-7秒才能完成。我已将其范围缩小为占用最多时间(大约5秒)的那一行:

let snapshot = await admin.firestore().collection('users').doc(context.auth.uid).get();
if (!snapshot.exists) {
  throw new functions.https.HttpsError('not-found', 'User profile could not be found');
}

90%的时间内,应该获取的文档都存在。我本来希望它能以惊人的速度返回;取而代之的是,通常需要5秒钟才能返回。任何对我可能做错事情的想法都将不胜感激。

注意:一切都部署到us-central-1

以下是我进行的其中一次分析运行的日志摘要:

5:36:51.248 AM addMember Function execution started
5:36:52.256 AM addMember Checkpoint #1
5:36:52.256 AM addMember Checkpoint #2
5:36:57.253 AM addMember Checkpoint #3
5:36:57.254 AM addMember Checkpoint #4
5:36:57.597 AM addMember Checkpoint #5
5:36:57.982 AM addMember Checkpoint #6
5:36:58.051 AM addMember Function execution took 6804 ms, finished with status code: 200

上面的代码段是在Checkpoint #2Checkpoint #3之间执行的;这是这两个语句之间的唯一代码行。我已经重复执行了此功能(10次),该块完成的平均时间约为5秒。

编辑: 我已经能够缩小代码范围以尝试找出任何瓶颈。这就是我所拥有的(仍然表现出缓慢的行为)

functions / index.js:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const addMember = require('./add-member');

admin.initializeApp();

exports.addMember = functions.https.onCall(addMember(admin));

functions / add-member.js:

const functions = require('firebase-functions');
const { generateCode} = require('../common');

module.exports = (admin) => async ({ email }, context) => {
  console.log('Checkpoint #1')
  const payload = {
    code: generateCode(),
    invited: new Date(),
    joined: null
  };

  console.log('Checkpoint #2');
  let snapshot = await admin.firestore().collection('users').doc(context.auth.uid).get();
  if (!snapshot.exists) {
    throw new functions.https.HttpsError('not-found', 'User profile could not be found');
  }
  console.log('Checkpoint #3');

  return Promise.resolve(payload);
};

1 个答案:

答案 0 :(得分:0)

对于它的价值,我碰到了一些有用的东西。函数的速度不是我期望的/希望的,我开始倾向于实例的CPU速度(128MB)。无论如何,希望其他人会发现这些帖子有用:

https://stackoverflow.com/a/47985480/541277 https://github.com/firebase/functions-samples/issues/170#issuecomment-323375462