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

Discovering & solving performance issues in PHP-apps

6a1345d8e6dd15b2c78eff0c331963b1?s=47 Denis Brumann
September 30, 2017

Discovering & solving performance issues in PHP-apps

I want to lay out how to effectively anticipate, identify and mitigate performance issues in a common PHP application. I want to discuss how to approach performance optimizations including micro-optimizations, some tools available to a developer for investigating and monitoring performance issues as well as some ways to improve performance, including a quick introduction to both application and HTTP-caching.

6a1345d8e6dd15b2c78eff0c331963b1?s=128

Denis Brumann

September 30, 2017
Tweet

Transcript

  1. DISCOVERING & SOLVING PERFORMANCE ISSUES

  2. I will give you… a quick tour of some tools

    I use an opinionated guide of practices an overview of typical problems/solutions
  3. DENIS BRUMANN denis.brumann@sensiolabs.de @dbrumann

  4. BEFORE YOU START… FIGURE OUT YOUR PROBLEM

  5. None
  6. It can be used to simulate a heavy load on

    a server, group of servers, network or object to test its strength or to analyze overall performance under different load types. https://jmeter.apache.org/
  7. LOAD TEST
 Verify application behavior under normal and peak load

    conditions. STRESS TEST 
 Determine or validate an application’s behavior when pushed
 beyond normal or peak load conditions. CAPACITY TEST
 Determine how many users and/or transactions a given system
 will support and still meet performance goals.
  8. None
  9. None
  10. None
  11. None
  12. WEB SERVER 
 access & error logs DATABASE 
 size,

    throughput, connections, slow queries CACHE
 hits, misses, size SYSTEM
 CPU & memory usage, disk I/O, network
  13. WHAT TO DO? slowly increase sample size monitor changes &

    spot anomalies make one change at a time
 based on a verifiable assumption repeat
  14. Search for causes not symptoms Do not make changes without

    cause Change as few things as possible Know when to stop
  15. You have rock solid & kickass monitoring
 for your production

    system! Tests & monitoring can be reused & adapted
 for other scenarios
  16. Performance Testing Guidance for Web Applications https://msdn.microsoft.com/en-us/library/bb924356.aspx

  17. WHAT ABOUT THE CLIENT?

  18. PERFORMANCE TAB

  19. NETWORK TAB

  20. None
  21. MEANWHILE ON THE SERVER…

  22. SYMFONY WEB PROFILER

  23. None
  24. PROFILING AS A SERVICE

  25. BLACKFIRE

  26. None
  27. None
  28. BLACKFIRE PLUGIN

  29. BLACKFIRE PLUGIN

  30. $config = new \Blackfire\ClientConfiguration(
 $clientId, $clientToken
 ); $client = new

    \Blackfire\Client($config); $probe = $client->createProbe(); // code to be profiled $client->endProbe($probe); BLACKFIRE SDK
  31. TYPICAL PROBLEM GROUPS I/O WAIT CPU USAGE MEMORY NETWORK LATENCY

    SQL QUERIES
  32. HOW TO DEVELOP FOR PERFORMANCE? DON’T!

  33. DEVELOP FOR READABILITY MAINTAINABILITY SAFETY

  34. WHAT DOES THAT MEAN? SOLID YAGNI DRY

  35. AVOID ABSTRACTIONS & MAGIC
 You are solving concrete business problems

    MICRO-OPTIMIZATIONS
 Saving a few µs in cpu cycles never matters GUESSING
 Only real data from your performance counts
  36. None
  37. PHP 7

  38. http://www.zend.com/en/resources/php7_infographic

  39. PHP INSPECTIONS (EA EXTENDED)

  40. BYTE CODE CACHE APC OpCache WinCache

  41. FOR SYMFONY
 opcache.max_accelerated_files = 20000 realpath_cache_size=4096K realpath_cache_ttl=600 https://symfony.com/doc/current/performance.html

  42. AUTOLOADING Use composer classmap composer dump-autoload --optimize \ --classmap-authoritative --no-dev

    composer install -noa
  43. VARNISH https://varnish-cache.org

  44. Varnish Cache is a web application accelerator also known as

    a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 - 1000x, depending on your architecture. https://varnish-cache.org/intro/index.html#the-basics
  45. PHP NGINX VARNISH NGINX NGINX

  46. <esi:include src=“https:/…” />

  47. CACHEABLE HTTP RESPONSES Cache-Control Expires ETag Last-Modified

  48. UTILITIES SYMFONY Helper methods in Response-object
 Controller-Annotations @Cache PSR-7
 https://github.com/micheh/psr7-cache

  49. CODE SAMPLE PSR-7 Response $response->withHeader( ‘Cache-Control’, ‘public, s-maxage=600, must-revalidate’ );

  50. https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching

  51. APPLICATION CACHING

  52. DON’T BE FOOLED REDIS IS NOT EASY TO SETUP

  53. APCu FILESYSTEM DATABASE

  54. ⚠ USE WITH CPU-HEAVY TASKS NOT MEMORY-HEAVY TASKS

  55. DOCTRINE

  56. http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/ improving-performance.html METADATA & QUERY CACHES
 it is strongly discouraged

    to use Doctrine without a Metadata and Query cache (preferably with APC or Memcache as the cache driver). ALTERNATIVE RESULT FORMATS
 Make effective use of the available alternative query result formats like nested array graphs or pure scalar results, especially in scenarios where data is loaded for read-only purposes.
  57. None
  58. https://github.com/igorw/retry/issues/3

  59. BACK IT UP WITH DATA! MEASURE ANALYZE OPTIMIZE MEASURE AGAIN

  60. THANK YOU!