profiling php applications
DESCRIPTION
Ctvrtkon 03/10/2013TRANSCRIPT
![Page 1: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/1.jpg)
Profilování PHP aplikací
Michal Haták Čtvrtkon3.10.2013
![Page 2: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/2.jpg)
O mně
Lead developer @ INIZIO Internet Media
PHP a CoffeeScript
@Twistacz
blog.twista.cz
![Page 3: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/3.jpg)
Co je vůbec profilování ?
“Profilování je vyhledávání míst v programu, které jsou vhodné pro
optimalizaci”
![Page 4: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/4.jpg)
Cíl: nalezení pomalých částí aplikace
![Page 5: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/5.jpg)
1. krok - měření
Získání statistických údajů o běžícím programu
Co můžeme měřit ?
● Paměť● Čas● Počet a délku volání funkcí● ...
![Page 6: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/6.jpg)
2. krok – profilovací analýza
Z naměřených údajů získáváme různé statistiky
Provádíme optimalizace
![Page 7: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/7.jpg)
Profilování můžeme rozdělit na dva druhy
![Page 8: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/8.jpg)
Zkusíme změnit konfiguraci/kód a změříme rozdíl
Běžné profilování
![Page 9: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/9.jpg)
Aghhhh, Aplikace neběží!!Proč je tak pomalá? Rychle, opravit!
Kritické profilování
![Page 10: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/10.jpg)
Obvykle budete radějiv první situaci
![Page 11: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/11.jpg)
ideální je profilovat během vývoje na reálných datech
sledovat prvotní známky „pomalosti“
dát si pozor na kritická místa
![Page 12: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/12.jpg)
Aplikace
![Page 13: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/13.jpg)
Nástroje
microtime();
memory_get_usage();
memory_get_peak_usage();
![Page 14: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/14.jpg)
Nástroje
microtime();
memory_get_usage();
memory_get_peak_usage();
![Page 15: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/15.jpg)
Nástroje
xDebug
XHprof
![Page 16: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/16.jpg)
Co mají společného
extenze do PHP
shromažďují data
pomáhají nám nalézt úzká místa
![Page 17: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/17.jpg)
XHProf
open source (3/09)
production-ready (malý režijní čas)
pecl extension
vestavěný „prohlížeč“
github.com/facebook/xhprof
![Page 18: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/18.jpg)
Linux:
> sudo pecl install xhprof-beta
[xhprof]
extension=xhprof.so
xhprof.output_dir=/tmp/xhprof
> sudo service apache2 restart
Windows:
http://dev.freshsite.pl/php-extensions/xhprof.html
Instalace
![Page 19: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/19.jpg)
XHGui
open source vylepšené GUI
jednoduché nasadit
pokročilejší konfigurace
github.com/preinheimer/xhgui
![Page 20: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/20.jpg)
Instalace XHGui
git clone https://github.com/preinheimer/xhgui
cd xhgui
chmod -R 0777 cache
php install.php //composer
![Page 21: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/21.jpg)
Nasazení - XHGui
// .htaccess
php_value auto_prepend_file "way/to/xhgui/external/header.php"
nebo
// php.ini
auto_prepend_file = "way/to/xhgui/external/header.php"
![Page 22: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/22.jpg)
// Obtain the answer to life, the universe, // and your application one time out of a hundred
if (rand(0, 100) !== 42) { return;}
![Page 23: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/23.jpg)
Nasazení – bez XHGui
xhprof_enable();
...
$data = xhprof_disable();
$xhp = new XHProfRuns_Default();
$xhpId = $xhp->save_run($data, "name");
zdrojak.cz/clanky/profilovani-php-skriptu-pomoci-xhprof/
![Page 24: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/24.jpg)
Spustíme aplikaci
a hurá do XHGui
![Page 25: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/25.jpg)
Úvodní obrazovka
![Page 26: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/26.jpg)
Obrazovka běhu
![Page 27: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/27.jpg)
Přehled volaných funkcí/metod
![Page 28: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/28.jpg)
Exclusive
čas strávený přímo v těle funkce
Inclusive
čas strávený v celém "podstromu funkcí"
Co můžeme měřit
![Page 29: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/29.jpg)
![Page 30: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/30.jpg)
Callgraph
![Page 31: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/31.jpg)
graphviz
![Page 32: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/32.jpg)
![Page 33: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/33.jpg)
xDebug
víc než profiler
vylepšuje klasický error reporting (stack trace)
debuging (+ provázání s IDE)
open source
![Page 34: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/34.jpg)
Instalace
xdebug.org/wizard.php
copy & paste phpinfo();
xdebug.profiler_enable = 1
xdebug.profiler_output_dir = /tmp
xdebug.profiler_enable_trigger = 1
![Page 35: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/35.jpg)
valgrind-format
Jsou potřeba speciální programy na prohlížení záznamů
WinCacheGrind – Windowsgithub.com/jokkedk/webgrind
KCacheGrind – Linuxkcachegrind.sourceforge.net
WebGrind – Webgithub.com/jokkedk/webgrind
![Page 36: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/36.jpg)
Webgrind
![Page 37: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/37.jpg)
xDebug vs. XHProf
+ komplexnější
- problém s legacy
+ memory tracking
+ menší logy (10-15x)
+ rychlejší
![Page 38: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/38.jpg)
Pár tipů
Snažte se přiblížit produkční verzi
Reálná data
Provoz (ab, siege)
![Page 39: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/39.jpg)
Pár tipů
Neztrácejte čas mikrooptimalizacemi
v produkci není třeba logovat vše
![Page 40: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/40.jpg)
Otázky?
![Page 41: Profiling PHP Applications](https://reader034.vdocuments.mx/reader034/viewer/2022042613/54bed6924a79593b678b457b/html5/thumbnails/41.jpg)
Děkuji za pozornost!
@Twistacz
blog.twista.cz