Why we choose Symfony2

54
#MERIXSTUDIO

Transcript of Why we choose Symfony2

Page 1: Why we choose Symfony2

#MERIXSTUDIO

Page 2: Why we choose Symfony2
Page 3: Why we choose Symfony2
Page 4: Why we choose Symfony2

#SYMFONY 2MAKES LIFE EASIER

Page 5: Why we choose Symfony2

#WHYILOVESYMFONY2 And why you should too

Page 6: Why we choose Symfony2
Page 7: Why we choose Symfony2

#COMMUNITYKnowledge sharing

Page 8: Why we choose Symfony2

Kris Wallsmith

Wow, such SymfonyCon

Page 9: Why we choose Symfony2
Page 10: Why we choose Symfony2

#PROJECTS USING SYMFONY

◇ Drupal◇ Composer◇ Laravel◇ Shopware◇ phpBB◇ Magento◇ Silex◇ Codeception◇ Behat◇ Thelia◇ eZ publish ◇ Piwik

Page 11: Why we choose Symfony2

#KNOWTHETRICKSAnd why you should to

Page 12: Why we choose Symfony2

#SMALL TRICKS

◇ composer dump-autoload --optimize

◇ {% do form.name.setRendered %}

◇ debug channels: "!event"

Page 13: Why we choose Symfony2

#CONSOLE MORE:ELEGANT

◇ BEGINNERS WAYphp app/console --env=prod assetic:dump

◇ BETTER WAY ./app/console --env=prod assetic:dump

◇ PROS WAYprod a:d

You audience will listen to you or read the content, but won’t do both.

Page 14: Why we choose Symfony2

◇ Query Cache

◇ Result Cache

◇ Metadata Cache

Page 15: Why we choose Symfony2

#QUITE UNCOMFORTABLE

SERIALIZE/DESERIALIZE HANDLE THE ASSOCIATIONS

WHEN UPDATE THE DATA ?

Page 16: Why we choose Symfony2

WHEN TO SET UP CACHE IN THE PROJECT?

to fast?

not now

to late - deadline

Page 17: Why we choose Symfony2

#SECOND LEVEL CACHE

Hell has frozen over

Page 18: Why we choose Symfony2

WHAT S NEEDED

RedisMemcached

Apcdoctrine/orm>=2.5.* Database

Page 19: Why we choose Symfony2

# Doctrine Configurationdoctrine: dbal: ... orm: ... entity_managers: default:

second_level_cache: enabled: true log_enabled: true region_cache_driver: redis

filters: ...

#DOCTRINE CONFIGURATION

Page 20: Why we choose Symfony2

CACHING MODE

READ_ONLY (DEFAULT)■ Can do reads,

inserts and deletes, cannot perform updates or employ any locks.

■ Useful for data that is read frequently but never updated.

■ Best performer.■ It is Simple.

NONSTRICT_READ_WRITE■ Read Write Cache

doesn t employ any locks but can do reads, inserts, updates and deletes.

■ Good if the application needs to update data rarely.

READ_WRITE■ Read Write cache

employs locks before update/delete.

■ Use if data needs to be updated.

■ Slowest strategy.■ To use it a the

cache region implementation must support locking.

Page 21: Why we choose Symfony2

