Page Summary
-
To optimize performance with the PHP client library for the Google Ads API, using the C implementation of Protobuf is recommended.
-
Regularly upgrading to newer PHP versions can lead to better overall performance.
-
Unused Google Ads API versions can be safely removed from the client library using provided utilities to potentially speed up execution and reduce memory footprint.
-
Implementing caching, such as OPcache, and optimizing autoload with Composer can improve performance and stability in production environments.
-
Adjusting logging levels and disabling debugger/profiler tools can help reduce execution time overhead and memory consumption.
The PHP client library eases interactions with the Google Ads API with minimal configuration on your part. However, performance depends significantly on how the library is used and integrated.
Most general best practices are applicable to all languages. This guide covers performance optimizations specific to PHP.
Protobuf and gRPC implementations
Protobuf is used by gRPC and the Google Ads API for
request and response messages. Two implementations are available (pure PHP and a
C extension), and the C extensions for both protobuf and grpc provide
substantially better serialization and network performance.
See the Protobuf guide and the Transport guide for more information.
Operation mode of the PHP interpreter
PHP is a versatile scripting language and has several operation modes depending on usage. PHP-FPM (FastCGI Process Manager) or persistent application servers have a notable advantage over standard CGI because persistent worker processes can reuse cached bytecode (through OPcache) and resources across requests.
PHP version
Regularly upgrade to a newer PHP version to benefit from runtime performance improvements. Check the list of supported PHP versions.
Unused Google Ads API versions
All versions of the client library support multiple Google Ads API versions. For each version of the Google Ads API supported by the client library, there are dedicated packages for that version.
The packages dedicated to versions of the Google Ads API that are not used by your application can be safely removed from the client library. To reduce memory footprint and speed up execution, the client library provides a utility to remove unused API versions programmatically.
Example
Suppose that you're implementing a client application that uses only the latest
API version (v25), and you want to remove support for the
unused API versions (v22,
v23, and v24).
In your project's composer.json file, define a Composer script (named
remove-google-ads-api-version-support) that leverages the utility provided by
the client library in ApiVersionSupport:
"scripts": {
"remove-google-ads-api-version-support": [
"Google\\Ads\\GoogleAds\\Util\\ApiVersionSupport::remove"
]
}
Then, run the Composer script with the version numbers as parameters and print status messages:
# 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
The following sample execution output shows the file size reduction, with
V25 as the only remaining supported version:
# 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
Development versus production
PHP is an interpreted language that compiles instructions before executing them. This is advantageous during development, when source files frequently change and execution time is less critical. In production environments, stability and performance become the primary concerns.
Cache
Caching is recommended because it improves performance and increases stability by storing precompiled script bytecode in shared memory.
OPcache is the standard solution and is bundled with PHP by default.
Autoload
Autoloading improves performance and stability by loading precompiled class map information.
The PHP client library conforms to PSR-4 for autoloading and provides
the definition as part of its composer.json file. Dedicated
Composer CLI options, such as --optimize-autoloader or
--classmap-authoritative, can then be used out of the box in production
builds.
Logging
Setting loggers to a higher threshold such as ERROR or WARNING in production
helps reduce I/O overhead and memory consumption.
See the Logging guide for more information.
Debugging and profiling
Disable debugger and profiler extensions (such as Xdebug) in production because they introduce substantial execution overhead.
Preload
Since PHP 7.4, OPcache preloading can be used to preload scripts into memory at server startup, going one step further than standard OPcache caching.
A preload script must be designed specifically for your application. The PHP client library does not provide a generic preload script because the trade-off between memory usage and performance gain is specific to each project and deployment environment.