symfony - modern technology in practice, webexpo prague

44
Symfony - modern technology in practice WebExpo 2010 Jaroslav Bauml, Pavel Campr BARTON STUDIO s.r.o. 24 Sep 2010 Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 1 / 44

Upload: pavel-campr

Post on 21-Jan-2015

2.610 views

Category:

Technology


2 download

DESCRIPTION

On an example project, build upon the symfony 1.4, using APC (Alternative PHP Cache) and XHProf (hierarchical profiler for PHP), we demonstrate possibilities of logging, debugging and profiling of several application layers. Caching comes in the end.

TRANSCRIPT

Page 1: Symfony - modern technology in practice, Webexpo Prague

Symfony - modern technology in practiceWebExpo 2010

Jaroslav Bauml, Pavel Campr

BARTON STUDIO s.r.o.

24 Sep 2010

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 1 / 44

Page 2: Symfony - modern technology in practice, Webexpo Prague

Symfony

"Full-featured" MVC framework. ... full of buzzwordsRESTful frameworkRAD frameworkForm frameworkEvent framework. . .

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 2 / 44

Page 3: Symfony - modern technology in practice, Webexpo Prague

Symfony 1.4

Version 1.4 = Current stableVersion 2.0 = Preview release

tomorrow Fabien Potencier 14:30 @ Development Hall

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 3 / 44

Page 4: Symfony - modern technology in practice, Webexpo Prague

Outline

1 Optimization in generalOptimizationOptimize page view speed

2 Optimization in PHPApache logsOptimization of HTTP layerXHProfApacheBenchPHP acceleratorsAPC

3 Optimization in symfony 1.4Symfony - environmentsSymfony - live demoCaching in symfony

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 4 / 44

Page 5: Symfony - modern technology in practice, Webexpo Prague

Optimize

NO HELLO WORLD BENCHMARKS!

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 5 / 44

Page 6: Symfony - modern technology in practice, Webexpo Prague

Optimize

Donald Knuth: ... Premature optimization is the root of all evil ...

... but this statement is not a license to ignore performance altogether.Knuth says: Optimize only inefficient stuff.

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 6 / 44

Page 7: Symfony - modern technology in practice, Webexpo Prague

Optimize

Measure and Control

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 7 / 44

Page 8: Symfony - modern technology in practice, Webexpo Prague

Optimize

What?User experience ?Server load ?Page view speed ?Memory usage ?Costs and Income ?...?

Page view speed

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 8 / 44

Page 9: Symfony - modern technology in practice, Webexpo Prague

1. Measure

Find appropriate tool to measure.

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 9 / 44

Page 10: Symfony - modern technology in practice, Webexpo Prague

1. Measure

Firebug + Page Speed or similar tools in other browsersapachetopapache status + ExtentedStatus Onapache-top.pyapache log analysisApacheBenchxhprof

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 10 / 44

Page 11: Symfony - modern technology in practice, Webexpo Prague

Firebug

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 11 / 44

Page 12: Symfony - modern technology in practice, Webexpo Prague

Firebug + Page Speed

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 12 / 44

Page 13: Symfony - modern technology in practice, Webexpo Prague

Apachetop for one host

Live analysis of an apache log filePro - Easy to configureCon - only one host

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 13 / 44

Page 14: Symfony - modern technology in practice, Webexpo Prague

Apache top for multi host

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 14 / 44

Page 15: Symfony - modern technology in practice, Webexpo Prague

Apache access log - basic benchmarking

Apache configuration:LogFormat "%h %l %u %t %D \"% r \ " %>s %b \"%{ Referer } i \ " \ "%{ User−Agent } i \ " " combined

Useful log value %D - time spent to serve the request in µs

