Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Caching and data analysis will move your Symfony2 application to the next level

10,763 views

Published on

If you don't ignore data you could find a business value.

Published in: Technology
  • Login to see the comments

Caching and data analysis will move your Symfony2 application to the next level

  1. 1. Zzz and bla^3will move yourto the next level.???
  2. 2. I have notinvented anythingpleaseread carefullythe referencesnot yet 
  3. 3. I’m @liuggio(aka Giulio De Donato)I LOVEand its dark passengerfabpot
  4. 4. _ _ _ and _ _ _will move yourto the next level.???
  5. 5. _ _ _ and _ _ _will move yourto the next level.Symfony2application
  6. 6. proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/photos/455.jpg HTTP/1.1"200 986 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/nav/tab_left_middle.gifHTTP/1.1" 200 1020 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13(KHTML, like Gecko) Version/3.1 Safari/525.13" "-"dyn-530.optus.com.au - - [22/Apr/2009:18:52:51 +1200] "GET /images/photos/5332.jpg HTTP/1.1" 200244 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)" "-"<?xml version="1.0"?><catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developers Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date>dhcp-465.telstra.com.au - - [22/Apr/2009:18:52:51@charset "UTF-8";html,body,div,span,applet,object,iframe,h1,h2,h3,h4,+1200] "GET /gallery.php?section=entertainment HTTP/1.0" 200 58636 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en)AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /adclick.php?c=LME2 HTTP/1.1"302 378 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/ad/RH4Y.jpeg HTTP/1.1"200 1031 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/favicon.ico HTTP/1.1" 2002824 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /adclick.php?c=GD52 HTTP/1.1"302 379 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeIn your companyevery minutedata is generateda lot ofdifferenttotally
  7. 7. proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/photos/455.jpg HTTP/1.1"200 986 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/nav/tab_left_middle.gifHTTP/1.1" 200 1020 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13(KHTML, like Gecko) Version/3.1 Safari/525.13" "-"dyn-530.optus.com.au - - [22/Apr/2009:18:52:51 +1200] "GET /images/photos/5332.jpg HTTP/1.1" 200244 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)" "-"dhcp-465.telstra.com.au - - [22/Apr/2009:18:52:51 +1200] "GET /gallery.php?section=entertainmentHTTP/1.0" 200 58636 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13(KHTML, like Gecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /adclick.php?c=LME2 HTTP/1.1"302 378 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/ad/RH4Y.jpeg HTTP/1.1"200 1031 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/favicon.ico HTTP/1.1" 2002824 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /adclick.php?c=GD52 HTTP/1.1"302 379 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /stories.php?date=yesterdayHTTP/1.1" 200 4231 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13(KHTML, like Gecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /latest.php HTTP/1.1" 200 991 "-""Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, like Gecko)Version/3.1 Safari/525.13" "-"proxy-622.dsl.clear.net.nz - - [22/Apr/2009:18:52:51 +1200] "GET /latest-news.rss HTTP/1.0" 200 7199data- Difficult to read- Difficulto to measure- Time consuming
  8. 8. proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/photos/455.jpg HTTP/1.1"200 986 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/nav/tab_left_middle.gifHTTP/1.1" 200 1020 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13(KHTML, like Gecko) Version/3.1 Safari/525.13" "-"dyn-530.optus.com.au - - [22/Apr/2009:18:52:51 +1200] "GET /images/photos/5332.jpg HTTP/1.1" 200244 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)" "-"dhcp-465.telstra.com.au - - [22/Apr/2009:18:52:51 +1200] "GET /gallery.php?section=entertainmentHTTP/1.0" 200 58636 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13(KHTML, like Gecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /adclick.php?c=LME2 HTTP/1.1"302 378 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/ad/RH4Y.jpeg HTTP/1.1"200 1031 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/favicon.ico HTTP/1.1" 2002824 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /adclick.php?c=GD52 HTTP/1.1"302 379 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /stories.php?date=yesterdayHTTP/1.1" 200 4231 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13(KHTML, like Gecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /latest.php HTTP/1.1" 200 991 "-""Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, like Gecko)Version/3.1 Safari/525.13" "-"proxy-622.dsl.clear.net.nz - - [22/Apr/2009:18:52:51 +1200] "GET /latest-news.rss HTTP/1.0" 200 7199dataDifference audences and destination-business                                                      -applications  -external services-error logs ( js error, and application error)- . . .sucks
  9. 9. proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/photos/455.jpg HTTP/1.1"200 986 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/nav/tab_left_middle.gifHTTP/1.1" 200 1020 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13(KHTML, like Gecko) Version/3.1 Safari/525.13" "-"dyn-530.optus.com.au - - [22/Apr/2009:18:52:51 +1200] "GET /images/photos/5332.jpg HTTP/1.1" 200244 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)" "-"dhcp-465.telstra.com.au - - [22/Apr/2009:18:52:51 +1200] "GET /gallery.php?section=entertainmentHTTP/1.0" 200 58636 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13(KHTML, like Gecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /adclick.php?c=LME2 HTTP/1.1"302 378 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/ad/RH4Y.jpeg HTTP/1.1"200 1031 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /images/favicon.ico HTTP/1.1" 2002824 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /adclick.php?c=GD52 HTTP/1.1"302 379 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, likeGecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /stories.php?date=yesterdayHTTP/1.1" 200 4231 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13(KHTML, like Gecko) Version/3.1 Safari/525.13" "-"proxy-435.dialup.xtra.co.nz - - [22/Apr/2009:18:52:51 +1200] "GET /latest.php HTTP/1.1" 200 991 "-""Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, like Gecko)Version/3.1 Safari/525.13" "-"proxy-622.dsl.clear.net.nz - - [22/Apr/2009:18:52:51 +1200] "GET /latest-news.rss HTTP/1.0" 200 7199rocksdataDifference audences and destination-business                                                      -applications  -external services-error logs ( js error, and application error)- . . .
  10. 10. What we dowith data?_ _ _ _ _ !ignoretrack
  11. 11. We track data_ _ _ _ _ !ignorethen
  12. 12. dataBUSINESSVALUEthen
  13. 13. … still 42.andbussinessvalue?andbussinessvalue?4242hitchhikers guide to the galaxy
  14. 14. Our code generates business value when it runs,not when we write code.@coda
  15. 15. Store log and error dataQuery on past eventsLocate the bugKnow Servers/applicationstatus: healthy/resources/Responding as it should?Face the reality of myecosystemUnderstand why andwhere the app. is slowAnalyze the behaviourwhen it ‘s runningwatch databe alertedfast websiteNeedsdata…BUSINESSVALUE
  16. 16. NeedsBUSINESSVALUEloggingdebuggingmonitoringprofilingmetrickingoptimizingActionsStore log and error dataQuery on past eventsLocate the bugKnow Servers/applicationstatus: healthy/resources/Responding as it should?Face the reality of myecosystemUnderstand why andwhere the app. is slowAnalyze the behaviourwhen it ‘s runningwatch databe alertedfast website…
  17. 17. BUSINESSVALUEloggingdebuggingmonitoringprofilingmetrickingoptimizingActionsmonolog, Logstash,GraylogSentry / GraylogNagios, Cacti …external serviceXhProfstatsd-php-client,StatsD, Graphite
  18. 18. Its not about the accuracy of estimating itsabout facing the reality of uncertainty.@jimhighsmith
  19. 19. metricshttp://samate.nist.gov/index.php/Metrics_and_Measures.htmlhttp://nvlpubs.nist.gov/nistpubs/jres/104/6/j46gra.pdfThe IEEE Standard Glossary of Software Engineering Terminologyalso defines quality metric as :(1) A quantitative measure of the degree to whichan item possesses a given quality attribute.(2) A function whose inputs are software data and whose output is asingle numerical value that can be interpreted as the degree to whichthe software possesses a given quality attribute.* @param array $data* @return floatfunction calculateUglyness(array $data);Ninja territory
  20. 20. measuringhttp://samate.nist.gov/index.php/Metrics_and_Measures.htmlHOW TO MEASURE ANYTHING - FINDING THE VALUE OF “INTANGIBLES” IN BUSINESSsecond edition Hubbardexperimentally obtainingone or more quantity valuesthat can reasonably beattributed to a quantity.reasonably => uncertainty reductionNinja territory
  21. 21. What metricsmeans?DUNNOMerlin knows Yes Matrix Isaw the film
  22. 22. metricsVisual tool tobetter understandreality
  23. 23. metricsalign technical andbusiness vision ofIT.
  24. 24. producesgraphsDECISIONSmetrics
  25. 25. StatsD simple— Node.JS daemon. Listens for message over UDPsimple daemon for easy stats aggregation.Graphite rude <key> <numeric value> <timestamp>Graphite stores numeric time-series data.— Real-time graphing system— Components:— Carbon stores the data in Graphite’s specialized database.— The data can then be visualized through graphiteweb interface developed with django.— Whisper allows carbon to write multiple data points.metrics
  26. 26. Symfony2 integration:there’s a bundle for it ‘liuggio/StatsDClientBundle’— Amount of visitor— Logged vs Anonymous user— Doctrine collector— Amount of PHP peak— Execution Time— Monolog— Easily add a new metric:$this->get(‘stats_d_client.factory)->increment(’upload.file);metrics
  27. 27. Symfony2 integration PHP Peakmetricsthis is a‘Timer’
  28. 28. Concepts— Bucket— Values— Flush Intervalmetricsmetrics— Counter— Gauge— Timer— Set— SampleUnderstand StatsD
  29. 29. GAUGES are arbitrary values, which can be recorded.metrics$factory = $this->get(stats_d_client.factory);$data[] = $factory->gauge(cart.item.number, 30);$data[] = $factory->gauge(cart.item.number, 34);$data[] = $factory->gauge(cart.item.number, 38);$this->get(stats_d_client.service’)->send($data);cart.item.number|38Result is the last one (per flush time)
  30. 30. COUNTS increment and decrement a value.metrics$factory = $this->get(stats_d_client.factory);$data[] = $factory->increment(user.visitor);$this->get(stats_d_client.service’)->send($data);The value stay in memory until the flush interval.
  31. 31. TIMERS collects numbers.metrics$factory = $this->get(stats_d_client.factory);$data[] = $factory->timing(’php-peak, 500);$this->get(stats_d_client.service’)->send($data);Operations [mean, upper, sum, lower, count, ...]are already calculated 
  32. 32. Collect from different sources# echo "deploy.web:1|c" | nc -w 1 -u graphite.com 8125
  33. 33. trackeverythings thatmovescanmetrics
  34. 34. you have moredata than you thinkand you need lessdata than you thinkMORELESS
  35. 35. new metrics— Why do you want to know?— How much do you know now?— What is the value to additionalinformation
  36. 36. The time you spend for metrics has a cost,and usually is more economicalthan many managers assume.maybe becausethey didn’tmeasure.bazingametrics
  37. 37. producesDECISIONSfastmetricsgood
  38. 38. good and fastDECISIONSBUSINESShelp the
  39. 39. and _ _ _will move yourto the next level.metrics???
  40. 40. OptimizingPerformance problems?Enlargeyourprofiler
  41. 41. Great caching is like great sex.It hides all the real problems@vivekhaldar
  42. 42. Wikipedia: “a Cache ( /’kæ∫/ KASH[1]) is acomponent that transparently stores dataso that future requests for that data can beserved faster.”the cache misshappens once for thesame response.given the same request the Cache miss happens once.given the same request the response is not ‘processed’ twice.A Cache Hit happens if the Cachecontains the answer.A Cache Miss happens if the Cachedoesn’t contain the answer.The perfect cache when
  43. 43. CACHEIS A RESPONSEMATTER
  44. 44. — Application caching: app/cache with opcode— Response caching and HTTP-CACHE— Doctrine2 Query and Result Caching— CDN and static files— OtherCACHE + SF2
  45. 45. Terravison vendor folder is about 340 MBThe app/cache/prod folder is about 3000 files1. Annotation (Entity/Controller/...)2. Templatea. name => fileb. twig in php3. Assetic (definition)4. Url matcher, Url creator5. Translations array key=>value6. Doctrine Entity Proxy./app/cache
  46. 46. Add your class to the services with the tag kernel.cache_warmerNote: the CacheWarmerInterface is not properly the same as the snippet.cache warmernamespace SymfonyComponentHttpKernelCacheWarmer;interface CacheWarmerInterface/*** Warms up the cache.* @param string $cacheDir The cache directory*/public function warmUp($cacheDir);public function isOptional()
  47. 47. Symfony2 is a web framework built from scratch aroundthe http specification— a resource could be Fresh || Stale— http-caching only on safe method— validation || expirationHTTP
  48. 48. HTTP cache
  49. 49. HTTP headers$response->setPublic() //default is private$response->setMaxAge(600);// Same as above but only for shared caches$response->setSharedMaxAge(600);$response->setETag(md5($response->getContent()));$response->setLastModified($datetime);if ($response->isNotModified($this->getRequest()))...$response->setVary(‘Accept-Encoding’);symfony.com/doc/master/book/http_cache.html
  50. 50. The leastexpensivequery is thequery younever run.
  51. 51. DoctrineCommonCacheCache(There’s not tagging)doctrine cache// Fetches an entry from the cache.string function fetch($id);// Tests if an entry exists in the cache.bool function contains($id);// Put data into the cache.bool function save($id, $data, $lifeTime);// Deletes a cache entry.bool function delete($id);
  52. 52. MetaData Cache Annotation (OPCODE)Query Cache DQL parsed into SQL(OPCODE/REDIS/MEMCACHED)Result Cache The query result(REDIS/MEMCACHED/OPCODE)doctrine caches
  53. 53. doctrine cacheClass ProductRepository...$query = $this->createQueryBuilder(‘p’)->where(‘p.price > :price’)->setParameter(‘price’, ‘19.99’)->getQuery();$products = $query->getResult();$query->useResultCache(true,$lifetime,__METHOD__ . serialize($query->getParameters()));$query->useQueryCache();
  54. 54. There are only two hard things in Computer Sciencecache invalidation and naming thingsPhil KarltonThere are only two hard things in Computer Sciencecache invalidation, naming things and off-by-1errors.anonymous
  55. 55. InvalidationEspecially for http, but in general, you should not waste time toinvalidate a value, but you should invest resources to find the righttime of validity.Cache miss stormWhen a resource is no longer available (cache miss), and is requiredby many clients simultaneously (storm), a congestion happens.cache miss storm, cache stampede,dog-piling, APC Cache slamcache
  56. 56. Pre-Caching/CachebackSometimes you need to have some data already available and youdon’t want that the first request wait.Uniqueness of the cache keyAttention to the conflicts in the key definition, always put a prefix ornamespace such as language or role.Http-Microcaching:Save all the responses for 1||2 secondslot of examples on internet for Nginxcache
  57. 57. and _ _ _will move yourto the next level.???
  58. 58. Performancetips - rule #1Mantainibilitywin overPerformance
  59. 59. — The user should never wait    slow operation data in a queue (Redis/*MQ)    processed by a consumer (cli + supersivord):— Image Manipulation— File Creation (pdf)— Sending email as spool— I/O operation— Use Session with Redis/Memcache— Caution on what do you do in the loop && nested loopeasy …
  60. 60. … tips— Must! OPCODE: APC using the apc.stat = 0— $php composer.phar dump-autoload –optimize— Twig.c php extension— Assets— Minify and combine all of your CSS and JS files— Run all (or just some) of your CSS or JS filesthrough some sort of compiler,such as LESS, SASS or CoffeeScript— Run image optimizations on your imageshttp://symfony.com/doc/current/book/performance.htmlhttps://speakerdeck.com/alexandresalome/symfony2-and-performances
  61. 61. the DIC of sf2.3 is lazyA lazy loading value holder proxy is a virtual proxy that wraps andlazily initializes a "real" instance of the proxied class.<service id="foo" class="AcmeFoo" lazy="true" />When do I use a lazy value holder?-Your object takes a lot of time and memory to be initialized (with alldependencies)-Your object is not always used, and the instantiation overhead can beavoidedgithub.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.mdgithub.com/Ocramius/symfony-docs/blob/master/components/dependency_injection/lazy_services.rst
  62. 62. Cache Driver# config_prod.ymlservices:cache:class: DoctrineCommonCacheApcCache #MemcacheCache# config_dev.ymlservices:cache:class: DoctrineCommonCacheArrayCacheif ($fooString = $this->get(‘cache’)->fetch(‘foo’)) {$foo = unserialize($fooString);} else {// do something with $foo$this->get(‘cache’)->save(‘foo’, serialize($foo),$lifetime);}http://docs.doctrine-project.org/en/latest/reference/caching.html
  63. 63. DOCTRINEObject relational mapper(ORM) for PHP that sits ontop of a powerful databaseabstraction layer (DBAL)
  64. 64. doctrine >=2.4New features:https://speakerdeck.com/asm89/what-is-new-in-doctrineit ill be faster ?NO
  65. 65. doctrine >= 2.3—2.4 flush multiples entities$em->flush(array($entityA, $entityB);—2.4 paginator—2.4 master slave—2.4 join to the interface—2.3 You can override metadata—2.3 Event listener in a separated class/*** @ORMManyToOne(targetEntity="Tvision...EntityStatusInterface")**/https://speakerdeck.com/asm89/what-is-new-in-doctrine
  66. 66. Optimize Doctrinehttp://xkcd.com/1195/
  67. 67. Optimize Doctrinehttps://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork.htmlstep1:deeply understand UOWstep2:goto step1
  68. 68. $article = new Article();$article->setHeadline(I love PHPday);$em->persist($article); // the entity is managed$em->flush(); // the entity is persisted...$article->setHeadline(I love JSday and PHPday);$em->flush(); // the Headline is updatedon the persistence layer$em->persist($article);
  69. 69. $article = $em->find(Article, 1234);$article->setHeadline(’I love GRUSP);$article2 = $em->find(Article, 1234);echo $article2->getHeadline(); // I love GRUSPif ($article === $article2) ---> true!!
  70. 70. Unit of workhttps://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork.htmlMaintains a list of objects affected by a business transaction andcoordinates the writing out of changes and the resolution of concurrencyproblems.P of EAA page 184 Patterns of Enterprise Application Architectureby Martin FowlerUOW = keeps trackchanges of ObjectsTL;DR
  71. 71. Identity MapThe flush has a cost— Number of managed Entities— Tracking policy— Type of entitieshttps://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork.htmlcontains all the managed entitiesOn flush makes a property by property comparison for all theentities managed.
  72. 72. tracking policyDoctrine definition:“is the process of determining what has changed in managed entitiessince the last time they were synchronized with the database.”— implicit tracking (default and simple)— explicit tracking (suggested for lot of entities managed)— notify tracking/*** @Entity* @ChangeTrackingPolicy("DEFERRED_EXPLICIT”)http://docs.doctrine-project.org/en/latest/reference/change-tracking-policies.htmlFlush optimization #1
  73. 73. tracking policyDeferred ImplicitUOW = DBhttp://docs.doctrine-project.org/en/latest/reference/change-tracking-policies.htmlDeferred ExplicitUOW != DB— UOW = ’I love PUG Roma’— DB is not updated.Need the persist!Flush optimization #1// $article is already managed or loaded// we are changing the Headline$article->setHeadline(I love PUG Roma);$em->flush();echo $article->getHeadline();// is ‘I love PUG Roma’
  74. 74. Read only entity/*** @Entity* @READONLY*/Class Status {$em->getUnitOfWork->markReadOnly($entity);Temporarily mark entities as read onlyFlush optimization #2
  75. 75. Flush only on fewentities$em->flush($entity)Flush optimization #3
  76. 76. Reducing theflush costThe flush has a cost— Number of managed Entities (flushing only some)— Tracking policy (explicit || implicit)— Type of entities (read-only || temp. read-only)Recap.
  77. 77. reference$post = $postRepository->find($postId);$comment->setPost($post);...// $postId comes from somewhere$comment->setPost($em->getReference(‘AcmeBundleEntityPost’, $postId));.
  78. 78. lazy associationsAssociation are LAZY by default// Order/*** @ManyToOne(targetEntity=”Cart”, cascade={“all”},fetch=”EAGER”)*/private $cart<?php$query = $em->createQuery(“SELECT o FROM Order o”);$query->setFetchMode(“Order”, “Cart”, “LAZY”);$query->execute();Change the fetch mode
  79. 79. associationsWhat’s better than LAZY ?/** @ORMManyToMany(targetEntity=”Trips”,mappedBy=”rides”,fetch=”EXTRA_LAZY”)*/private $trips;$turin->getTrips()->contains($turinCaselle);$turin->getTrips()->count();$turin->getTrips()->slice($offset, $length);
  80. 80. and _ _ _will move yourto the next level.Symfony2applicationOptimization
  81. 81. Done everything?
  82. 82. Did you reallyneed it?
  83. 83. Rationality
  84. 84. Knowing when optimization is prematuredefines the differences the master engineerand the apprenticeTheo SchlossnagleOptimizationWe’ve heardthis a milliontimes
  85. 85. When you discover the real problem, you done the half.Problem are not all equals, and most are illusions.Pieter Hintjens
  86. 86. Optimizationyou are doing it wrong:— Cache everything— Guessing / action without measurement
  87. 87. and _ _ _will move yourto the next level.Symfony2applicationOptimization
  88. 88. andwill move yourto the next level.Symfony2applicationOptimization
  89. 89. andwill move yourto the next level.BUSINESS
  90. 90. andwill move yourto the next level.metrics
  91. 91. andwill move yourto the next level.BUSINESS
  92. 92. andwill move yourto the next level.BUSINESS
  93. 93. andwill move yourto the next level.metrics
  94. 94. andwill move yourto the next level.metricsOptimization
  95. 95. andwill move yourto the next level.metricsSymfony2application
  96. 96. datathenBUSINESS VALUE
  97. 97. Performance optimization couldMetrics shouldRationality must
  98. 98. @liuggioplease read the references
  99. 99. https://speakerdeck.com/asm89/what-is-new-in-doctrinehttp://docs.doctrine-project.org/http://nvlpubs.nist.gov/nistpubs/jres/104/6/j46gra.pdfhttp://ilia.ws/files/phpuk_bottlenecks.pdfhttp://www.slideshare.net/anton_chuvakin/log-mining-beyond-log-analysishttp://pivotallabs.com/139-metrics-metrics-everywhere/http://www.hubbardresearch.com (HOW TO MEASURE ANYTHING - FINDING THE VALUE OF “INTANGIBLES” )https://people.mozilla.com/~rmiller/heka-intro-2013-03/#/2http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-18https://developers.google.com/speed/docs/best-practices/cachinghttp://notmysock.org/blog/php/user-cache-timebomb.htmlhttp://sonata-project.org/blog/2012/5/15/assetic-package-configurationhttp://www.slideshare.net/jonkruger/advanced-objectorientedsolid-principleshttp://slides.seld.be/?file=2011-10-20+High+Performance+Websites+with+Symfony2.htmlhttp://stackoverflow.com/questions/8893081/how-to-cache-in-symfony-2http://www.slideshare.net/postwait/scalable-internet-architecturehttp://techportal.inviqa.com/2009/12/01/profiling-with-xhprof/http://www.mysqlperformanceblog.com/2010/09/10/cache-miss-stormhttp://www.slideshare.net/iamcal/scalable-web-architectures-common-patterns-ap...http://blog.servergrove.com/2012/04/18/how-to-create-a-cache-warmer-in-symfony2/http://www.slideshare.net/jwage/doctrine-intherealworldsf-live2011sanfranhttp://www.slideshare.net/quipo/scalable-architectures-taming-the-twitter-firehosehttps://github.com/liuggio/StatsDClientBundlehttp://www.slideshare.net/quipo/monitoring-at-scale-intuitive-dashboard-designhttp://www.slideshare.net/ihower/rails-performancehttp://www.infoq.com/presentations/Architecture-Scale-ZeroMQhttps://speakerdeck.com/keithpitt/keith-and-marios-guide-to-fast-websiteshttp://www.slideshare.net/anton_chuvakin/log-mining-beyond-log-analysishttp://marco-pivetta.com/proxy-pattern-in-php/#/9http://www.slideshare.net/guilhermeblanco/object-calisthenics-applied-to-phphttp://talks.php.net/show/dojo/https://github.com/symfony/symfony/pull/7818https://speakerdeck.com/roidrage/metrics-monitoring-logginghttp://codahale.com/codeconf-2011-04-09-metrics-metrics-everywhere.pdfhttp://pivotallabs.com/139-metrics-metrics-everywhere/http://blog.pkhamre.com/2012/07/24/understanding-statsd-and-graphite/References

×