PHP 用戶端程式庫可簡化與 Google Ads API 的互動,您只需進行最少設定。不過,效能很大程度取決於程式庫的使用和整合方式。
大多數一般最佳做法都適用於所有語言。 本指南將介紹 PHP 專屬的效能最佳化做法。
Protobuf 和 gRPC 實作
gRPC 和 Google Ads API 會使用 Protobuf 傳送要求和回應訊息。有兩種實作方式 (純 PHP 和 C 擴充功能),而 protobuf 和 grpc 的 C 擴充功能可大幅提升序列化和網路效能。
詳情請參閱 Protobuf 指南和傳輸指南。
PHP 直譯器的作業模式
PHP 是一種多用途指令碼語言,並根據用途提供多種作業模式。PHP-FPM (FastCGI Process Manager) 或永久應用程式伺服器比標準 CGI 更有優勢,因為永久工作站程序可以跨要求重複使用快取的位元碼 (透過 OPcache) 和資源。
PHP 版本
定期升級至較新的 PHP 版本,可提升執行階段效能。請參閱支援的 PHP 版本清單。
未使用的 Google Ads API 版本
所有版本的用戶端程式庫都支援多個 Google Ads API 版本。用戶端程式庫支援的每個 Google Ads API 版本,都有專屬的套件。
您可以從用戶端程式庫中安全地移除專為 Google Ads API 版本設計的套件,這些版本並未由您的應用程式使用。為減少記憶體用量並加快執行速度,用戶端程式庫提供一項公用程式,可透過程式輔助方式移除未使用的 API 版本。
範例
假設您要導入的用戶端應用程式只使用最新 API 版本 (v26),並想移除對未使用 API 版本 (v23、v24 和 v25) 的支援。
在專案的 composer.json 檔案中,定義 Composer 指令碼 (名為 remove-google-ads-api-version-support),該指令碼會運用 ApiVersionSupport 中用戶端程式庫提供的公用程式:
"scripts": {
"remove-google-ads-api-version-support": [
"Google\\Ads\\GoogleAds\\Util\\ApiVersionSupport::remove"
]
}
接著,執行 Composer 指令碼,並以版本號碼做為參數,然後輸出狀態訊息:
# Change the current directory to the project directory.
cd /path/to/the/project
# Install the project.
composer install
# Output the vendor folder size and the list of Google Ads API versions that are
# supported before removing support for unused Google Ads API versions.
echo "# Supported Google Ads API versions:"
find ./vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V* \
-maxdepth 0 | grep -oE 'V[0-9]+$'
echo "# Vendor folder size:"
du -sh ./vendor
# Use the Composer script to remove the unused versions
# v23, v24, and
# v25 of the Google Ads API.
echo "# Removing support..."
composer run-script remove-google-ads-api-version-support -- \
23 \
24 \
25
# Output the vendor folder size and the list of Google Ads API versions that are
# supported after removing support for unused Google Ads API versions.
echo "# Supported Google Ads API versions:"
find ./vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V* \
-maxdepth 0 | grep -oE 'V[0-9]+$'
echo "# Vendor folder size:"
du -sh ./vendor
以下範例執行輸出內容顯示檔案大小縮減情形,其中 V26 是唯一支援的版本:
# Supported Google Ads API versions:
V23
V24
V25
V26
# Vendor folder size:
135M ./vendor
# Removing support...
> Google\Ads\GoogleAds\Util\ApiVersionSupport::remove
Removing support for the version 23 of Google Ads API...
Done
Removing support for the version 24 of Google Ads API...
Done
Removing support for the version 25 of Google Ads API...
Done
# Supported Google Ads API versions:
V26
# Vendor folder size:
60M ./vendor
開發與正式環境
PHP 是一種解譯語言,會在執行前編譯指令。在開發期間,來源檔案經常變更,且執行時間較不重要,因此這種做法很有幫助。在正式環境中,穩定性和效能是首要考量。
快取
建議您啟用快取功能,將預先編譯的指令碼位元碼儲存在共用記憶體中,藉此提升效能和穩定性。
OPcache 是標準解決方案,預設與 PHP 搭配。
自動儲值
自動載入功能會載入預先編譯的類別對應資訊,提升效能和穩定性。
PHP 用戶端程式庫符合 PSR-4 自動載入標準,並在 composer.json 檔案中提供定義。專用的 Composer CLI 選項 (例如 --optimize-autoloader 或 --classmap-authoritative) 隨即可在正式版建構作業中使用。
記錄
在正式環境中,將記錄器設為較高的門檻 (例如 ERROR 或 WARNING),有助於減少 I/O 負擔和記憶體消耗。
詳情請參閱記錄指南。
偵錯與分析
在正式環境中停用偵錯工具和分析器擴充功能 (例如 Xdebug),因為這些功能會大幅增加執行負擔。
預先載入
自 PHP 7.4 起,您可以使用 OPcache 預先載入功能,在伺服器啟動時將指令碼預先載入記憶體,比標準 OPcache 快取更進一步。
預先載入指令碼必須專為應用程式設計。PHP 用戶端程式庫不會提供一般預先載入指令碼,因為記憶體用量和效能提升之間的取捨,會因每個專案和部署環境而異。