为什么 Firebase Cloud Function 部署后会比在模拟器中慢得多,而不是冷启动?

Why would a Firebase Cloud Function be much slower once deployed than in the emulator, other than cold starts?

我有一个 Firebase Cloud Function,可以解析一些 HTML 等等。我测量了 HTML-parsing 方法本身,它在 MacBook Pro 上的模拟器中大约需要 300ms。部署到 Firebase 时似乎需要大约 4-8 秒,这对我的应用程序来说是不能容忍的。

我正在使用 Dart 使用 firebase_functions_interop 编写我的 Firebase 云函数,并使用 Dart2JS 编译为 JS。在不同环境之间表现截然不同的方法实际上是 Dart 的内置 HTML 解析方法。我知道冷启动是云函数花费比预期更长的时间的常见原因,但似乎这不是这里的罪魁祸首,因为我正在明确测量解析 HTML 所需的时间,这就是我的地方我看到了巨大的差异。

Cloud Function 的生产速度 运行 比 MacBook Pro 慢得多吗?

部署 firebase 云函数时,您可以自定义 runtime environment. But here, there isn't CPU capacity. If you have a look to Cloud Function documentation directly(Firebase 云函数由 Google 云函数支持)您可以看到内存量与 CPU 权力.

注意:4Gb内存=2CPU@2.4Ghz,不是1CPU(一个线程)@4.8Ghz

所以现在,使用 2Gb 内存部署你的 Firebase Cloud Function,速度应该会更好(默认为 256Mb)。但请记住这一点:Cloud Function 是单声道 CPU,并且限于 2.4Ghz。您的 macbook pro 应该有 8 或 16 CPU,并且具有 3.4Ghz 的处理能力。