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: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Symfony - modern technology in practice, Webexpo Prague
1. 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
2. Symfony
"Full-featured" MVC framework. ... full of buzzwords
RESTful framework
RAD framework
Form framework
Event framework
...
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 2 / 44
3. Symfony 1.4
Version 1.4 = Current stable
Version 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
4. Outline
1 Optimization in general
Optimization
Optimize page view speed
2 Optimization in PHP
Apache logs
Optimization of HTTP layer
XHProf
ApacheBench
PHP accelerators
APC
3 Optimization in symfony 1.4
Symfony - environments
Symfony - live demo
Caching in symfony
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 4 / 44
5. Optimize
NO HELLO WORLD BENCHMARKS!
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 5 / 44
6. 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
7. Optimize
Measure and Control
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 7 / 44
8. 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
9. 1. Measure
Find appropriate tool to measure.
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 9 / 44
10. 1. Measure
Firebug + Page Speed or similar tools in other browsers
apachetop
apache status + ExtentedStatus On
apache-top.py
apache log analysis
ApacheBench
xhprof
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 10 / 44
11. Firebug
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 11 / 44
12. Firebug + Page Speed
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 12 / 44
13. Apachetop for one host
Live analysis of an apache log file
Pro - Easy to configure
Con - only one host
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 13 / 44
14. Apache top for multi host
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 14 / 44
15. Apache access log - basic benchmarking
Apache configuration:
LogFormat "%h %l %u %t % "% r " %>s %b " % { R e f e r e r } i " " % { User−Agent } i " " combined
D
Useful log value %D - time spent to serve the request in µs
Output:
127.0.0.154 −− [ 1 9 / Sep / 2 0 1 0 : 0 8 : 1 6 : 5 1 +0200] 242517 "GET / cs / HTTP / 1 . 1 " 200 121172 "−" " M o z i l l a / 4 . 0
127.0.0.154 −− [ 1 9 / Sep / 2 0 1 0 : 0 8 : 1 6 : 5 2 +0200] 8947 "GET / s t y l e . css HTTP / 1 . 1 " 200 56903 " h t t p : / / l o c a
127.0.0.154 −− [ 1 9 / Sep / 2 0 1 0 : 0 8 : 1 6 : 5 2 +0200] 8204 "GET / f r o n t e n d ( screen ) . css HTTP / 1 . 1 " 200 464 " h t t
127.0.0.154 −− [ 1 9 / Sep / 2 0 1 0 : 0 8 : 1 6 : 5 2 +0200] 8533 "GET / j q u e r y . fancybox . css HTTP / 1 . 1 " 200 5339 " h t t
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 15 / 44
16. Apache access log - HTTP 404 / Not Found
127.0.0.1 −− [.. date ..] 9638 "GET / p r o d u c t s / 3 4 . j p g HTTP / 1 . 0 " 404 15161 "−" " Seznam Zbozi Robot "
127.0.0.1 −− [.. date ..] 9301 "GET / images / header . j p g HTTP / 1 . 0 " 404 25131 "−" " M o z i l l a / 5 . 0 "
127.0.0.1 −− [.. date ..] 8563 "GET / r o b o t s . t x t HTTP / 1 . 1 " 404 1077 "−" " msnbot / 2 . 0 b (+ h t t p : / / search . m
127.0.0.1 −− [.. date ..] 8289 "GET / f a v i c o n . i c o HTTP / 1 . 1 " 404 1081 "−" " M o z i l l a / 5 . 0 ( c o m p a t i b l e ; G
All requests to nonexistent regular files are passed to a symfony
controller! This is useless and CPU consuming process. Take care of:
favicon.ico in default location: /favicon.ico
robots.txt
css and js files
images (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
17. 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
18. 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 / o r a c t i o n a f t e r p a r s i n g t h e URL " / media / t h u m b n a i l / small_em −37. j p g " ( / ) .
[ . . date . . ] [ e r r o r ] [ c l i e n t 1 2 7 . 0 . 0 . 1 ]
T h i s r e q u e s t has been forwarded t o a 404 e r r o r page by t h e a c t i o n " page / show " .
And other errors:
[ . . date . . ] [ e r r o r ] [ c l i e n t 1 2 7 . 0 . 0 . 1 ]
A c t i o n " comment / l i s t " does n o t e x i s t .
[ . . date . . ] [ e r r o r ] [ c l i e n t 1 2 7 . 0 . 0 . 1 ]
Unknown r e c o r d p r o p e r t y / 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 E r r o r : SQLSTATE[ 0 8 0 0 4 ] [ 1 0 4 0 ] Too many c o n n e c t i o n s
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 18 / 44
19. Optimization of HTTP layer
Page view
Number of HTTP Requests. (X)HTML document and threescore of
images, styles and javascripts.
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 19 / 44
20. Optimization of HTTP layer
Leverage HTTP caching for cacheble resource
Expires and Cache-Control: max-age
Last-Modified and ETag
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 20 / 44
21. Optimization of HTTP layer
Minimize request count
Avoid 404 requests
Combine javascripts
Combine CSSs
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 21 / 44
22. Optimization of HTTP layer
Combine images using CSS sprites
Minimize javascripts and CSSs
Gzip outputs
Parallelize 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
23. XHProf
XHProf1 - hierarchical profiler for PHP
Data collection is implemented in C as a PHP extension.
User interface is in PHP.
Not implemented for Windows, works on Linux/FreeBSD, expected to
work on Mac OS.
1
Open 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
24. XHProf - Features
Reporting function-level inclusive and exclusive:
wall times
memory usage
CPU times
number of calls for each function
Additionally, XHProf can compare two runs and aggregate results from
multiple runs.
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 24 / 44
25. ApacheBench
ApacheBench (ab) 2
command line tool for measuring the performance of HTTP web
servers
Gives an idea about the performance of HTTP server:
requests per second
time per request - min, mean, median, max
2
bundled with Apache web server
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 25 / 44
26. 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 b y t e s
Concurrency L e v e l : 1
Time taken f o r t e s t s : 0.580 seconds
Complete r e q u e s t s : 20
Total transferred : 461520 b y t e s
HTML t r a n s f e r r e d : 456240 b y t e s
Requests per second : 34.48 [ # / sec ] ( mean )
Time per r e q u e s t : 29.000 [ ms ] ( mean )
Time per r e q u e s t : 29.000 [ ms ] ( mean , across a l l c o n c u r r e n t r e q u e s t s )
Transfer rate : 777.07 [ Kbytes / sec ] r e c e i v e d
Connection Times (ms)
min mean[+/−sd ] median max
Connect : 0 0 0.0 0 0
Processing : 26 29 2.2 28 35
Waiting : 26 28 2.2 28 34
Total : 27 29 2.2 29 35
Percentage o f t h e r e q u e s t s served w i t h i n a c e r t a i n t i m e (ms)
50% 29
66% 30
75% 30
80% 30
90% 34
95% 35
98% 35
99% 35
100% 35 ( l o n g e s t r e q u e s t )
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 26 / 44
27. 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 b y t e s
Concurrency L e v e l : 10
Time taken f o r t e s t s : 0.254 seconds
Complete r e q u e s t s : 20
Total transferred : 461520 b y t e s
HTML t r a n s f e r r e d : 456240 b y t e s
Requests per second : 78.86 [ # / sec ] ( mean )
Time per r e q u e s t : 126.807 [ ms ] ( mean )
Time per r e q u e s t : 12.681 [ ms ] ( mean , across a l l c o n c u r r e n t r e q u e s t s )
Transfer rate : 1777.12 [ Kbytes / sec ] r e c e i v e d
Connection Times (ms)
min mean[+/−sd ] median max
Connect : 0 0 0.3 0 1
Processing : 55 108 49.4 103 253
Waiting : 54 108 49.4 103 253
Total : 55 109 49.4 105 253
Percentage o f t h e r e q u e s t s served w i t h i n a c e r t a i n t i m e (ms)
50% 105
66% 123
75% 128
80% 131
90% 203
95% 253
98% 253
99% 253
100% 253 ( l o n g e s t r e q u e s t )
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 27 / 44
28. PHP accelerators
PHP accelerator
is a PHP extension, improves the performance. Usually works by
caching the compiled bytecode of PHP scripts.
no parsing and compiling of PHP scripts on each request
cached bytecode is stored in shared memory, no reads from a
disk
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 28 / 44
29. PHP accelerators - list
APC1 - Alternative PHP Cache, support for PHP 5.3, built-in for
PHP 6
eAccelerator2 - support for PHP 5.3
XCache3 - support for PHP 5.3
...
1
http://php.net/manual/book.apc.php
2
http://eaccelerator.net
3
http://xcache.lighttpd.net
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 29 / 44
30. APC - Runtime configuration
Serious users should consider tuning of APC parameters, primarily:
apc.shm_size - memory allocated to APC
apc.stat - check if a file has been modified
Further tuning:
apc.filters - to be cached or not to be cached (multiple regexp for
filenames)
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 30 / 44
31. APC - Configuration for symfony
APC runtime configuration suitable for symfony projects:
# Use 96MB f o r APC
apc . shm_size=96
# D i s a b l e f i l e m o d i f i c a t i o n check
apc . s t a t =0
# A l l f i l e n a m e s are cached by d e f a u l t
apc . c a c h e _ b y _ d e f a u l t =1
# L i s t o f a comma −separated POSIX extended r e g u l a r e x p r e s s i o n s .
# I f any p a t t e r n matches t h e f i l e n a m e , t h e f i l e w i l l n o t be cached .
apc . f i l t e r s = "−dev,−s t a g i n g ,− t e s t , − . yml$ "
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 31 / 44
32. APC - apc.php interface - cache information
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 32 / 44
33. APC - apc.php interface - wrong configuration
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 33 / 44
34. APC - apc.php interface - list of cached entries
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 34 / 44
35. APC - User Cache
APC User cache
is a key-value data store which persists between requests.
It’s good for caching when used wisely.
<?php
function slow_function ( ) {
s l e e p ( 1 ) ; / / r e a l l y hard j o b here
r e t u r n a r r a y ( ’ what ’ => ’ webexpo ’ , ’ where ’ => ’ Prague ’ ) ;
}
$apc_key = ’ my_data ’ ;
$data = a p c _ f e t c h ( $apc_key , $success ) ;
i f ( ! $success ) {
$data = s l o w _ f u n c t i o n ( ) ;
a p c _ s t o r e ( $apc_key , $data ) ;
}
echo ’ Welcome a t ’ . $data [ ’ what ’ ] . ’ i n ’ . $data [ ’ where ’ ] ;
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 35 / 44
36. APC with Apache web server
APC tuning with Apache web server
Some 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 i s a b l e APC f o r t h i s domain by u s i n g v a l u e " 0 "
php_admin_value apc . enabled 1
# t o 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 u a l H o s t >
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 36 / 44
37. APC is awesome!
PHP 5.3.2 1
case apc.enabled = 0 apc.enabled = 1 request time
apc.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 time
apc.stat = 1 apc.stat = 0 with APC
1 277 ms 81 ms 79 ms 29 %
2 297 ms 95 ms 93 ms 31 %
1
OS Debian, PHP 5.3.2-2, APC 3.1.3p1
2
OS Debian, PHP 5.2.6-1, APC 3.0.19
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 37 / 44
38. Symfony - environments
dev staging prod
cache - + +
log + + -
debug + + -
stats - - +
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 38 / 44
39. Symfony - live demo
Live Demo
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 39 / 44
40. Caching in symfony - page
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 40 / 44
41. Caching in symfony - action
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 41 / 44
42. Caching in symfony - partial
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 42 / 44
43. Caching in symfony
Live Demo
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 43 / 44
44. Thank you!
Jaroslav Bauml / bauml@bartonstudio.cz
Pavel Campr / campr@bartonstudio.cz / @PavelCampr
www.bartonstudio.cz
Jaroslav Bauml, Pavel Campr Symfony - modern technology in practice 24 Sep 2010 44 / 44