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.

42e57550044496027f9a3a4303f13362?s=128

Justin Carmony

April 29, 2014
Tweet

Transcript

  1. Profiling PHP Applications Justin Carmony - Lone Star PHP `14

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

    of the Utah PHP Usergroup •Make (and Break) Web Stuff ~10 years
  3. 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.
  4. Lets Start With a Story!

  5. You Work for an Awesome Tech Company

  6. Team Is Working Hard to Build New Things!

  7. You launch your awesome product!

  8. A Few More Features…

  9. … and next thing you know…

  10. None
  11. Awesome Job Team, We Rock!

  12. We Need ! Real-Time XYZ Feature! ASAP! &#$%!

  13. None
  14. “I fixed it by turning
 off caching…” - Dev @

    80th Hour This Week
  15. “I’m sure this will work…”

  16. None
  17. None
  18. None
  19. “Our servers are melting!”

  20. None
  21. None
  22. None
  23. None
  24. None
  25. None
  26. Let’s Learn How to Profile

  27. We’ll Use An Example and an awesome one at that...

  28. None
  29. We Have Similar Jobs Awesome, huh?

  30. 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
  31. None
  32. 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...
  33. None
  34. So Lets Get Started! php /lets/get/started.php now

  35. First Off, Some  PHP Myths and there are a

    lot of them!
  36. 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
  37. These “Performance Tips” 
 Rarely Make a Difference

  38. Thats Like Debating the Windshield Wipers’s Effect on this Car’s

    Performance
  39. Immaterial

  40. Intuition Based 
 Optimizations i.e. how to not effectively improve

    performance Phrase Coined by Dave Smith http://thesmithfam.org/blog/2011/02/
  41. WRONG 90% of the time Your Intuitions are

  42. Example: Fuel Efficiency Myths

  43. 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
  44. BUSTED They Didn’t Make a Difference, Or Worsened Fuel Efficiency

    All Were
  45. After Testing, Tory & Grant Used 33% More Fuel While

    Driving Angry Driving Angry? Tory & Grant used 33% more fuel while driving angry.
  46. Effective Profiling is based on  Actual Results

  47. Two “Profiling” Modes

  48. Normal Profiling Lets Try this Configuration Change and Measure the

    Performance Difference
  49. Emergency Profiling Aaaaaaaaaaaaah the Website is Down!!!! Why is it

    so slow??? Fix it!!!!!!
  50. 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.
  51. The Full Picture

  52. Browser Web Server Static Files PHP App Server OS Hardware

    Web Services & Resources Cache Database Data Store
  53. Don’t Panic!

  54. Goal: Find Bottlenecks

  55. Tools!

  56. Browser Web Server Static Files PHP App Server OS Hardware

    Web Services & Cache Database Data Store
  57. Browser Dev Tools

  58. Google Page Speed Web Site & Browser Plugin

  59. None
  60. None
  61. HTTP Debugging Lower Level Fun Stuff

  62. WireShark Windows / Mac / Linux

  63. None
  64. Charles
 Web Debugging Proxy Windows / Mac / Linux

  65. None
  66. Other Tools • YSlow
 http://developer.yahoo.com/yslow/ • Pingdom Tools
 http://tools.pingdom.com/ •

    Windows HTTP Debugger: Fiddler2
  67. Browser Web Server Static Files PHP App Server OS Hardware

    Web Services & Cache Database Data Store
  68. apachetop apachetop -f /var/log/apache/access.log

  69. None
  70. Apachetop works  with nginx

  71. 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
  72. None
  73. siege

  74. siege • Command Line Tool • Run Concurrent HTTP Requests

    • Runs on Linux & Mac OS • Windows Users: Run a VM • Great Way to Test End Result
  75. • 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
  76. None
  77. None
  78. 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
  79. Browser Web Server Static Files PHP App Server OS Hardware

    Web Services & Resources Cache Database Data Store
  80. XHProf / XHGui My Favorite PHP Profiler

  81. None
  82. 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
  83. XHGui 1.0 • Better GUI • Easy to Setup •

    Built In Sampling • Advanced Configuration • MySQL Backend https://github.com/preinheimer/xhprof
  84. 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
  85. Demo Time

  86. Xdebug Profiler • Install Xdebug • Enable Xdebug Profiling •

    Outputs a Cachegrind Log • Use KCachegrind / WinCachegrind / Webgrind to view • MacCallGrind for $40
  87. Enabling Xdebug Profiling xdebug.profiler_enable=1 
 xdebug.profiler_output_dir=/tmp 
 xdebug.profiler_output_name=cachegrind.out.%p Xdebug Profiling

    Not for Production (unless you have a 100TB HDD laying around)
  88. Webgrind

  89. 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
  90. 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);
  91. Other Tools • Inclued
 http://php.net/manual/en/book.inclued.php • Memtrack
 http://php.net/manual/en/book.memtrack.php

  92. Browser Web Server Static Files PHP App Server OS Hardware

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

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

    Paid (Awesome) • Not Cheap ($399) • But Worth It • Analytics Over Time
  95. Jet Profiler

  96. MySQL Commands • explain <query> • show processlist • show

    variables • show status
  97. mtop

  98. Browser Web Server Static Files PHP App Server OS Hardware

    Web Services & Cache Database Data Store
  99. vmstat command: vmstat 1

  100. None
  101. 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
  102. vnstat command: vnstat -l -i eth0

  103. top

  104. htop

  105. strace

  106. iotop

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

    • (on Debian, install via sysstat package)
  108. 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
  109. Monitoring

  110. Graphite & StatsD

  111. Munin http://munin-monitoring.org/

  112. Wormly http://wormly.com/

  113. Whew, Lots of Tools and a lot more out there

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

    Premature Optimization • Complicated Change • Little Reward • Use siege, or alternative, to simulate load.
  115. 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
  116. Emergency Profiling • What Changed? • Increased Traffic? • New

    Feature? • Something Failed/Down? • Don’t Panic & Start Wildly Guessing
  117. Few Final Thoughts

  118. The Better You Understand The Problem The Better You Can

    Fix It
  119. Don’t  Put Off Profiling Until there is an Emergency

  120. “Throwing Hardware” at it typically Masks the Problem

  121. Ask Others for Ideas ! aka Brainstorming

  122. Good Luck!

  123. Checkout Dustin Whittle’s Talk https://speakerdeck.com/dustinwhittle/performance-testing-crash-course-1

  124. Questions?

  125. Thanks! Twitter: JustinCarmony ! IRC: carmony #uphpu #salt #phpmentoring !

    Website: http://www.justincarmony.com/blog ! Email: justin@justincarmony.com