性能

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

大多数最佳实践适用于所有语言。本指南将介绍专门针对 PHP 的错误。

Protobuf 实现

ProtobufgRPC 使用 以及用于请求和响应消息的 Google Ads API。两种实现方法 可用,尽管使用 C 语言编写的代码效果更好。

如需了解详情,请参阅 Protobuf 指南

PHP 解释器的操作模式

PHP 是一种用途多样的脚本 language,并且有许多操作 模式。菲律宾比索 CGI(通用网关接口)的优势在于它可以与 资源。

PHP 版本

最好定期升级到较新的 PHP 版本,因为较新的版本通常具有更好的整体性能。支持的 PHP 版本列表

不再使用的 Google Ads API 版本

客户端库的所有版本都支持多个 Google Ads API 版本。对于客户端库支持的各个 Google Ads API 版本,有 专用软件包

您可以放心地从客户端库中移除专门用于未使用的 Google Ads API 版本的软件包。因为它有助于加快 或减少内存占用,那么客户端库会提供相应的实用程序, 以编程方式创建

示例

假设您要实现的客户端库仅使用最新的 API 版本:v18,并且您想取消对未使用的 API 版本的支持 API 版本:v17v16

在项目的 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 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 是最常用的解决方案,默认情况下可用。

自动充值

自动加载很常见,因为它可以加载有关类的预编译信息,从而提高性能并增强稳定性。

PHP 客户端库符合 PSR-4 自动加载要求,并在 composer.json 文件中提供定义。然后,您就可以直接使用 Composer 的专用选项,例如 --optimize-autoloader--classmap-authoritative

日志记录

将记录器设置为 ERROR 等高级别有助于减少执行时间开销和内存用量。

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

调试和性能分析

我们建议停用调试程序和性能分析器工具,因为它们通常自带 有一些执行时间开销。

预加载

从 PHP 7.4 开始,OPcache 预加载 可用于预加载内存中的脚本,比常规 缓存。

脚本必须设计成使用该功能,但 PHP 代码 客户端库则不支持,因为不存在实现 OPcache 的通用方法 内存使用和性能增益之间的权衡是 与给定项目和执行密切相关。