Benchmark Profile and Boost your Symfony application

8,051 views

Published on

@symfonyday portugal

Published in: Technology
1 Comment
17 Likes
Statistics
Notes
No Downloads
Views
Total views
8,051
On SlideShare
0
From Embeds
0
Number of Embeds
54
Actions
Shares
0
Downloads
39
Comments
1
Likes
17
Embeds 0
No embeds

No notes for slide

Benchmark Profile and Boost your Symfony application

  1. 1. RATIONALLYBOOST SYMFONY2APPLICATION 2014 VERSION
  2. 2. welcometothe bundle.com@liuggiowelcometothebundle.com
  3. 3. PERFORMANCE PROBLEM?
  4. 4. PERFORMANCE PROBLEM?TODAY SPEED IS A FEATURE
  5. 5. BUSINESSNEEDSSPEED? (speed in terms of adding a new feature, bug hunting ...)
  6. 6. load balancer reverse proxy CDN web server MQ DB cache session storage DB internal storage BROWSER private cache shared cache DNS ISP your company
  7. 7. GUESS PRIDE PREJUDICE AND GUESSING
  8. 8. THEENDLESSCYCLE make change benchmark profile
  9. 9. benchmarking ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchm the process of comparing … sterilized environment - disable profiler 1
  10. 10. ab $ apt-get install apache-utils $ ab -e output.csv -c 10 -t 10 http://book.local $ ab -p post.txt -T application/x-www-form-urlencoded -e output.csv -kc 10 -t 10 http://book.local ● http_load simple ● siege complete ● jmeter complex ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchm
  11. 11. ProfilingXHProf demo ...2 ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchm make change benchmark profile
  12. 12. ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchm
  13. 13. ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchm
  14. 14. ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchm
  15. 15. ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchm
  16. 16. ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchm
  17. 17. Make Changes ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchm 3 make change benchmark profile
  18. 18. 404 silver bullet Not Found ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  19. 19. ● Think as readonly filesystem ● Don’t let the user wait ● Caches are your friends ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma ● Try to quit from the optimization-cycle
  20. 20. PHPAPC ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma ● apc.stat=0 (need maintenance) ● fragmentation (symfony is not slow! opcode, php 5.4, apc)
  21. 21. ● Zend Opcode ● ACPu (emulate all the apc_* calls) ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma PHP5.5
  22. 22. HHVM ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma (hhvm - HPHPc - Performance on real application)
  23. 23. HHVM = PHP++ ? ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma (HHVM is a great opportunity for Symfony and for object oriented lovers)
  24. 24. symfonyecosystem ● $ composer dump-autoload --optimize ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma ● twig extension http://twig.sensiolabs.org/doc/installation.html
  25. 25. symfonyecosystem ● [ASSETIC] use assetic and CDN, compress and minify files ● [SWIFT MAILER] email in the spool like Redis/Gearman or MQ (remember to flush) ● [PROXY] no logic in the constructor! <service id="foo" class="AcmeFoo" lazy="true" /> ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  26. 26. [MONOLOG] don’t log to file in production debugging: Sentry or Graylog ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  27. 27. yourcachewarmup ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  28. 28. liip/LiipDoctrineCacheBundle Doctrine/Common/Cache Without bundle services.yml in your application Use a bundle ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  29. 29. doctrineORM ● Database Abstraction Layer (DBAL) ● Object Relational Mapper (ORM) ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  30. 30. ORMcaches metadata, query, result ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change
  31. 31. understandUOW Unit Of Work ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  32. 32. UOWeffect TLDR; keeps track changes of objects and coordinates the writing ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems
  33. 33. UnitofWork Managed ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  34. 34. UnitofWork Managed ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  35. 35. UOWeffect flush is expensive* while { …. $this->em->persist($v); $this->em->flush() } ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma * profile your domain first.
  36. 36. Reducing the flush cost* — Number of managed Entities — Type of entities (Read-Only) $em->flush($entity); /** * @Entity * @READONLY*/ Class Status { $em->getUnitOfWork() ->markReadOnly($entity); ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma * if you really need
  37. 37. Reducing the flush cost* /** @Entity * @ChangeTrackingPolicy("DEFERRED_EXPLICIT”) tracking policy implicit DB === UOW explicit DB != OUW ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma * if you really need
  38. 38. doctrineORM ● Reference ● Partial object* ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma $q = $em->createQuery("select partial u.{id,name} from DomainUser u"); $comment->setPost($em->getReference(‘AcmeBundleEntityPost’, $postId)); * smelling tip, inconsistent objects
  39. 39. doctrineORM ● Change Hydration * HYDRATE_OBJECT $query->setHydrationMode(DoctrineORMQuery::HYDRATE_ARRAY); HYDRATE_SCALAR HYDRATE_SINGLE_SCALAR HYDRATE_SIMPLEOBJECT ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma * Object Oriented will cry.
  40. 40. theHidden Hydrationcache * ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma * Be careful, only for Read operations
  41. 41. ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma theHydrationcache * With ResultCache With HydrationCache * profile data from my domain not yours.
  42. 42. cacheproblem ● invalidation ● cache miss storm morelesscache ● pre-caching cache-back ● microcaching ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  43. 43. doctrineORMassociation ● Fetching is LAZY by default ● Changing for some queries ● Fetching EXTRA LAZY $query = … $query->setFetchMode(“Order”, “Cart”, “LAZY”); ->contains($entity); ->count(); ->slice($offset, $length); /*** @ManyToOne(targetEntity=”Cart”, cascade={“all”}, fetch=”EAGER”) ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  44. 44. ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma HTTPCache ● http specification ● validation / expiration ● safe methods ● Edge Side Included http://symfony.com/doc/current/book/http_cache.html
  45. 45. measure … and Metrics? ● Behaviour ● Measure applications data ● Don’t use for benchmarking Visual tool to better understand the reality ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma
  46. 46. benchmark->profile->make change metrics produces graphs decisions composer install liuggio/statsd-client-bundle ~1
  47. 47. Optimizing dev tool ->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchma https://www.flickr.com/photos/itomcash/12953162645/sizes/l/ (multiple xdebug configs, db on ram, tricks, shared and private fixtures)
  48. 48. OOD, SOA, DDD, 12factors Architecture
  49. 49. BALANCED OPTIMIZATION vs MAINTAINABILITY BE
  50. 50. References 1. http://labs.qandidate.com/blog/2013/10/21/running-symfony-standard-on-hhvm/ 2. http://www.appdynamics.com/blog/php/php-performance-crash-course-part-1-the-basics/ 3. https://support.cloud.engineyard.com/entries/26902267-PHP-Performance-I-Everything-You-Need- to-Know-About-OpCode-Caches 4. https://speakerdeck.com/dshafik/lonestar-php-fast-not-furious-how-to-identify-and-fix-slow-code 5. http://share.ez.no/learn/ez-publish/ez-publish-performance-optimization-part-1-of-3-introduction- and-benchmarking/%28page%29/5 6. http://www.linuxhelp.in/2012/11/benchmarking-with-http-load.html 7. https://speakerdeck.com/bastianhofmann/profiling-php-applications 8. http://www.symfony.com 9. http://blog.ircmaxell.com/2013/09/rambling-on-internals.html 10. .P of EAA page 184 Patterns of Enterprise Application Architecture by Martin Fowler 11. https://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork.html 12. http://docs.doctrine-project.org/en/latest/reference/change-tracking-policies.htmlFlush optimization 13. http://slides.seld.be/?file=2011-10-20+High+Performance+Websites+with+Symfony2.html#1 Credits: https://www.flickr.com/photos/kelehen/8962203423 https://www.flickr.com/photos/itomcash/12953162645
  51. 51. QUESTIONSARE ALWAYSBETTER THANANSWERS @liuggio https://joind.in/10783

×