<?php/** * @Entity * * @Cache(usage="READ_ONLY", region="my_entity_region") **/class Country{ /** * @Id * @GeneratedValue * @Column(type="integer") */ protected $id;

/** * @Column(unique=true) */ protected $name; // other properties and methods}

#ENTITY

Page 22: Why we choose Symfony2

<?php/** * @Entity * @Cache("NONSTRICT_READ_WRITE") */class State{ /** * @Id * @GeneratedValue * @Column(type="integer") */ protected $id;

/** * @Column(unique=true) */ protected $name;

#CACHE ASSOCIATION

/** * @Cache("NONSTRICT_READ_WRITE") * @ManyToOne(targetEntity="Country") * @JoinColumn(name="country_id", referencedColumnName="id") */ protected $country;

/** * @Cache("NONSTRICT_READ_WRITE") * @OneToMany(targetEntity="City", mappedBy="state") */ protected $cities;

// other properties and methods}

The most common use case is to cache entities. But we can also cache relationships. It caches the primary keys of association and cache each element will be cached into its region.

Page 23: Why we choose Symfony2

Place your screenshot here

Get insight into the errors that affect your customers.

Page 24: Why we choose Symfony2

monolog: handlers: main: type: fingers_crossed action_level: error handler: grouped_main

sentry: type: raven dsn: 'https://<key>:<secret>@app.getsentry.com/<project>' level: error

# Groups grouped_main: type: group members: [sentry, streamed_main]

# Streams streamed_main: type: stream path: %kernel.logs_dir%/%kernel.environment%.log level: error

#SENTRY_SYMFONY

Page 25: Why we choose Symfony2

#BEST FEATURES

◇ KNOW IMMEDIATELY IF SOMETHING GOES WRONG

◇ SNOZE TIL NEXT VERSION

◇ SEE THE IMPACT OF EACH RELEASE IN REAL-TIME

◇ COLLECT AND GROUP ERRORS

◇ DIAGNOSE AND FIX ISSUES FASTER THAN EVER

◇ NOTIFICATIONS AND EXTRA READABLE LOGS

Page 26: Why we choose Symfony2

#IS SYMFONY FAST?

Page 27: Why we choose Symfony2

SO PHALCON MAYBE ????

Page 28: Why we choose Symfony2

IT S ABOUT TOOLS

#IT S NOT ONLY PERFORMANCE

Page 29: Why we choose Symfony2

#INITIALIZATION TIME

Page 30: Why we choose Symfony2

KERNEL.REQUEST.LOADING//JMSDebuggingBundle

#MORE ABOUT WEIGHT OF THE LISTENERS

Page 31: Why we choose Symfony2

SYMFONY/COMPONENT/SECURITY/HTTP/FIREWALL

Page 32: Why we choose Symfony2

public function vote(Token Interface $token, $object, array $attributes)

// … details: checking if we support this attribute

if(in_array(‘ROLE_SUPER_ADMIN’, $oken->getRoles())){

return self::ACCESS_GRANTED;

}

//get the user, force to null if we’re anonymous

$user = ($token->getUser() instanceof User) ? $token->getUser() : null;

if($object->getOwner() && $object->getOwner() == $user) {

return self::ACCESS_GRANTED;

}

return VoterInterface::ACCESS_DENIED;

}

#CUSTOM VOTER

Page 33: Why we choose Symfony2

◇ SEPARATED BUSINESS LOGIC !!!

◇ NOT USING ACL :))

◇ EASY TO UNIT TESTS

◇ SIMPLE ! AND REUSABLE !

#WHY TO DO THAT

Page 34: Why we choose Symfony2

$sc->isGranted( EDIT , $blog )

Page 35: Why we choose Symfony2

SPOOLING EMAILS

SPOOL IN MEMORYswiftmailer:

# ...

spool: { type: memory }

SPOOL USING FILESswiftmailer:

# ...

spool:

type: file

path: /path/to/spooldir

TIME LIMITphp bin/console swiftmailer:spool:send --time-limit=10 --env=prod

MESSAGE LIMIT

php bin/console swiftmailer:spool:send --message-limit=10 --env=prod

Page 36: Why we choose Symfony2

PRIORITIZED EMAILS

