性能

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

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

Protobuf 和 gRPC 实现

Protobuf 由 gRPC 和 Google Ads API 用于请求和响应消息。目前有两种实现方式(纯 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 版本 (v25) 的客户端应用,并希望移除对未使用的 API 版本(v22、v23 和 v24)的支持。

在项目的 composer.json 文件中,定义一个利用 ApiVersionSupport 中客户端库提供的实用程序的 Composer 脚本(名为 remove-google-ads-api-version-support):

"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 文件的一部分。然后,您可以在生产 build 中直接使用专用 Composer CLI 选项,例如 --optimize-autoloader 或 --classmap-authoritative。

日志记录

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

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

调试和性能分析

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

预加载

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

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