Why we choose Symfony2

Post on 19-Feb-2017

138 views 3 download

Transcript of Why we choose Symfony2

#MERIXSTUDIO

#SYMFONY 2MAKES LIFE EASIER

#WHYILOVESYMFONY2 And why you should too

#COMMUNITYKnowledge sharing

Kris Wallsmith

Wow, such SymfonyCon

#PROJECTS USING SYMFONY

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

#KNOWTHETRICKSAnd why you should to

#SMALL TRICKS

◇ composer dump-autoload --optimize

◇ {% do form.name.setRendered %}

◇ debug channels: "!event"

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

◇ Query Cache

◇ Result Cache

◇ Metadata Cache

#QUITE UNCOMFORTABLE

SERIALIZE/DESERIALIZE HANDLE THE ASSOCIATIONS

WHEN UPDATE THE DATA ?

WHEN TO SET UP CACHE IN THE PROJECT?

to fast?

not now

to late - deadline

#SECOND LEVEL CACHE

Hell has frozen over

WHAT S NEEDED

RedisMemcached

Apcdoctrine/orm>=2.5.* Database

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

second_level_cache: enabled: true log_enabled: true region_cache_driver: redis

filters: ...

#DOCTRINE CONFIGURATION

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.

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

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

Place your screenshot here

Get insight into the errors that affect your customers.

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

#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

#IS SYMFONY FAST?

SO PHALCON MAYBE ????

IT S ABOUT TOOLS

#IT S NOT ONLY PERFORMANCE

#INITIALIZATION TIME

KERNEL.REQUEST.LOADING//JMSDebuggingBundle

#MORE ABOUT WEIGHT OF THE LISTENERS

SYMFONY/COMPONENT/SECURITY/HTTP/FIREWALL

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

◇ SEPARATED BUSINESS LOGIC !!!

◇ NOT USING ACL :))

◇ EASY TO UNIT TESTS

◇ SIMPLE ! AND REUSABLE !

#WHY TO DO THAT

$sc->isGranted( EDIT , $blog )

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

PRIORITIZED EMAILS

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

REGULAR EMAILS

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

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

#STILL TO SLOW?

#DUMMY HELLO TEST

~6-18ms With it

~40-50msWithout

#PHP PROCES MANAGER

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.

# 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

There will be a memory leak !

Fabpot is working on it.

"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

#LET S USE IT AS A DEV SERVER

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

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

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

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

Nope

#PHPFastCGI

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.

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"

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.

ANY QUESTIONS?

You can find me ata.klimczyk@merixstudio.com

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