Google Cloud Function额外500ms延迟排查求助
Firebase HTTPS 云函数额外延迟排查求助
我在us-central区域部署了Firebase HTTPS V1云函数,通过Cloud Logging查看其执行耗时不足100ms,但实际调用总耗时约600ms。已经用Benchmarkify循环调用排除了冷启动问题,且同区域部署的其他云函数也存在类似延迟,请求协助排查该额外500ms延迟的原因。
测试日志
"2024-08-11T01:29:23.326906Z","benchmark0","Platform info:" "2024-08-11T01:29:23.326970Z","benchmark0","==============" "2024-08-11T01:29:23.327112Z","benchmark0"," Linux 4.4.0 x64" "2024-08-11T01:29:23.327180Z","benchmark0"," Node.JS: 20.15.1" "2024-08-11T01:29:23.327271Z","benchmark0"," V8: 11.3.244.8-node.23" "2024-08-11T01:29:23.328138Z","benchmark0"," CPU: unknown × 2" "2024-08-11T01:29:23.328690Z","benchmark0"," Memory: 1 GB" "2024-08-11T01:29:23.328768Z","benchmark0","" "2024-08-11T01:29:23.332129Z","benchmark0","Suite: octree.bin" "2024-08-11T01:29:23.332281Z","benchmark0","=================" "2024-08-11T01:29:23.332367Z","benchmark0","" "2024-08-11T01:29:23.332876Z","benchmark0","- Running 'A'..." "2024-08-11T01:29:23.333353Z","benchmark0","querying 1" "2024-08-11T01:29:24.482909Z","app","Function execution started" "2024-08-11T01:29:24.708106Z","app","Function execution took 225 ms, finished with status code: 206" "2024-08-11T01:29:24.746945Z","benchmark0","result" "2024-08-11T01:29:24.747128Z","benchmark0","querying 2" "2024-08-11T01:29:25.054992Z","app","Function execution started" "2024-08-11T01:29:25.122878Z","app","Function execution took 67 ms, finished with status code: 206" "2024-08-11T01:29:25.219459Z","benchmark0","result" "2024-08-11T01:29:25.219596Z","benchmark0","querying 3" "2024-08-11T01:29:25.364295Z","app","Function execution started" "2024-08-11T01:29:25.506003Z","app","Function execution took 141 ms, finished with status code: 206" "2024-08-11T01:29:25.523353Z","benchmark0","result" "2024-08-11T01:29:25.523463Z","benchmark0","querying 4" "2024-08-11T01:29:25.851368Z","app","Function execution started" "2024-08-11T01:29:25.917669Z","app","Function execution took 66 ms, finished with status code: 206" "2024-08-11T01:29:26.021476Z","benchmark0","result" "2024-08-11T01:29:26.021624Z","benchmark0","querying 5" "2024-08-11T01:29:26.267686Z","app","Function execution started" "2024-08-11T01:29:26.342285Z","app","Function execution took 74 ms, finished with status code: 206" "2024-08-11T01:29:26.420235Z","benchmark0","result" "2024-08-11T01:29:26.932175Z","benchmark0","✔ A* 2 ops/sec" "2024-08-11T01:29:26.932535Z","benchmark0","" "2024-08-11T01:29:26.933566Z","benchmark0"," A* (#) 0% (2 ops/sec) (avg: 617ms)" "2024-08-11T01:29:26.933774Z","benchmark0","" "2024-08-11T01:29:26.934018Z","benchmark0","┌───┬────────────────────────────────────────────────────┐" "2024-08-11T01:29:26.934206Z","benchmark0","│ A │ ██████████████████████████████████████████████████ │" "2024-08-11T01:29:26.934268Z","benchmark0","└───┴────────────────────────────────────────────────────┘" "2024-08-11T01:29:26.934459Z","benchmark0","----------------------------------------------------------------------- "
云函数精简代码
const functions = require('firebase-functions'); const express = require('express'); const app = express(); const { initializeApp } = require("firebase-admin/app"); initializeApp(); app.get('/app/pointcloud/*/content/*', async (req, res) => { //从Firebase Storage下载内容 }) exports.app = functions.https.onRequest(app);
基准测试函数代码
const functions = require('@google-cloud/functions-framework'); const Benchmarkify = require("benchmarkify"); functions.http('helloHttp', (req, res) => { const benchmark = new Benchmarkify().printHeader() var instance = 0 // 创建测试套件 benchmark.createSuite("octree.bin", { time: 1000, minSamples: 5 }) .ref("A", (done) => { instance += 1; console.log(`querying ${instance}`) const url = "https://myapp/app/pointcloud/xxx/content/octree.bin" fetch(url, { headers: { "x-range": `bytes=${instance * 100000}-${(instance + 1) * 100000}` } }).then(() => { console.log('result') done() }) }); benchmark.run().then(result => console.log(result)); });
排查方向建议
- 负载均衡层延迟:Firebase云函数请求会先经过Google前端负载均衡,这部分耗时不会计入函数执行时间。可以用同区域GCE虚拟机直接调用函数内部逻辑(跳过负载均衡),对比耗时差异。
- 请求预处理开销:检查是否存在未显式处理的认证、CORS或请求解析逻辑,这些步骤在函数执行开始前运行,不会出现在Cloud Logging的执行耗时统计中。
- Storage调用延迟:单独测试Firebase Storage的下载耗时,确认是否是跨区域调用或内部链路问题导致额外延迟。
- 函数资源限制:当前函数配置1GB内存/2核CPU,若遇到实例并发请求争抢资源,可能引发调度延迟。尝试提高资源配置,观察延迟变化。
- 测试环境额外开销:确认基准测试函数与目标函数是否在同一区域,跨区域调用会增加网络往返耗时;同时检查Benchmarkify测试逻辑是否存在额外等待或处理步骤。
内容的提问来源于stack exchange,提问作者FloatingKiwi
相关产品推荐
相关产品推荐

