我使用 F4 機器在 Google App Engine Standard 上部署了一個 Django 應用程式。API 正在做一些機器學習,并且處理在本地的持續時間約為 4 秒(在 3.5 到 4 秒之間)。對于我的用例,部署的應用程式也可以有 4-5 秒的延遲。但是,當我多次測驗部署的應用程式執行相同的請求時,我發現第一個請求需要 3-4 秒,但在 10-15 次迭代后,它們需要大約 8 秒。
這是我用來測驗我的應用程式的代碼:
session = requests.Session()
all_times = []
for i in range(50):
try:
t0 = time.time()
resp = session.post(url_api, headers=headers, json=data)
t1 = time.time()
print(t1 - t0)
all_times.append(t1 - t0)
except Exception as e:
print("Err", e)
請求持續時間的結果如下:

我想知道為什么在 15 次請求之后會有這個差距,以及為什么有些點遠高于平均 7-8 秒(例如 10 秒)。
在查看 Google Cloud Console 中的延遲時,我得到了相同的模式:

我試過的
I tried to change the autoscaling parameters, thinking that it could be because of the creation of instances. But I got the same pattern when I restrict the number of instance to 1 with this in app.yaml:
automatic_scaling:
max_instances: 1
I also tried to:
- Use session in
requeststo avoid creating a new session at each iteration. - Do my requests using curl, which leads to the same results.
My goal is to minimize the variability and keep the request durations below 5s.
Update
Here is an example of the trace list from Cloud Trace, which shows the same pattern:


I compared traces associated with a large latency and traces with a lower latency, but didn't find any important difference.
Update 2
Thanks to the answer of Priyashree, setting the min_idle_instances to 1 to avoid machine restart solved the problem:

uj5u.com熱心網友回復:
在將 Google Cloud Platform 性能與本地性能進行比較時,您應該記住,在 GCP 上部署需要更多時間來匯入所有必要的庫并設定 Django 框架。
一般來說,將本地機器上的性能與 GCE 上的性能進行比較沒有多大意義,因為本地機器可能運行不同的作業系統。但是,是的,我同意 GAE 在 10 到 15 個請求之后提供的請求之間的延遲差異是不可接受的,而且很奇怪。
檢查以下內容:
- 如果實體已超過其配置的 instance_class的最大記憶體,在您的情況下為 F4,這可能會導致您的實體關閉并且 App Engine 在其位置創建一個新實體,因為這可能是由于沒有任何可用的實體,因為那是關于部署實體所需的時間并且您已將 max_instances 設定為 1。
- 如果您設定了任何target_cpu_utilization。它指定當舊實體達到目標 CPU 使用率時新實體將啟動處理流量的 CPU 使用率閾值。
- 正如您在評論中提到的那樣,您發送請求的時間間隔很重要,“您正在逐一提出請求”。當請求量減少時,App Engine 會減少實體數量。當應用程式根本不被使用時,App Engine 會關閉其關聯的動態實體,但會在需要時立即重新加載它們。重新加載實體可能會給用戶帶來額外的延遲。為確保您沒有重新加載實體,請指定空閑實體的最小數量。根據請求量為您的應用程式設定適當數量的空閑實體可以讓您的應用程式以極少的延遲為每個請求提供服務,除非您遇到例外高的請求量。
此外,根據檔案,如果 instance_class 設定為 F2 或更高,您可以通過將max_concurrent_requests設定為高于默認值 10 的值來優化您的實體。要確定最佳值,請逐漸增加它并監控應用程式的性能。
通過PageSpeed Insights 分析網頁的內容,然后生成建議以使該頁面更快并且可能很方便。我還建議您聯系 Google 支持人員進行一對一互動,因為這種情況/問題是特定于環境的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/433873.html
上一篇:ImportError:無法在Pythonfastapi中從“google.cloud”(未知位置)匯入名稱“tasks_v2”