Output:127.0.0.154 −− [ 1 9 / Sep/2010:08:16:51 +0200] 242517 "GET / cs / HTTP/ 1 . 1 " 200 121172 "−" " Moz i l l a / 4 . 0 ( compat ib le ; MSIE 8 . 0 ; Windows NT 6 . 0 ) "127.0.0.154 −− [ 1 9 / Sep/2010:08:16:52 +0200] 8947 "GET / s t y l e . css HTTP/ 1 . 1 " 200 56903 " h t t p : / / l o c a l h o s t / cs / " " Moz i l l a / 4 . 0 ( compat ib le ; MSIE 8 . 0 ; Windows NT 6 . 0 ; T r i den t / 4 . 0 ; SLCC1) "127.0.0.154 −− [ 1 9 / Sep/2010:08:16:52 +0200] 8204 "GET / f ron tend ( screen ) . css HTTP/ 1 . 1 " 200 464 " h t t p : / / l o c a l h o s t / cs / " " Moz i l l a / 4 . 0 ( compat ib le ; MSIE 8 . 0 ; Windows NT 6 . 0 ; T r i den t / 4 . 0 ; SLCC1) "127.0.0.154 −− [ 1 9 / Sep/2010:08:16:52 +0200] 8533 "GET / jquery . fancybox . css HTTP/ 1 . 1 " 200 5339 " h t t p : / / l o c a l h o s t / cs / " " Moz i l l a / 4 . 0 ( compat ib le ; MSIE 8 . 0 ; Windows NT 6 . 0 ; T r i den t / 4 . 0 ; SLCC1) "

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 15 / 44

Page 16: Symfony - modern technology in practice, Webexpo Prague

Apache access log - HTTP 404 / Not Found

127.0 .0 .1 −− [ . . date . . ] 9638 "GET / products / 3 4 . jpg HTTP/ 1 . 0 " 404 15161 "−" "Seznam Zbozi Robot "127 .0 .0 .1 −− [ . . date . . ] 9301 "GET / images / header . jpg HTTP/ 1 . 0 " 404 25131 "−" " Moz i l l a / 5 . 0 "127 .0 .0 .1 −− [ . . date . . ] 8563 "GET / robots . t x t HTTP/ 1 . 1 " 404 1077 "−" " msnbot / 2 . 0 b(+ h t t p : / / search .msn . com/ msnbot . htm ) "127 .0 .0 .1 −− [ . . date . . ] 8289 "GET / fav i con . i co HTTP/ 1 . 1 " 404 1081 "−" " Moz i l l a / 5 . 0 ( compat ib le ; Google Desktop /5 .9 .1005 .12335) "

All requests to nonexistent regular files are passed to a symfonycontroller! This is useless and CPU consuming process. Take care of:

favicon.ico in default location: /favicon.icorobots.txtcss and js filesimages (linked from css, user images, ...)old links - you can redirect them (sometimes) to a static 404 page...

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 16 / 44

Page 17: Symfony - modern technology in practice, Webexpo Prague

Apache error log (1)

Error log? We don’t need for performance tuning :-)

more errors ⇒ more anger ⇒ less users ⇒ performance grows!

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 17 / 44

Page 18: Symfony - modern technology in practice, Webexpo Prague

Apache error log (2)

404 errors are in apache error log too:[ . . date . . ] [ e r r o r ] [ c l i e n t 1 2 7 . 0 . 0 . 1 ]

Empty module and / or ac t i on a f t e r pars ing the URL " / media / thumbnai l / small_em−37. jpg " ( / ) .[ . . date . . ] [ e r r o r ] [ c l i e n t 1 2 7 . 0 . 0 . 1 ]

This request has been forwarded to a 404 e r r o r page by the ac t i on " page / show " .

And other errors:[ . . date . . ] [ e r r o r ] [ c l i e n t 1 2 7 . 0 . 0 . 1 ]

Act ion "comment / l i s t " does not e x i s t .[ . . date . . ] [ e r r o r ] [ c l i e n t 1 2 7 . 0 . 0 . 1 ]

Unknown record proper ty / r e l a t e d component " keywords " on "Page"[ . . date . . ] [ e r r o r ] [ c l i e n t 1 2 7 . 0 . 0 . 1 ]

PDO Connection Er ro r : SQLSTATE[08004] [1040] Too many connect ions

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 18 / 44

Page 19: Symfony - modern technology in practice, Webexpo Prague

Optimization of HTTP layer

Page viewNumber of HTTP Requests. (X)HTML document and threescore ofimages, styles and javascripts.

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 19 / 44

Page 20: Symfony - modern technology in practice, Webexpo Prague

Optimization of HTTP layer

Leverage HTTP caching for cacheble resourceExpires and Cache-Control: max-ageLast-Modified and ETag

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 20 / 44

Page 21: Symfony - modern technology in practice, Webexpo Prague

Optimization of HTTP layer

Minimize request countAvoid 404 requestsCombine javascriptsCombine CSSs

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 21 / 44

Page 22: Symfony - modern technology in practice, Webexpo Prague

