[英]Async/Await Cloud functions for Firebase, slow performance
I need some help and support in solving a performance problem in my cloud functions for Firebase.我需要一些帮助和支持来解决 Firebase 云函数中的性能问题。 I'm using async/await in combination with try/catch blocks to make my code more readable.
我将 async/await 与 try/catch 块结合使用,以使我的代码更具可读性。
I have a simple function that writes on two locations in the database, based on params key value from a queue write event.我有一个简单的函数,它根据队列写入事件中的 params 键值在数据库中的两个位置写入。
This simple function takes on average 1500ms to complete.这个简单的函数平均需要 1500 毫秒才能完成。 Seems a little long in my opinion for a simple write to database task.
在我看来,对于简单的数据库写入任务来说似乎有点长。 I think that I made a mistake in my code and that a part of the code is not resolving correctly.
我认为我在代码中犯了一个错误,并且部分代码没有正确解析。
Cloud function云功能
export const watchRegisterJobs = functions.database.ref(`queues/register/{$key}`).onWrite(
async (event: Event<any>) => {
if (!event.data.exists()) {
return;
}
if (event.data.exists()) {
const formData = event.data.val();
let key;
event.params&&(key=event.params.$key);
if (key) {
console.log('New user with the following id', key)
try {
await addUser(formData, key)
} catch (error) {
console.log('can not add user to database', error)
}
try {
await addOrganization(formData, key)
} catch (error) {
console.log('can not add organization to database', error)
}
try {
await event.data.ref.remove();
} catch (error) {
console.log('can not remove job')
}
return;
}
}
}
)
Helper functions辅助函数
export const addUser = async (data, key) => {
const dataToSave = {
information: ....,
organizations: .....
};
try {
return await admin.database().ref(`users/${key}`).set(dataToSave);
} catch (error) {
return error;
}
}
export const addOrganization = async (data, key) => {
const organization = {
...
}
try {
return await admin.database().ref(`organizations/${key}`).set(organization);
} catch (error) {
return error;
}
}
Update Based on the comments I have made some improvements to the code.更新根据评论,我对代码进行了一些改进。 Performance is still the same, but code looks better.
性能仍然相同,但代码看起来更好。
Cloud function云功能
export const watchRegisterJobs = functions.database.ref(`queues/register/{$key}`).onWrite(
async (event: Event<any>) => {
if (!event.data.exists()) {
return;
}
const formData = event.data.val();
let key;
if(event.params) {
key = event.params.$key;
}
if (key) {
console.log('New user with the following id', key)
try {
await addUser(formData, key)
await addOrganization(formData, key)
await event.data.ref.remove();
} catch (error) {
console.log('can not write to the database', error)
}
return;
}
}
)
Helpers帮手
export const addUser = (data, key) => {
const dataToSave = {
information: ....,
organizations: ....
};
return admin.database().ref(`users/${key}`).set(dataToSave);
}
export const addOrganization = (data, key) => {
const organization = {
...
}
return admin.database().ref(`organizations/${key}`).set(organization);
}
Seeing as your code looks alright (I'm not seeing any codepaths that doesn't return), I just did my own test with async await, and here are my results:看到你的代码看起来没问题(我没有看到任何不返回的代码路径),我只是用异步等待做了我自己的测试,这是我的结果:
I suspect that you are running into a cold-start of your functions.我怀疑您的功能正在冷启动。 The first time you execute a function after a code update, Firebase needs to setup the environment, which takes a while the first time.
代码更新后第一次执行函数时,Firebase 需要设置环境,第一次需要一段时间。 The same happens when the function hasn't been used for a while.
当函数有一段时间没有使用时,也会发生同样的情况。
More info here by one of the devs: Firebase cloud functions is very slow其中一位开发人员提供的更多信息: Firebase 云功能非常慢
EDIT: Forgot to mention that I'm using typescript.编辑:忘了提到我正在使用打字稿。 Meaning my code will obviously be transpiled, as Firebase doesn't natively support async/await yet.
这意味着我的代码显然会被转译,因为 Firebase 本身还不支持 async/await。 I suspect you are doing something similar.
我怀疑你正在做类似的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.