问题
I got this from google cloud console. I don't understand why after the function is executed, it should turn hot for some duration. However, after deploying the function and call it, I found cold start and then drop to actual and then increase to cold start again. Please help!
// index.js
import * as functions from 'firebase-functions'
import express from 'express'
import cors from 'cors'
import auth from 'controllers/auth'
const authApp = express()
authApp.use(cors({ origin: true }))
authApp.use(auth)
authApp.use('*', unknownPathHandler, errorMiddleware)
const authApi = functions.https.onRequest(authApp)
exports.auth = authApi
// controllers/auth.js
app.post('/user', verifySecretKey, (req, res, next) => {
const { email, password } = req.body
return appFirebase.auth().signInWithEmailAndPassword(email, password)
.then(() => {
return appFirebase.auth().currentUser.getIdToken().then((token) => {
return res.end(token)
})
})
.catch((err) => next(err))
})
firebase version
"firebase-admin": "5.12.0",
"firebase-functions": "1.0.2",
回答1:
There is potential for a lot of variance in you function. It's doing all of the following tasks, none of which have guarantees how long they will take:
- Using Firebase Auth to effectively sign in a user
- Fetching an ID token for that user
- Sending a response to the client, wherever in the world they are, with whatever connection speed.
If you want to understand the performance characteristics of your function, you should profile each one of these steps. The sending of the response may not be possible to benchmark, and could be highly variable based on their physical location and connection speed.
If you have solid benchmarks that suggest that Cloud Functions is underperforming compared to expectations, please send those to Firebase support. https://firebase.google.com/support/
来源:https://stackoverflow.com/questions/51759752/firebase-cloud-functions-execution-time-is-so-fluctuate