benchmark profile and boost your symfony application

51
RATIONALLYBOOST SYMFONY2APPLICATION 2014 VERSION

Upload: giulio-de-donato

Post on 13-Jan-2015

5.992 views

Category:

Technology


3 download

DESCRIPTION

@symfonyday portugal

TRANSCRIPT

Page 1: Benchmark Profile and Boost your Symfony application

RATIONALLYBOOSTSYMFONY2APPLICATION

2014 VERSION

Page 2: Benchmark Profile and Boost your Symfony application

welcometothebundle.com@liuggio welcometothebundle.com

Page 3: Benchmark Profile and Boost your Symfony application

PERFORMANCE PROBLEM?

Page 4: Benchmark Profile and Boost your Symfony application

PERFORMANCE PROBLEM?TODAY SPEED IS A FEATURE

Page 5: Benchmark Profile and Boost your Symfony application

BUSINESSNEEDSSPEED?

(speed in terms of adding a new feature, bug hunting ...)

Page 6: Benchmark Profile and Boost your Symfony application

loadbalancer

reverseproxy

CDN

web server

MQ

DBcache

sessionstorage

DB

internalstorage

BROWSER

privatecache

shared cache

DNS

ISP

your company

Page 7: Benchmark Profile and Boost your Symfony application

GUESS

PRIDE PREJUDICE AND GUESSING

Page 8: Benchmark Profile and Boost your Symfony application

THEENDLESSCYCLE

make change

benchmark

profile

Page 9: Benchmark Profile and Boost your Symfony application

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

Page 10: Benchmark Profile and Boost your Symfony application

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->

Page 11: Benchmark Profile and Boost your Symfony application

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

Page 12: Benchmark Profile and Boost your Symfony application

->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->

Page 13: Benchmark Profile and Boost your Symfony application

->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->

Page 14: Benchmark Profile and Boost your Symfony application

->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->

Page 15: Benchmark Profile and Boost your Symfony application

->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->

Page 16: Benchmark Profile and Boost your Symfony application

->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->

Page 17: Benchmark Profile and Boost your Symfony application

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

Page 18: Benchmark Profile and Boost your Symfony application

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->

Page 19: Benchmark Profile and Boost your Symfony application

● 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

Page 20: Benchmark Profile and Boost your Symfony application

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)

Page 21: Benchmark Profile and Boost your Symfony application

● 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

Page 22: Benchmark Profile and Boost your Symfony application

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)

Page 23: Benchmark Profile and Boost your Symfony 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)

Page 24: Benchmark Profile and Boost your Symfony application

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

Page 25: Benchmark Profile and Boost your Symfony application

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->

Page 26: Benchmark Profile and Boost your Symfony application

[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->

Page 27: Benchmark Profile and Boost your Symfony application

yourcachewarmup->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->

Page 28: Benchmark Profile and Boost your Symfony application

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->

Page 29: Benchmark Profile and Boost your Symfony application

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->

Page 30: Benchmark Profile and Boost your Symfony application

ORMcachesmetadata, query, result

->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change

Page 31: Benchmark Profile and Boost your Symfony application

understandUOWUnit Of Work

->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->

Page 32: Benchmark Profile and Boost your Symfony application

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

Page 33: Benchmark Profile and Boost your Symfony application

UnitofWork

Managed

->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->

Page 34: Benchmark Profile and Boost your Symfony application

UnitofWork

Managed

->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->profile->make change->benchmark->

Page 35: Benchmark Profile and Boost your Symfony application

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.

Page 36: Benchmark Profile and Boost your Symfony application

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

Page 37: Benchmark Profile and Boost your Symfony application

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

Page 38: Benchmark Profile and Boost your Symfony application

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

Page 39: Benchmark Profile and Boost your Symfony application

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.

Page 40: Benchmark Profile and Boost your Symfony application

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

Page 41: Benchmark Profile and Boost your Symfony application

->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.

Page 42: Benchmark Profile and Boost your Symfony application

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->

Page 43: Benchmark Profile and Boost your Symfony application

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->

Page 44: Benchmark Profile and Boost your Symfony application

->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

Page 45: Benchmark Profile and Boost your Symfony application

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->

Page 46: Benchmark Profile and Boost your Symfony application

benchmark->profile->make change

metrics produces graphs decisions

composer install liuggio/statsd-client-bundle ~1

Page 47: Benchmark Profile and Boost your Symfony application

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)

Page 48: Benchmark Profile and Boost your Symfony application

OOD, SOA, DDD, 12factorsArchitecture

Page 49: Benchmark Profile and Boost your Symfony application

BALANCED

OPTIMIZATION vs MAINTAINABILITY

BE

Page 50: Benchmark Profile and Boost your Symfony application

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

Page 51: Benchmark Profile and Boost your Symfony application

QUESTIONSAREALWAYSBETTERTHANANSWERS

@liuggio

https://joind.in/10783