PHP 客户端库可简化与 Google Ads API 的交互,您只需进行最少的配置。不过,性能在很大程度上取决于库的使用和集成方式。
大多数一般最佳实践都适用于所有语言。 本指南介绍了针对 PHP 的性能优化。
Protobuf 和 gRPC 实现
Protobuf 由 gRPC 和 Google Ads API 用于
请求和响应消息。有两种实现方式(纯 PHP 和 C 扩展程序),并且 protobuf 和 grpc 的 C 扩展程序可提供明显更好的序列化和网络性能。
如需了解详情,请参阅 Protobuf 指南和 传输 指南。
PHP 解释器的运行模式
PHP 是一种多功能的 脚本语言,根据使用情况有多种 运行 模式。与标准 CGI 相比,PHP-FPM (FastCGI Process Manager) 或持久应用服务器具有显著优势,因为持久工作器进程可以跨请求重复使用缓存的字节码(通过 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 客户端库不提供通用预加载脚本,因为内存使用量与性能提升之间的权衡取决于每个项目和部署环境。