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

Discovering & solving performance issues in PHP-apps

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.

Denis Brumann

September 30, 2017
Tweet

More Decks by Denis Brumann

Other Decks in Programming

Transcript

  1. DISCOVERING & SOLVING
    PERFORMANCE ISSUES

    View Slide

  2. I will give you…
    a quick tour of some tools I use
    an opinionated guide of practices
    an overview of typical problems/solutions

    View Slide

  3. DENIS
    BRUMANN
    [email protected]
    @dbrumann

    View Slide

  4. BEFORE YOU START…
    FIGURE OUT YOUR PROBLEM

    View Slide

  5. View Slide

  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/

    View Slide

  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.

    View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. WEB SERVER 

    access & error logs
    DATABASE 

    size, throughput, connections, slow queries
    CACHE

    hits, misses, size
    SYSTEM

    CPU & memory usage, disk I/O, network

    View Slide

  13. WHAT TO DO?
    slowly increase sample size
    monitor changes & spot anomalies
    make one change at a time

    based on a verifiable assumption
    repeat

    View Slide

  14. Search for causes not symptoms
    Do not make changes without cause
    Change as few things as possible
    Know when to stop

    View Slide

  15. You have rock solid & kickass monitoring

    for your production system!
    Tests & monitoring can be reused & adapted

    for other scenarios

    View Slide

  16. Performance Testing Guidance
    for Web Applications
    https://msdn.microsoft.com/en-us/library/bb924356.aspx

    View Slide

  17. WHAT ABOUT THE CLIENT?

    View Slide

  18. PERFORMANCE TAB

    View Slide

  19. NETWORK TAB

    View Slide

  20. View Slide

  21. MEANWHILE ON THE SERVER…

    View Slide

  22. SYMFONY WEB PROFILER

    View Slide

  23. View Slide

  24. PROFILING AS A SERVICE

    View Slide

  25. BLACKFIRE

    View Slide

  26. View Slide

  27. View Slide

  28. BLACKFIRE PLUGIN

    View Slide

  29. BLACKFIRE PLUGIN

    View Slide

  30. $config = new \Blackfire\ClientConfiguration(

    $clientId, $clientToken

    );
    $client = new \Blackfire\Client($config);
    $probe = $client->createProbe();
    // code to be profiled
    $client->endProbe($probe);
    BLACKFIRE SDK

    View Slide

  31. TYPICAL PROBLEM GROUPS
    I/O WAIT
    CPU USAGE
    MEMORY
    NETWORK LATENCY
    SQL QUERIES

    View Slide

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

    View Slide

  33. DEVELOP FOR
    READABILITY
    MAINTAINABILITY
    SAFETY

    View Slide

  34. WHAT DOES THAT MEAN?
    SOLID
    YAGNI
    DRY

    View Slide

  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

    View Slide

  36. View Slide

  37. PHP 7

    View Slide

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

    View Slide

  39. PHP INSPECTIONS (EA EXTENDED)

    View Slide

  40. BYTE CODE CACHE
    APC
    OpCache
    WinCache

    View Slide

  41. FOR SYMFONY

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

    View Slide

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

    View Slide

  43. VARNISH
    https://varnish-cache.org

    View Slide

  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

    View Slide

  45. PHP
    NGINX
    VARNISH
    NGINX
    NGINX

    View Slide


  46. View Slide

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

    View Slide

  48. UTILITIES
    SYMFONY
    Helper methods in Response-object

    Controller-Annotations @Cache
    PSR-7

    https://github.com/micheh/psr7-cache

    View Slide

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

    View Slide

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

    View Slide

  51. APPLICATION CACHING

    View Slide

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

    View Slide

  53. APCu
    FILESYSTEM
    DATABASE

    View Slide


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

    View Slide

  55. DOCTRINE

    View Slide

  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.

    View Slide

  57. View Slide

  58. https://github.com/igorw/retry/issues/3

    View Slide

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

    View Slide

  60. THANK YOU!

    View Slide