Upload
giulio-de-donato
View
5.995
Download
3
Tags:
Embed Size (px)
DESCRIPTION
@symfonyday portugal
Citation preview
RATIONALLYBOOSTSYMFONY2APPLICATION
2014 VERSION
welcometothebundle.com@liuggio welcometothebundle.com
PERFORMANCE PROBLEM?
PERFORMANCE PROBLEM?TODAY SPEED IS A FEATURE
BUSINESSNEEDSSPEED?
(speed in terms of adding a new feature, bug hunting ...)
loadbalancer
reverseproxy
CDN
web server
MQ
DBcache
sessionstorage
DB
internalstorage
BROWSER
privatecache
shared cache
DNS
ISP
your company
GUESS
PRIDE PREJUDICE AND GUESSING
THEENDLESSCYCLE
make change
benchmark
profile
benchmarking
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
the process of comparing …
sterilized environment - disable profiler
1
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->benchmark->
ProfilingXHProf demo ...2
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
make change
benchmark
profile
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
Make Changes
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
3
make change
benchmark
profile
404 silver bulletNot Found
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
● 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->benchmark->
● Try to quit from the optimization-cycle
PHPAPC
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
● apc.stat=0 (need maintenance)● fragmentation
(symfony is not slow! opcode, php 5.4, apc)
● 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->benchmark->
PHP5.5
HHVM->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
(hhvm - HPHPc - Performance on real application)
HHVM = PHP++ ?->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
(HHVM is a great opportunity for Symfony and for object oriented lovers)
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->benchmark->
● twig extension http://twig.sensiolabs.org/doc/installation.html
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="Acme\Foo" lazy="true" />
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
[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->benchmark->
yourcachewarmup->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
liip/LiipDoctrineCacheBundle
Doctrine/Common/CacheWithout 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->benchmark->
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->benchmark->
ORMcachesmetadata, query, result
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change
understandUOWUnit Of Work
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
UOWeffect
TLDR;
keeps track changes of objectsand coordinates the writing
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the
resolution of concurrency problems
UnitofWork
Managed
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
UnitofWork
Managed
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
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->benchmark->
* profile your domain first.
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->benchmark->
* if you really need
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->benchmark->
* if you really need
doctrineORM● Reference
● Partial object*
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
$q = $em->createQuery("select partial u.{id,name} from \Domain\User u");
$comment->setPost($em->getReference(‘AcmeBundleEntityPost’, $postId));
* smelling tip, inconsistent objects
doctrineORM
● Change Hydration *
HYDRATE_OBJECT
$query->setHydrationMode(\Doctrine\ORM\Query::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->benchmark->
* Object Oriented will cry.
theHidden Hydrationcache *
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
* Be careful, only for Read operations
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
theHydrationcache * With ResultCache
With HydrationCache
* profile data from my domain not yours.
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->benchmark->
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->benchmark->
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
HTTPCache
● http specification● validation / expiration● safe methods● Edge Side Included
http://symfony.com/doc/current/book/http_cache.html
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->benchmark->
benchmark->profile->make change
metrics produces graphs decisions
composer install liuggio/statsd-client-bundle ~1
Optimizing dev tool
->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->
https://www.flickr.com/photos/itomcash/12953162645/sizes/l/
(multiple xdebug configs, db on ram, tricks, shared and private fixtures)
OOD, SOA, DDD, 12factorsArchitecture
BALANCED
OPTIMIZATION vs MAINTAINABILITY
BE
References1. 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-Caches4. https://speakerdeck.com/dshafik/lonestar-php-fast-not-furious-how-to-identify-and-fix-slow-code5. http://share.ez.no/learn/ez-publish/ez-publish-performance-optimization-part-1-of-3-introduction-
and-benchmarking/%28page%29/56. http://www.linuxhelp.in/2012/11/benchmarking-with-http-load.html7. https://speakerdeck.com/bastianhofmann/profiling-php-applications8. http://www.symfony.com9. http://blog.ircmaxell.com/2013/09/rambling-on-internals.html
10. .P of EAA page 184 Patterns of Enterprise Application Architecture by Martin Fowler11. https://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork.html12. http://docs.doctrine-project.org/en/latest/reference/change-tracking-policies.htmlFlush optimization13. http://slides.seld.be/?file=2011-10-20+High+Performance+Websites+with+Symfony2.html#1
Credits:https://www.flickr.com/photos/kelehen/8962203423https://www.flickr.com/photos/itomcash/12953162645
QUESTIONSAREALWAYSBETTERTHANANSWERS
@liuggio
https://joind.in/10783