效能

PHP 用戶端程式庫可讓您輕鬆與 Google Ads API 互動,只需進行最少的設定即可。不過,效能很大程度上取決於程式庫的使用和整合方式。

這些最佳做法大多適用於您 。本指南會逐一介紹 PHP 專屬的功能。

Protobuf 實作

gRPC 會使用 Protobuf 和 Google Ads API,以用於要求和回應訊息。有兩種實作項目可供使用,但以 C 編寫的實作項目效能較佳。

詳情請參閱 Protobuf 指南

PHP 解譯器的作業模式

PHP 是一種多功能的指令碼 語言,過程中有許多作業 模式 (視使用情形而定)。菲律賓披索 CGI (共同閘道介面) 具有明顯的優勢,因為能與 與執行作業之間的資源配置

PHP 版本

建議您定期升級至較新的 PHP 版本 也能提升整體成效支援的 PHP 版本清單

未使用的 Google Ads API 版本

所有版本的用戶端程式庫都支援多個 Google Ads API 版本。 用戶端程式庫支援的每個 Google Ads API 版本, 專屬套件

您可以安全地從用戶端程式庫中移除專屬於未使用的 Google Ads API 版本的套件。因為這對於加快 用戶端程式庫能讓您 以程式輔助的方式完成

範例

假設您正在實作只使用最新 API 版本 (v18) 的用戶端程式庫,且想要移除對未使用的 API 版本 (v17v16) 的支援。

在專案的 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 Google Ads API versions.
echo "# Supported Google Ads API versions:"
find ./vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V* -maxdepth 0 | grep -o '..$'
echo "# Vendor folder size:"
du -sh ./vendor

# Use the Composer script to remove the unused versions v16 and v17 of the Google Ads API.
echo "# Removing support..."
composer run-script remove-google-ads-api-version-support -- 16 17

# Output the vendor folder size and the list of Google Ads API versions that are
# supported after removing support for Google Ads API versions.
echo "# Supported Google Ads API versions:"
find ./vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V* -maxdepth 0 | grep -o '..$'
echo "# Vendor folder size:"
du -sh ./vendor

下方的執行輸出範例顯示檔案大小減少 50M,且 只剩餘的支援版本為 V18

# Supported Google Ads API versions:
V16
V17
V18
# Vendor folder size:
110M    ./vendor
# Removing support...
> Google\Ads\GoogleAds\Util\ApiVersionSupport::remove
Removing support for the version 16 of Google Ads API...
Done
Removing support for the version 17 of Google Ads API...
Done
# Supported Google Ads API versions:
V18
# Vendor folder size:
60M     ./vendor

開發環境與正式環境

PHP 是一種翻譯語言 它會在執行前編譯指令這通常是有利的做法,因為在開發期間,來源經常會變更,而執行時間並非那麼重要。不過,在實際運作期間,穩定性和效能才是主要考量。

快取

快取是常見的做法,也是我們強烈建議的做法,因為它可儲存預先編譯的腳本指令,進而提高效能和穩定性。

OPcache 是最常用的解決方案,並預設可供使用。

自動儲值

Autoload 很常見,因為它會載入類別的預編譯資訊,進而提升效能和穩定性。

PHP 用戶端程式庫符合 PSR-4, 並在 composer.json 檔案。Composer 的專屬選項,例如 --optimize-autoloader--classmap-authoritative 則是指 立即使用。

記錄

將記錄器設為高度層級 (例如 ERROR) 有助於縮短執行時間 減少不必要的負擔和記憶體用量

詳情請參閱 Logging 指南

偵錯與分析

建議您停用常見的偵錯工具和分析器工具 會增加一些執行時間的負擔

預先載入

自 PHP 7.4 起,OPcache 預先載入中 可讓您預先載入記憶體中的指令碼,比一般程序更深入 快取功能。

指令碼必須設計成可運用這項功能,但 PHP 沒有通用的 OPcache 實作方式,因此無法新增用戶端程式庫 而記憶體用量與效能提升之間的取捨 與特定專案和執行狀況非常明確相關