gpt4 book ai didi

node.js - 使用 Cloud Functions 和 Firestore 的 Firebase 动态页面速度很慢

转载 作者:IT老高 更新时间:2023-10-28 23:12:57 26 4
gpt4 key购买 nike

我们有 动态页面Firebase Cloud Functions 提供服务,但 TTFB 在这些页面上的 TTFB 非常慢900ms - 2s,起初我们只是假设它是一个冷启动问题,但即使流量稳定,在700ms - 1.2s的TTFB也很慢>.

这对我们的项目来说有点问题,因为它依赖于自然流量,而且 Google Pagespeed 需要小于 200ms 的服务器响应。

无论如何,我们尝试检查可能导致问题的原因并使用 Firestore 进行了定位,当云函数访问 Firestore 时,我们注意到存在一些延迟。这是我们如何实现 Cloud Function 和 Firestore 的基本示例代码:

dynamicPages.get('/ph/test/:id', (req, res) => {

var globalStartTime = Date.now();
var period = [];

db.collection("CollectionTest")
.get()
.then((querySnapshot) => {

period.push(Date.now() - globalStartTime);

console.log('1', period);

return db.collection("CollectionTest")
.get();

})
.then((querySnapshot) => {

period.push(Date.now() - globalStartTime);

console.log('2', period);

res.status(200)
.send('Period: ' + JSON.stringify(period));

return true;

})
.catch((error) => {

console.log(error);
res.end();

return false;

});

});

这是在 Firebase + Cloud Functions + NodeJS

上运行的

CollectionTest 非常小,里面只有 100 个文档,每个文档都有以下字段:

directorName: (string)
directorProfileUrl: (string)
duration: (string)
genre: (array)
posterUrl: (string)
rating: (string)
releaseDate: (string)
status: (int)
synopsis: (string)
title: (string)
trailerId: (string)
urlId: (string)

通过这个测试,我们会得到以下结果:

[467,762] 1.52s
[203,315] 1.09s
[203,502] 1.15s
[191,297] 1.00s
[206,319] 1.03s
[161,267] 1.03s
[115,222] 843ms
[192,301] 940ms
[201,308] 945ms
[208,312] 950ms

此数据为 [Firestore 调用 1 执行时间Firestore 调用 2 执行时间] TTFB

如果我们检查测试结果,有迹象表明 TTFB 正在变低,可能是 Cloud Function 已经预热的时候?但即便如此,根据我们第二次 Firestore 调用的结果,Firestore 在 Cloud Function 中消耗了 200-300 毫秒,即使 Firestore 执行时间较短,TTFB 仍将占用 600-800 毫秒,但那是另一回事。

无论如何,任何人都可以帮助我们如何提高 Cloud Functions 中的 Firestore 性能(或者如果可能的话,提高 TTFB 性能)?也许我们正在做一些我们不知道的明显错误的事情?

最佳答案

我会尽力提供帮助,但在返回 dynamicPages 之前可能缺少一些关于您加载的内容的上下文,但这里有一些线索:

首先,最明显的部分(无论如何我都要指出来):

1 - 注意如何测量 TTFB:

Measuring TTFB remotely means you're also measuring the network latency at the same time which obscures the thing TTFB is actually measuring: how fast the web server is able to respond to a request.

2 - 来自关于 Understanding Resource Timing (here) 的 Google Developers 文档:

[...]. Either:

Bad network conditions between client and server, or
A slowly responding server application

To address a high TTFB, first cut out as much network as possible. Ideally, host the application locally and see if there is still a big TTFB. If there is, then the application needs to be optimized for response speed. This could mean optimizing database queries, implementing a cache for certain portions of content, or modifying your web server configuration. There are many reasons a backend can be slow. You will need to do research into your software and figure out what is not meeting your performance budget.

If the TTFB is low locally then the networks between your client and the server are the problem. The network traversal could be hindered by any number of things. There are a lot of points between clients and servers and each one has its own connection limitations and could cause a problem. The simplest method to test reducing this is to put your application on another host and see if the TTFB improves.

不那么明显的:

您可以在此处查看有关 Cloud Functions Performance 的官方 Google 文档:https://cloud.google.com/functions/docs/bestpractices/tips

您之前需要一些文件吗?

根据 Firebase 云功能的这个答案很慢:Firebase cloud functions is very slow :

Looks like a lot of these problems can be solved using the hidden variable process.env.FUNCTION_NAME as seen here: https://github.com/firebase/functions-samples/issues/170#issuecomment-323375462

这些加载的动态页面是由 guest 用户还是 logged 用户访问的?因为可能第一个请求要理清身份验证细节,所以众所周知比较慢……

如果这些都不起作用,我将看看常见的性能问题,例如数据库连接(here : 优化数据库性能)、改进服务器配置、尽可能缓存并小心您的应用程序中可能的重定向...

最后,通过互联网阅读,您的问题有很多线程(简单的 Cloud Functions 性能低下)。喜欢这个:https://github.com/GoogleCloudPlatform/google-cloud-node/issues/2374 && 在 S.O: https://stackoverflow.com/search?q=%5Bgoogle-cloud-functions%5D+slow

带有如下评论:

since when using cloud functions, the penalty is incurred on each http invocation the overhead is still very high (i.e. 0.8s per HTTP call).

或:

Bear in mind that both Cloud Functions and Cloud Firestore are both in beta and provide no guarantees for performance. I'm sure if you compare performance with Realtime Database, you will see better numbers.

也许这仍然是一个问题。

希望对你有帮助!

关于node.js - 使用 Cloud Functions 和 Firestore 的 Firebase 动态页面速度很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50665931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com