Optimization of HTTP layer

Combine images using CSS spritesMinimize javascripts and CSSsGzip outputsParallelize downloads across hostnames (cookieless domain)served by statically configured apache or fast server (nginx).

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 22 / 44

Page 23: Symfony - modern technology in practice, Webexpo Prague

XHProf

XHProf1- hierarchical profiler for PHPData collection is implemented in C as a PHP extension.User interface is in PHP.

Not implemented for Windows, works on Linux/FreeBSD, expected towork on Mac OS.

1Open sourced in March 2009, originally developed at Facebook.http://pecl.php.net/package/xhprof | http://mirror.facebook.net/facebook/xhprof

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 23 / 44

Page 24: Symfony - modern technology in practice, Webexpo Prague

XHProf - Features

Reporting function-level inclusive and exclusive:

wall timesmemory usageCPU timesnumber of calls for each function

Additionally, XHProf can compare two runs and aggregate results frommultiple runs.

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 24 / 44

Page 25: Symfony - modern technology in practice, Webexpo Prague

ApacheBench

ApacheBench (ab) 2

command line tool for measuring the performance of HTTP webservers

Gives an idea about the performance of HTTP server:

requests per secondtime per request - min, mean, median, max

2bundled with Apache web serverJaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 25 / 44

Page 26: Symfony - modern technology in practice, Webexpo Prague

ApacheBench - example 1# ab −n 20 −c 1 h t t p : / / l o c a l h o s t /. . .Document Path : /Document Length : 22812 bytes

