效能

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 版本:v17,而要移除對未使用的 API 的支援 API 版本:v16v15

在專案的 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 v15 and v16 of the Google Ads API.
echo "# Removing support..."
composer run-script remove-google-ads-api-version-support -- 15 16

# 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,且 只剩餘的支援版本為 V17

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

開發與正式環境

PHP 是一種翻譯語言 它會在執行前編譯指令這通常 因為在開發期間,來源經常在執行時有所變化 時間並不重要然而,在正式生產前也是如此 穩定性和效能是主要考量

快取

我們經常建議您利用快取功能來提高效能,因此強烈建議您這麼做。 並藉由儲存預先編譯的指令碼指示,提升穩定性

OPcache 是最常被使用的 解決方案,而且這是系統預設提供的選項

自動儲值

自動載入是很常見的做法 因為這樣可以藉由載入檔案來提高效能 預先編譯類別的資訊

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

記錄

將記錄器設為高度層級 (例如 ERROR) 有助於縮短執行時間 減少這類軟體耗用的資源

詳情請參閱 Logging 指南

偵錯與剖析

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

預先載入

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

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