Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Top to Bottom PHP Profiling

Top to Bottom PHP Profiling

PHP applications are becoming more complex than ever before. We've moved away from the simple LAMP stack to having many different layers and pieces that our application interacts with. There are many places where things can slow down or break. We'll discuss the different parts of a PHP application including the browser, php application, database, and the server. We'll show different tools for each part and how to use them. By the end of this presentation you should have a whole arsenal of tools to help you profile & understand your application much better.

Justin Carmony

April 29, 2014
Tweet

More Decks by Justin Carmony

Other Decks in Technology

Transcript

  1. About Me •Director of Development
 @ Deseret Digital Media •President

    of the Utah PHP Usergroup •Make (and Break) Web Stuff ~10 years
  2. Experience •Lots of work with “Middle-Scale” websites. •Lucky to Work

    on Fun Projects •Lots of Web Service Stuff •Learned most of this from other really smart people.
  3. MythBusters • They get a Myth • Break it down

    to its different parts • They Conduct a Bunch of Tests • They Draw Conclusions • and have a Fun Time
  4. Developers • We get Myths
 (Hey, this is slow) •

    We Break it down to the different pieces • We Conduct a Bunch of Tests. • We Draw Conclusions
 (and normally make changes) • and Have a Fun Time...
  5. Common Performance 
 Claims (Myths) • echo instead of print()

    • echo commas, not periods • don’t use custom functions/ classes • pass by reference instead of by value • single quotes, not double quotes • include instead of include_once • === faster than == • for faster than foreach Busted
  6. Intuition Based 
 Optimizations i.e. how to not effectively improve

    performance Phrase Coined by Dave Smith http://thesmithfam.org/blog/2011/02/
  7. MythBusters Tested • Keeping Your AC Off vs Windows Down

    • Idling Better than Stop/Start • Magical Aerodynamics • Dirt-Free Filters • Special Fuel Additives to Slow Burn
  8. After Testing, Tory & Grant Used 33% More Fuel While

    Driving Angry Driving Angry? Tory & Grant used 33% more fuel while driving angry.
  9. Same Techniques  Apply to Both • Some you’ll want

    to chose first depending on your situation. • You’ll want to be careful when profiling in Production, you can make things worse.
  10. Browser Web Server Static Files PHP App Server OS Hardware

    Web Services & Resources Cache Database Data Store
  11. Browser Web Server Static Files PHP App Server OS Hardware

    Web Services & Cache Database Data Store
  12. Browser Web Server Static Files PHP App Server OS Hardware

    Web Services & Cache Database Data Store
  13. Apache’s mod_status <Location /server-status> SetHandler server-status ! Order Deny,Allow Deny

    from all Allow from 25.131.42.122 </Location> ! # ExtendedStatus On
  14. siege • Command Line Tool • Run Concurrent HTTP Requests

    • Runs on Linux & Mac OS • Windows Users: Run a VM • Great Way to Test End Result
  15. • Create txt file with lists of URLs to hit

    • Run Command:
 siege -c 10 -r 10 -f urls.txt • c = concurrent
 r = # of requests
 f = path to URL file siege
  16. Other Tools • nginx stub status (similar to mod_status)
 http://wiki.nginx.org/HttpStubStatusModule

    • Apache Bench & http_load -- CLI • DDM crawlr
 https://github.com/deseretdigital/crawlr • JMeter -- Java Desktop App
  17. Browser Web Server Static Files PHP App Server OS Hardware

    Web Services & Resources Cache Database Data Store
  18. XHProf • Developed by Facebook • Works well for both

    Development 
 & Production* Profiling • pecl extension • Decent UI for viewing Results http://mirror.facebook.net/facebook/xhprof/doc.html * - Use Sampling & Special CPU Flags for Production
  19. XHGui 1.0 • Better GUI • Easy to Setup •

    Built In Sampling • Advanced Configuration • MySQL Backend https://github.com/preinheimer/xhprof
  20. XHGui 2.0 • Rewrite of XHGUI 1.0 • MongoDB Backend

    • Better UI • Use this unless Mongo won’t work in your setup. • I recommend using this! https://github.com/perftools/xhgui
  21. Xdebug Profiler • Install Xdebug • Enable Xdebug Profiling •

    Outputs a Cachegrind Log • Use KCachegrind / WinCachegrind / Webgrind to view • MacCallGrind for $40
  22. Timing Points • Most Frameworks have Built-In Profiling / Timing

    Points • Most ORMs have them as well • You can do them yourself • A must for Database Queries
  23. Timing Points $start = microtime(true); // true to get as

    float ! /* Do your Crazy Code, i.e. query */ ! $end = microtime(true); ! $time = round($end - $start, 4);
  24. Browser Web Server Static Files PHP App Server OS Hardware

    Web Services & Cache Database Data Store
  25. Databases • Typically, First Thing to Slow Down • Things

    to that will Kill the DB: • Missing Indexes • Nested Queries • Large Joins • Locked Queries
  26. Jet Profiler • MySQL Profiler • Free Version (Okay) &

    Paid (Awesome) • Not Cheap ($399) • But Worth It • Analytics Over Time
  27. Browser Web Server Static Files PHP App Server OS Hardware

    Web Services & Cache Database Data Store
  28. vmstat • swap > 0 means swapping
 Memory Issue •

    cpu sys + us really high
 CPU / Code / PHP Problem • cpu wa > 10
 Disk IO Problem* * - Technically could be Network IO as well, but typically one of the last and more rare bottlenecks to hit
  29. top

  30. Other Tools • netstat • iostat • mpstat • pidstat

    • (on Debian, install via sysstat package)
  31. Other Tools • grep, awk, sed • IPs Connected:
 netstat

    -plan | grep :80 | awk '{print $5}' | sed 's/::ffff://g' | awk -F: '{print $1}' | sort | uniq -c | sort -n
  32. Whew, Lots of Tools and a lot more out there

    not in this talk Find any cool ones, let me know!
  33. Normal Profiling • Start with XHProf/XHGui and FireBug • Avoid

    Premature Optimization • Complicated Change • Little Reward • Use siege, or alternative, to simulate load.
  34. Emergency Profiling • Start with OS Level Testing Tools (htop,

    vmstat, vnstat) to check Server Performance • Determine which Resource(s) are being over utilized • Finding the bottleneck is key
  35. Emergency Profiling • What Changed? • Increased Traffic? • New

    Feature? • Something Failed/Down? • Don’t Panic & Start Wildly Guessing