Concurrency Level : 1Time taken f o r t e s t s : 0.580 secondsComplete requests : 20To ta l t r a n s f e r r e d : 461520 bytesHTML t r a n s f e r r e d : 456240 bytesRequests per second : 34.48 [ # / sec ] (mean)Time per request : 29.000 [ms] (mean)Time per request : 29.000 [ms] (mean , across a l l concur rent requests )Trans fer ra te : 777.07 [ Kbytes / sec ] rece ived

Connection Times (ms)min mean[+/−sd ] median max

Connect : 0 0 0.0 0 0Processing : 26 29 2.2 28 35Wai t ing : 26 28 2.2 28 34To ta l : 27 29 2.2 29 35

Percentage of the requests served w i t h i n a c e r t a i n t ime (ms)50% 2966% 3075% 3080% 3090% 3495% 3598% 3599% 35

100% 35 ( longes t request )

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 26 / 44

Page 27: Symfony - modern technology in practice, Webexpo Prague

ApacheBench - example 2# ab −n 20 −c 10 h t t p : / / l o c a l h o s t /. . .Document Path : /Document Length : 22812 bytes

Concurrency Level : 10Time taken f o r t e s t s : 0.254 secondsComplete requests : 20To ta l t r a n s f e r r e d : 461520 bytesHTML t r a n s f e r r e d : 456240 bytesRequests per second : 78.86 [ # / sec ] (mean)Time per request : 126.807 [ms] (mean)Time per request : 12.681 [ms] (mean , across a l l concur rent requests )Trans fer ra te : 1777.12 [ Kbytes / sec ] rece ived

Connection Times (ms)min mean[+/−sd ] median max

Connect : 0 0 0.3 0 1Processing : 55 108 49.4 103 253Wai t ing : 54 108 49.4 103 253To ta l : 55 109 49.4 105 253

Percentage of the requests served w i t h i n a c e r t a i n t ime (ms)50% 10566% 12375% 12880% 13190% 20395% 25398% 25399% 253

100% 253 ( longes t request )

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 27 / 44

Page 28: Symfony - modern technology in practice, Webexpo Prague

PHP accelerators

PHP acceleratoris a PHP extension, improves the performance. Usually works bycaching the compiled bytecode of PHP scripts.

no parsing and compiling of PHP scripts on each requestcached bytecode is stored in shared memory, no reads from adisk

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 28 / 44

Page 29: Symfony - modern technology in practice, Webexpo Prague

PHP accelerators - list

APC1 - Alternative PHP Cache, support for PHP 5.3, built-in forPHP 6eAccelerator2 - support for PHP 5.3XCache3 - support for PHP 5.3...

1http://php.net/manual/book.apc.php2http://eaccelerator.net3http://xcache.lighttpd.net

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 29 / 44

Page 30: Symfony - modern technology in practice, Webexpo Prague

APC - Runtime configuration

Serious users should consider tuning of APC parameters, primarily:

apc.shm_size - memory allocated to APCapc.stat - check if a file has been modified

Further tuning:

apc.filters - to be cached or not to be cached (multiple regexp forfilenames)

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 30 / 44

Page 31: Symfony - modern technology in practice, Webexpo Prague

APC - Configuration for symfony

APC runtime configuration suitable for symfony projects:# Use 96MB f o r APCapc . shm_size=96

# Disable f i l e m o d i f i c a t i o n checkapc . s t a t =0

# A l l f i lenames are cached by d e f a u l tapc . cache_by_defaul t=1

# L i s t o f a comma−separated POSIX extended regu la r expressions .# I f any pa t t e rn matches the f i lename , the f i l e w i l l not be cached .apc . f i l t e r s = "−dev,−staging ,− t es t , − \ . yml$ "

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 31 / 44

Page 32: Symfony - modern technology in practice, Webexpo Prague

APC - apc.php interface - cache information

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 32 / 44

Page 33: Symfony - modern technology in practice, Webexpo Prague

APC - apc.php interface - wrong configuration

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 33 / 44

Page 34: Symfony - modern technology in practice, Webexpo Prague

APC - apc.php interface - list of cached entries

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 34 / 44

Page 35: Symfony - modern technology in practice, Webexpo Prague

APC - User Cache

APC User cacheis a key-value data store which persists between requests.It’s good for caching when used wisely.

<?phpf u n c t i o n s low_func t ion ( ) {

s leep ( 1 ) ; / / r e a l l y hard job herer e t u r n ar ray ( ’ what ’=> ’ webexpo ’ , ’ where ’=> ’ Prague ’ ) ;

}

$apc_key = ’ my_data ’ ;$data = apc_fetch ( $apc_key , $success ) ;

i f ( ! $success ) {$data = s low_func t ion ( ) ;apc_store ( $apc_key , $data ) ;

}

echo ’Welcome at ’ . $data [ ’ what ’ ] . ’ i n ’ . $data [ ’ where ’ ] ;

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 35 / 44

Page 36: Symfony - modern technology in practice, Webexpo Prague

APC with Apache web server

APC tuning with Apache web serverSome APC configuration options can be different for each VirtualHost

< V i r t u a l H o s t ∗:80>

ServerName www. webexpo . cz

# we can d isab le APC f o r t h i s domain by using value "0 "php_admin_value apc . enabled 1

# to enable f i l e m o d i f i c a t i o n check , use "1 "php_admin_value apc . s t a t 1

. . . . .

</ V i r t ua lHos t >

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 36 / 44

Page 37: Symfony - modern technology in practice, Webexpo Prague

APC is awesome!

PHP 5.3.2 1

case apc.enabled = 0 apc.enabled = 1 request timeapc.stat = 1 apc.stat = 0 with APC

1 256 ms 85 ms 81 ms 32 %2 288 ms 104 ms 100 ms 35 %

PHP 5.2.6 2

case apc.enabled = 0 apc.enabled = 1 request timeapc.stat = 1 apc.stat = 0 with APC

1 277 ms 81 ms 79 ms 29 %2 297 ms 95 ms 93 ms 31 %

1OS Debian, PHP 5.3.2-2, APC 3.1.3p12OS Debian, PHP 5.2.6-1, APC 3.0.19

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 37 / 44

Page 38: Symfony - modern technology in practice, Webexpo Prague

Symfony - environments

dev staging prodcache - + +log + + -debug + + -stats - - +

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 38 / 44

Page 39: Symfony - modern technology in practice, Webexpo Prague

Symfony - live demo

Live Demo

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 39 / 44

Page 40: Symfony - modern technology in practice, Webexpo Prague

Caching in symfony - page

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 40 / 44

Page 41: Symfony - modern technology in practice, Webexpo Prague

Caching in symfony - action

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 41 / 44

Page 42: Symfony - modern technology in practice, Webexpo Prague

Caching in symfony - partial

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 42 / 44

Page 43: Symfony - modern technology in practice, Webexpo Prague

Caching in symfony

Live Demo

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 43 / 44

Page 44: Symfony - modern technology in practice, Webexpo Prague

Thank you!

Jaroslav Bauml / [email protected] Campr / [email protected] / @PavelCampr

www.bartonstudio.cz

Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 44 / 44