Ottimizzazione del networking (1ª generazione)
La semplicità delle funzioni Cloud Run ti consente di sviluppare rapidamente il codice ed eseguirlo in un ambiente serverless. Su scala moderata, il costo di esecuzione delle funzioni è basso, e l'ottimizzazione del codice potrebbe non sembrare una priorità elevata. Tuttavia, man mano che il deployment viene scalato, l'ottimizzazione del codice diventa sempre più importante.
Questo documento descrive come ottimizzare il networking per le tue funzioni. Alcuni di i vantaggi dell'ottimizzazione del networking sono i seguenti:
- Riduci il tempo di CPU impiegato per stabilire nuove connessioni a ogni chiamata di funzione.
- Riduci la probabilità di esaurire la connessione o il DNS quote di spazio di archiviazione.
Mantenere le connessioni permanenti
Questa sezione fornisce esempi di come mantenere le connessioni permanenti in un personalizzata. In caso contrario, le quote di connessione potrebbero esaurirsi rapidamente.
In questa sezione vengono trattati i seguenti scenari:
- HTTP/S
- API di Google
Richieste HTTP/S
Il seguente snippet di codice ottimizzato mostra come mantenere le connessioni permanenti anziché creare una nuova connessione a ogni chiamata di funzione:
Node.js
Python
Vai
PHP
Ti consigliamo di utilizzare Guzzle PHP HTTP Framework di inviare richieste HTTP, poiché gestisce le connessioni automaticamente.
Accesso alle API di Google
L'esempio seguente utilizza Cloud Pub/Sub, ma questo approccio funziona anche per altre librerie client, ad esempio Cloud Natural Language o Cloud Spanner. Tieni presente che i miglioramenti del rendimento possono dipendere dall'implementazione corrente di determinate librerie client.
La creazione di un oggetto client Pub/Sub genera una connessione e due DNS query per chiamata. Per evitare connessioni e query DNS non necessarie, crea l'oggetto client Pub/Sub a livello globale come mostrato nel seguente esempio:
Node.js
Python
Vai
Reimpostazione delle connessioni in uscita
Gli stream di connessione dalla funzione sia al VPC sia a internet possono essere occasionalmente interrotti e sostituiti quando l'infrastruttura sottostante viene riavviata o aggiornata. Se la tua applicazione riutilizza connessioni di lunga durata, ti consigliamo di configurare l'applicazione per ristabilire le connessioni evitare il riutilizzo di una connessione inattiva.
Eseguire il test di carico della funzione
Per misurare quante connessioni vengono eseguite in media dalla funzione, esegui il deployment come funzione HTTP e utilizza un framework di test delle prestazioni per invocarla a determinati QPS. Una possibile scelta è Artillery, che puoi invocare con una singola riga:
$ artillery quick -d 300 -r 30 URL
Questo comando recupera l'URL specificato a 30 QPS per 300 secondi.
Dopo aver eseguito il test, controlla l'utilizzo della quota di connessione sulla Pagina delle quote dell'API Cloud Run Functions nella console Google Cloud. Se l'utilizzo è costantemente intorno a 30 (o un multiplo), stabilisci una (o più) connessioni in ogni chiamata. Dopo ottimizzare il codice, dovresti notare alcune (10-30) connessioni solo all'inizio del test.
Puoi anche confrontare il costo della CPU prima e dopo l'ottimizzazione nel grafico della quota di CPU nella stessa pagina.