性能

PHP 客户端库可简化与 Google Ads API 的交互,您只需进行极少的配置。不过,性能在很大程度上取决于库的使用和集成方式。

大多数一般最佳实践适用于所有语言。本指南介绍了 PHP 特有的性能优化。

Protobuf 和 gRPC 实现

gRPC 和 Google Ads API 使用 Protobuf 来处理请求和响应消息。目前有两种实现(纯 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 版本 (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 文件中提供了相关定义。然后,可以在生产 build 中直接使用专用的 Composer CLI 选项,例如 --optimize-autoloader 或 --classmap-authoritative。

日志记录

在生产环境中将日志记录器设置为更高的阈值(例如 ERROR 或 WARNING)有助于减少 I/O 开销和内存消耗。

如需了解详情,请参阅日志记录指南。

调试和性能分析

在生产环境中停用调试程序和性能分析器扩展程序(例如 Xdebug),因为它们会产生大量执行开销。

预加载

自 PHP 7.4 起,OPcache 预加载可用于在服务器启动时将脚本预加载到内存中,比标准 OPcache 缓存更进一步。

预加载脚本必须专为您的应用而设计。PHP 客户端库不提供通用预加载脚本,因为内存用量与性能提升之间的权衡取决于每个项目和部署环境。