HIGH-PRIORITY EMAILS$container->get('swiftmailer.mailer.instant)->...

REGULAR EMAILS

$container->get('swiftmailer.mailer')->...

$container->get('swiftmailer.mailer.delayed')->...

Page 37: Why we choose Symfony2

#STILL TO SLOW?

Page 38: Why we choose Symfony2

#DUMMY HELLO TEST

~6-18ms With it

~40-50msWithout

Page 39: Why we choose Symfony2

#PHP PROCES MANAGER

Page 40: Why we choose Symfony2

PHP-PM...is a process manager, supercharger and load balancer for PHP applications.

It's based on ReactPHP and works best with applications that use request-response frameworks like Symfony's HTTPKernel. The approach of this is to kill the expensive bootstrap of PHP (declaring symbols, loading/parsing files) and the bootstrap of feature-rich frameworks. See Performance section for a quick hint. PHP-PM basically spawns several PHP instances as worker bootstraping your application (eg. the whole Symfony Kernel) and hold it in the memory to be prepared for every incoming request: This is why PHP-PM makes your application so fast.

Page 41: Why we choose Symfony2

# change minimum-stability to dev in your composer.json (until we have a version tagged): "minimum-stability": "dev"

composer require php-pm/php-pm:dev-master

composer require php-pm/httpkernel-adapter:dev-master #if you have httpkernel (laravel, symfony)

./vendor/bin/ppm config --bootstrap=symfony #places a ppm.json in your directory

./vendor/bin/ppm start #reads ppm.json and starts the server like you want

#INSTALLATION

Page 42: Why we choose Symfony2

There will be a memory leak !

Fabpot is working on it.

Page 43: Why we choose Symfony2

"bridge": "HttpKernel", "host": "127.0.0.1", "port": 8001, "workers": 8, "app-env": "dev", "debug": 1, "logging": 1, "static": true, "bootstrap": "symfony", "max-requests": 1000, "concurrent-requests": false, "php-cgi": false

#CONFIGURATION

Page 44: Why we choose Symfony2

#LET S USE IT AS A DEV SERVER

Page 45: Why we choose Symfony2

Server Software: Server Hostname: localhostServer Port: 8001

Document Path: /Document Length: 26037 bytes

Concurrency Level: 10Time taken for tests: 133.327 secondsComplete requests: 5000Failed requests: 0Total transferred: 131720000 bytesHTML transferred: 130185000 bytesRequests per second: 37.50 [#/sec] (mean)Time per request: 266.654 [ms] (mean)Time per request: 26.665 [ms] (mean, across all concurrent requests)Transfer rate: 964.79 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median maxConnect: 0 0 0.0 0 1Processing: 235 266 7.2 265 469Waiting: 219 260 7.2 259 463Total: 236 266 7.2 265 469

dev s:start localhost:8001

Page 46: Why we choose Symfony2

Server Software: Server Hostname: localhostServer Port: 8001

Document Path: /Document Length: 26050 bytes

Concurrency Level: 10Time taken for tests: 12.233 secondsComplete requests: 5000Failed requests: 0Total transferred: 131255000 bytesHTML transferred: 130250000 bytesRequests per second: 408.72 [#/sec] (mean)Time per request: 24.466 [ms] (mean)Time per request: 2.447 [ms] (mean, across all concurrent requests)Transfer rate: 10477.97 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median maxConnect: 0 0 0.0 0 0Processing: 10 24 15.1 23 524Waiting: 7 22 14.9 20 519Total: 10 24 15.1 23 524

./vendor/bin/ppm start

Page 47: Why we choose Symfony2

~11 % ?? Whoa! That’s a big number, aren’t you proud?

Page 48: Why we choose Symfony2

~11 X !!!Whoa! That’s a big number, aren’t you proud?

Nope

Page 49: Why we choose Symfony2

#PHPFastCGI

Page 50: Why we choose Symfony2

PHPFastCGI…is a collection of libraries that can be used to build FastCGI applications in PHP. Unlike normal PHP applications, these applications can stay alive between request cycles - improving speed and lowering resource use. is a process manager, supercharger and load balancer for PHP applications

Speedfony Bundle.A symfony2 bundle which allows applications to reduce overheads by exposing symfony's Request-Response structure to a FastCGI daemon.

Page 51: Why we choose Symfony2

BUTSTILL NOT STABLE

composer require "phpfastcgi/speedfony-bundle:^0.8"

// app/AppKernel.php // ... new PHPFastCGI\SpeedfonyBundle\PHPFastCGISpeedfonyBundle(), php app/console speedfony:run --port 5000 --env="prod"

Page 52: Why we choose Symfony2

Conclusion

There is lots to be gained and lots to be lost by daemonizing your PHP applications. It is very important that you are aware of the potential issues you could face and how to mitigate these. However, with a properly designed and carefully considered application - you can reach response speeds well beyond the dreaming limits of conventional PHP applications.

Page 53: Why we choose Symfony2

ANY QUESTIONS?

You can find me [email protected]

Page 54: Why we choose Symfony2

SOURCES

This presentations uses the following sources:◇ http://www.slideshare.net/javier.eguiluz/symfony-tips-and-tricks◇ http://doctrine-orm.readthedocs.org/projects/doctrine-

orm/en/latest/reference/second-level-cache.html◇ https://www.youtube.com/watch?

v=DuWtvjQCoZk&index=1&list=PLo7mBDsRHu12dJVHaL2Eu5qDUuoe6xq_5

◇ http://espeo.eu/blog/is-phalcon-really-so-good/◇ https://www.youtube.com/watch?

v=DuWtvjQCoZk&index=1&list=PLo7mBDsRHu12dJVHaL2Eu5qDUuoe6xq_5