PHP 用戶端程式庫可簡化與 Google Ads API 的互動,您只需進行最少的設定。不過,效能很大程度取決於程式庫的使用和整合方式。
大多數一般最佳做法都適用於所有語言。 本指南將介紹 PHP 專屬的效能最佳化做法。
Protobuf 和 gRPC 實作項目
Protobuf 是 gRPC 和 Google Ads API 用於要求和回應訊息的格式。有兩種實作方式 (純 PHP 和 C 擴充功能),而 protobuf 和 grpc 的 C 擴充功能可大幅提升序列化和網路效能。
詳情請參閱 Protobuf 指南和傳輸指南。
PHP 解譯器的運作模式
PHP 是一種多功能的指令碼語言,並提供多種運作模式,可因應不同用途。PHP-FPM (FastCGI 程序管理工具) 或持續性應用程式伺服器比標準 CGI 具有顯著優勢,因為持續性工作程序可以跨要求重複使用快取位元碼 (透過 OPcache) 和資源。
PHP 版本
請定期升級至較新的 PHP 版本,以提升執行階段效能。請參閱支援的 PHP 版本清單。
未使用的 Google Ads API 版本
所有版本的用戶端程式庫都支援多個 Google Ads API 版本。用戶端程式庫支援的每個 Google Ads API 版本,都有專屬的套件。
您可以從用戶端程式庫中安全地移除專為 Google Ads API 版本設計的套件,這些版本並未由您的應用程式使用。為減少記憶體用量並加快執行速度,用戶端程式庫提供實用工具,可透過程式輔助方式移除未使用的 API 版本。
範例
假設您要導入僅使用最新 API 版本 (v25) 的用戶端應用程式,並想移除對未使用 API 版本 (v22、v23 和 v24) 的支援。
在專案的 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
# v22, v23, and
# v24 of the Google Ads API.
echo "# Removing support..."
composer run-script remove-google-ads-api-version-support -- \
22 \
23 \
24
# 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
下列範例執行輸出內容顯示檔案大小縮減情形,其中 V25 是唯一支援的版本:
# Supported Google Ads API versions:
V22
V23
V24
V25
# Vendor folder size:
135M ./vendor
# Removing support...
> Google\Ads\GoogleAds\Util\ApiVersionSupport::remove
Removing support for the version 22 of Google Ads API...
Done
Removing support for the version 23 of Google Ads API...
Done
Removing support for the version 24 of Google Ads API...
Done
# Supported Google Ads API versions:
V25
# 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 用戶端程式庫不會提供一般預先載入指令碼,因為記憶體用量和效能提升之間的取捨,會因每個專案和部署環境而異。