Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Drupal & PHP performances

Drupal & PHP performances

Presentation content from my talk to the DrupalCon in Pragues 2022

Perussel Nicolas

September 21, 2022
Tweet

More Decks by Perussel Nicolas

Other Decks in Programming

Transcript

  1. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Hi!

    I’m Nicolas Perussel, I’m working at ekino As PHP Lead Senior @mamoot64 mamoot I come from world
  2. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel This

    talk is not about Drupal performance ARCHITECTURE WEBPERF BE A BETTER PHP CODER 1. 2. 3.
  3. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Basics

    Drupal performance Common advices Drupal performance / Common Use internal cache • Use PHP 8 or newer version ASAP • Avoid to install or use lot of modules (impact on discovery, code parsing …), be defensive • Use code linter to fix FQFN (function opcode inlining - Ex: \strlen()) • Use the DIC like a Symfonist (tag, compiler pass and other fun things), the ServicesProviderBase is made for this • Avoid to load unnecessary entities • Avoid to use views for any needs (SQL RAW Query + cache are often better) • Disable Watchdog and prefer the usage of Monolog • Log Slow SQL query + EXPLAIN • … • Cache ALL things as soon as possible! (render, block, views, entities …) • Configure your cache backend (next Chapter)
  4. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Basics

    Drupal hosting infrastructure Drupal performance / Common https://aws.amazon.com/fr/efs/resources/aws-refarch-drupal/ Important things to know Drupal needs : • Webserver (Apache or Nginx) • PHP (PHP-FPM) • Database (MySQL or PostgreSQL) • NFS (mount point or S3) But in fact, for production use: • Cache reverse proxy (Nginx / Varnish) • CDN (Cloudfront / Cloudflare / Fastly / Akamaï …) • Memory cache database (Memcached or Redis)
  5. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Latency

    Numbers Than Every Programmer Should now! Drupal performance / Common
  6. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel What

    are the cache backend? The cache storage Cache storage is separated into "bins", each containing various cache items. Each bin can be configured separately. A cache backend is like a « handler » to interact with bins. A cache backend class is an implementation of the CacheBackendInterface. Bins are collected by the Compiler Pass ListCacheBinsPass. Drupal cache backend / Cache backend definition Main bins are the followed: • Bootstrap : request needs • Render: avoid to recompute the rendering pipeline • Discovery: tells to Drupal to find things • Default: for the common
  7. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Different

    cache backend are available The database backend The default cache backend. Everything is stored in the database. « … as the number of cache and session objects grows, frequent actions such as cache invalidation put a high demand on the database layer. This can cause increased network traffic and ultimately increase the load on the database servers.» Unlike a Symfony app, the database is the heart of a Drupal application. You need to preserve it to keep good performance. Drupal cache backend / Cache backend available The null cache backend The memory cache backend The PHP cache backend The fastest cache backend. Thank’s to the PHP Opcode ;) Stores cache items in a PHP file using a storage that implements Drupal\Component\PhpStorage\PhpStorageInterface
  8. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Different

    cache backend are available Drupal cache backend / Cache backend available The fast chained backend ITEM FOUND GET ITEMS ITEMS NOT FOUND ITEMS ARE RETURNED FETCH OTHER BACKEND APCU STORE IN APCU The Fast Chained backend flow With an APC and backend chain, the cache will check APC first and return immediately if an item is found. If not, it will check the configured backend and then write back to APC if the item is found there; and on cache misses, it will write to both.
  9. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Redis

    cache backend by default Drupal cache backend / Redis the Hero https://pantheon.io/blog/why-we-recommend-redis-drupal-or-wordpress-caching-backend
  10. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Go

    further with IgBinary Drupal cache backend / IgBinary What’s IgBinary ? Igbinary is a drop in replacement for the standard php serializer. Igbinary stores php data structures in a compact binary form. Memory savings are significant when using memcached, APCu. The typical reduction in storage requirements are around 50%. https://github.com/igbinary/igbinary Easy Install with Docker Support PHP v7.4 & v8.0 https://pecl.php.net/package/igbinary
  11. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Go

    further with IgBinary (the proof) Drupal cache backend / IgBinary https://github.com/igbinary/igbinary/blob/master/benchmark/comparisons.php
  12. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Enable

    IgBinary with Redis Drupal cache backend / Redis the Hero
  13. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Now,

    you can play with Drupal cache backend / Adjust it Find the best combination for your app
  14. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel What

    is Opcache? Opcache / Overview How to monitor Opcache? From PHP function : opcache-get-status https://www.php.net/manual/en/function.opcache-get-status.php From this phar : https://github.com/gordalina/cachetool OPcache an extension (include from PHP v5.5) which improves PHP performance by storing precompiled script opcode in shared memory, thereby removing the need for PHP to load and parse scripts on each request. Definition https://wp-rocket.me/blog/best-php-accelerators/
  15. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel OpCache

    config adjustment The Opcache config - Enable Opcache - Increase memory to used - Increase the amount of files that can be cached - Don't Check PHP Files Timestamps - Don’t Check the file size Opcache / Config opcache.file_update_protection = 0 (default "2") Prevents caching files that are less than this number of seconds old. It protects from caching of incompletely updated files. In case all file updates on your site are atomic, you may increase performance setting it to "0".
  16. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel PHP

    Settings / Autoloader PHP / Overview You can use IgBinary with APCu : apc.serializer=igbinary Dump the composer autoloader like this : composer dump-autoload --no-dev --classmap-authoritative You can set the composer autoloader into APCU… Ex: "apcu-autoloader": true Tips « So, whenever you'll use a path to access a file (in the Unix meaning), you or your library or at least your Kernel will have to resolve it. Calling stat() is heavy, first because this is a system call, needing a Kernel trap and a context switch, and also because it most likely asks the disk about metadata. » Julien Pauly - http://blog.jpauli.tech/2014-06-30-realpath-cache-html Increase realpath cache settings https://getcomposer.org/doc/articles/autoloader-optimization.md
  17. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel PHP-FPM

    PHP / Overview Update your PHP-FPM pool configuration PHP-FPM can use one of three process management types: Static, dynamic & ondemand • Ondemand has PHP-FPM fork processes when requests are received. • Dynamic manages the number of available child processes and ensures that at least one child process is always available • Static ensures a fixed number of child processes are always available to handle user requests Always use an Unix Socket instead of TCP Socket => better performance Low Traffic site => use onDemand worker High Traffic site => use static worker Useful links : https://tideways.com/profiler/blog/an-introduction-to-php-fpm-tuning https://docs.platform.sh/languages/php/fpm.html Use hey (https://github.com/rakyll/hey) or K6 (https://k6.io/) to emulate your traffic Þ Stress test Þ Load testing Enable PHP-FPM status and track children
  18. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel PHP-FPM

    PHP / PHP-FPM PHP-FPM load balancing You can create as many PHP-FPM pool as you want (backend / frontend / api / whatever … => use Nginx to proxy pass to one or other (by path by exemple) => each pool can have his own configuration => better isolation
  19. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Use

    Fastgi cache Nginx / FastCGI cache Only the first request can be send to to upstream, others wait the response => fastcgi_cache_lock on; If your upstream is suffuring, use stale cached instead of ! => proxy_cache_use_stale error timeout http_500; Avoid to call PHP for nothing http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html Proxy cache http://nginx.org/en/docs/http/ngx_http_proxy_module.html
  20. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Some

    easy config to set PHP / Config Really easy for HTTP2 ! => listen 80 ssl http2; More tricky for QUIC (HTTP3), still experimental (https://quic.nginx.org/readme.html) Enable HTTP2 or HTTP3 (for fun) brotli on; https://github.com/google/ngx_brotli Enable GZIP or Brotli (better) compression Cache assets
  21. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel What

    is code profiling? Drupal cache backend / Cache backend available Profiling Profiling is collecting deep performance metrics while the code is running, and have full details and context on the code’s behavior to find the root cause of performance bottlenecks. WALL TIME CPU TIME MEMORY HTTP CALLS SQL QUERIES Þ Detect code bottleneck Þ Analyse the critical path Þ Analyse Network (TCP calls) Þ Global, analyse how your code is running Support PHP v7+ & v8+ https://pecl.php.net/package/xhprof https://github.com/longxinH/xhprof
  22. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Test

    your perf with Blackfire assertion Drupal cache backend / Cache backend available Measure performance during build and run phases Over time, performance of most web applications inexorably decline: the size of the response, the time it takes to render the response content, the number of assets, and more. Performance need to be monitor Don’t hesitate to block the CI if performance isn’t suitable. You can ensure your frontend perf with Dareboost / CircleCI …
  23. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel My

    last advices Searching for performance resolution can be expensive in term of cost & time Drupal performance / Common Search for performance is not for all projects typologies For me, no needs to focus on micro-optimisations It’s collaborative work (PHP team / DevOps team / System Team / Frontend team) Do not focus only on PHP, but on its ecosystem Be not only a site builder, be a “deep dive” PHP developer Sorry, but Drupal is not always the good solution
  24. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel Join

    us for contribution opportunities 20-23 September, 2022 Room C2 + C3 Mentored Contribution First Time Contributor Workshop General Contribution #DrupalContributions 20 - 22 September: 9:00 - 18:00 Room C3 23 September: 9:00 - 18:00 Room C2 + C3 20 September: 17:15 - 18:00 Room D9 21 September: 10:30 - 11:15 Room D9 23 September: 09:00 - 12:30 Room C2 23 September: 09:00 - 18:00 Room C2 + C3
  25. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel What

    did you think? Please fill in this session survey directly from the Mobile App.
  26. DrupalCon Prague 2022 | Drupal performances | Nicolas Perussel We

    appreciate your feedback! Please take a moment to fill out: the general conference survey Flash the QR code OR It will be sent by email the Individual session surveys (located under each session description) 1 2