加快云功能 [英] Speeding up Cloud Functions

查看:75
本文介绍了加快云功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的函数,它仅执行事务以帮助保持列表计数.

I have a simple function that just executes a transaction to help keep a count of a list.

但是,我注意到当我运行它时,它花了将近5秒钟的时间执行,对于函数的简单程度来说似乎确实很慢.有什么我可以做的,或者是一种更快的方式来保持计数器?

However, I am noticing when I run it, it takes nearly 5 seconds to execute, which seems really slow for how simple the function is. Is there anything I can do or a faster way to keep a counter?

exports.CountCommentsUp = functions.firestore.document('Groups/{groupID}/TextFeedActive/{postID}/Comments/{commentID}').onCreate(event => {
  // ref to the parent document
  const docRef = admin.firestore().collection('Groups/' + event.params.groupID+ '/Feed/').doc(event.params.postID);

  //Along with Creating Counter, We need to create Notification REF
  return admin.firestore().runTransaction(function(transaction) {
      return transaction.get(docRef).then(function(sfDoc) {
          var newCC = sfDoc.data().CommentCount + 1;
          transaction.update(docRef, { CommentCount: newCC });
          return newCC;

      });
    })
}); 

我看了很多遍,它确实有效,只是感觉真的很慢.是否有其他方法可以做到这一点?为了使数据库具有实时性,最好还具有更快的计数器变量

I looked through it a bunch of times, and it definitely works, just feels really slow. Is there an alternate way to do this? For the database to feel realtime it would be great to have really faster counter variables as well

推荐答案

供将来的读者使用:

GCP文档的部分,他们在其中进行了讨论关于改善云功能性能的方法.

There is section of the GCP documentation, where they discuss about ways of improving cloud function performance.

从文档中引用:

明智地使用依赖项

由于函数是无状态的,因此执行环境通常是 从头开始初始化(在所谓的冷启动期间).什么时候 发生冷启动时,将评估该函数的全局上下文.

Because functions are stateless, the execution environment is often initialized from scratch (during what is known as a cold start). When a cold start occurs, the global context of the function is evaluated.

如果您的函数导入模块,则这些模块的加载时间可以 在冷启动期间增加了调用延迟.你可以减少这个 延迟以及部署功能所需的时间 正确加载依赖项而不加载依赖项 功能不使用.

If your functions import modules, the load time for those modules can add to the invocation latency during a cold start. You can reduce this latency, as well as the time needed to deploy your function, by loading dependencies correctly and not loading dependencies your function doesn't use.

使用全局变量在将来的调用中重用对象

不能保证云功能的状态为 保留以供将来调用.但是,云功能经常 回收先前调用的执行环境.如果你 在全局范围内声明变量,其值可以在以下位置重用 随后的调用,而不必重新计算.

There is no guarantee that the state of a Cloud Function will be preserved for future invocations. However, Cloud Functions often recycles the execution environment of a previous invocation. If you declare a variable in global scope, its value can be reused in subsequent invocations without having to be recomputed.

这样,您可以缓存可能在其上重新创建的对象可能很昂贵的对象 每个函数调用.从功能主体中移动此类对象 扩展到全球范围可能会显着提高性能. 以下示例每个函数仅创建一次重对象 实例,并在到达 给定实例: 在全局范围内缓存网络连接,库引用和API客户端对象尤其重要.有关示例,请参见优化网络.

This way you can cache objects that may be expensive to recreate on each function invocation. Moving such objects from the function body to global scope may result in significant performance improvements. The following example creates a heavy object only once per function instance, and shares it across all function invocations reaching the given instance: It is particularly important to cache network connections, library references, and API client objects in global scope. See Optimizing Networking for examples.

对全局变量进行延迟初始化

如果在全局范围内初始化变量,则初始化代码 将始终通过冷启动调用执行,从而增加了 功能的延迟.如果某些对象未在所有代码路径中使用, 考虑根据需要延迟初始化它们:

If you initialize variables in global scope, the initialization code will always be executed via a cold start invocation, increasing your function's latency. If some objects are not used in all code paths, consider initializing them lazily on demand:

如果您在 单个文件,不同的函数使用不同的变量.除非 您使用延迟初始化,可能会浪费资源, 已初始化但从未使用过.

This is particularly important if you define several functions in a single file, and different functions use different variables. Unless you use lazy initialization, you may waste resources on variables that are initialized but never used.

您还可以阅读Google开发者倡导者写的文章:

You may also read the article written by a Google developer advocate : Improving Cloud Function cold start time. To summarizing the key points made in the article (for speeding up cloud functions)

  • 整理依赖项.
  • 使用依赖项缓存.
  • 延迟加载
  • Trimming dependencies.
  • Using dependencies cache.
  • Lazy loading

这篇关于加快云功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