Advertisement
Advertisement

More Related Content

Advertisement

Profiling php applications

  1. Profiling PHP Applications July 2011 - Justin Carmony #uphpu wifi: C7bluffdale -- c7dcguest
  2. About Me • CTO of Dating DNA • CIO of CEVO LLC • Make Websites & iPhone Apps • Worked on Websites for Dell, nVidia, DirectTV, and other big clients.
  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. We’ll Use An Example and an awesome one at that...
  5. We Have Similar Jobs Awesome, huh?
  6. 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
  7. 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...
  8. So Lets Get Started! php /lets/get/started.php now
  9. First Off, Some PHP Myths and there are a lot of them!
  10. Common Performance Claims (Myths) • echo instead of print() • single quotes, not double quotes • echo commas, not periods • include instead of include_once • don’t use custom functions/classes • === faster than == • pass by reference • for faster than foreach instead of by value
  11. To Quote Derick Rethans Author of Xdebug...
  12. “I can only say one thing about this.... Complete & Utter Bull****”
  13. or in MythBusters Terminology....
  14. I’m Serious, There are a LOT of these Myths These Example Came from Google’s Top 10 results for “php performance tips”
  15. These “Performance Tips” Rarely Make a Difference
  16. Thats Like Debating the Windshield Wipers’s Effect on this Car’s Performance
  17. Intuition Based Optimizations i.e. how to not effectively improve performance Phrase Coined by Dave Smith http://thesmithfam.org/blog/2011/02/
  18. Your Intuitions are WRONG 90% of the time
  19. Example: Fuel Efficiency Myths
  20. 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 All Busted! They Didn’t Make a Difference, Or Worsened Fuel Efficiency
  21. How About Driving Angry? After Testing, Tory & Grant Used 33% More Fule While Driving Angry
  22. Effective Profiling is based on Actual Results
  23. There are Two Types of Profiling “Modes”
  24. Normal Profiling Lets Try this Configuration Change and Measure the Performance Difference
  25. Emergency Profiling Aaaaaaaaaaaaah the Website is Down!!!! Why is it so slow??? Fix it!!!!!!
  26. 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.
  27. Understanding the Full Picture
  28. Browser Static Files Database and/or Data Store Web Server PHP App Server OS Web Services & Resources Hardware Cache
  29. Don’t Panic!
  30. Goal: Find Slow Parts aka Bottlenecks
  31. Profiling & Testing Different Parts
  32. Tools!
  33. Browser Static Files Database and/or Data Store Web Server PHP App Server OS Web Services & Resources Hardware Cache
  34. FireBug
  35. FireBug
  36. Google Page Speed
  37. Other Tools • YSlow http://developer.yahoo.com/yslow/ • Pingdom Tools http://tools.pingdom.com/ • WireShark / Fiddler2
  38. Browser Static Files Database and/or Data Store Web Server PHP App Server OS Web Services & Resources Hardware Cache
  39. apachetop apachetop -f /var/log/apache/example.com.access.log
  40. Apachetop works with nginx
  41. 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
  42. Apache’s mod_status
  43. siege
  44. siege • Command Line Tool • Run Concurrent HTTP Requests • Runs on Linux & Mac OS • Windows Users: Run a VM • Great Way to Test End Result
  45. siege • 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
  46. siege
  47. siege
  48. Other Tools • nginx stub status (similar to mod_status) http://wiki.nginx.org/HttpStubStatusModule • Apache Bench & http_load -- CLI • JMeter -- Java Desktop App
  49. Browser Static Files Database and/or Data Store Web Server PHP App Server OS Web Services & Resources Hardware Cache
  50. My Favorite New Tools: XHProf & XHGui
  51. XHProf • Developed by Facebook • Works well for both Development & Production* Profiling • pecl extension • Decent UI for viewing Results * - Use Sampling & Special CPU Flags for Production http://mirror.facebook.net/facebook/xhprof/doc.html
  52. XHGui • Improved GUI • Easy to Setup • Built In Sampling • Advanced Configuration • MySQL Backend • I recommend using this! https://github.com/preinheimer/xhprof
  53. More Fun to Show Demo Time
  54. Xdebug Profiler • Install Xdebug • Enable Xdebug Profiling • Outputs a Cachegrind Log • Use KCachegrind / WinCachegrind / Webgrind to view • For the Rich, MacCallGrind for $150
  55. 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)
  56. Webgrind
  57. 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
  58. 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);
  59. Other Tools • Inclued http://php.net/manual/en/book.inclued.php • Memtrack http://php.net/manual/en/ book.memtrack.php
  60. Browser Static Files Database and/or Data Store Web Server PHP App Server OS Web Services & Resources Hardware Cache
  61. Databases • Typically, First Thing to Slow Down • Things to that will Kill the DB: • Missing Indexes • Nested Queries • Large Joins • Locked Queries
  62. Jet Profiler • MySQL Profiler • Free Version (Okay) & Paid (Awesome) • Not Cheap ($399) • But Worth It • Analytics Over Time
  63. Jet Profiler
  64. MySQL Commands • explain <query> • show processlist • show variables • show status
  65. mtop
  66. Browser Static Files Database and/or Data Store Web Server PHP App Server OS Web Services & Resources Hardware Cache
  67. vmstat command: vmstat 1
  68. 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
  69. vnstat command: vnstat -l -i eth0
  70. top
  71. htop
  72. strace
  73. iotop
  74. Other Tools • netstat • iostat • mpstat • pidstat • (on ubuntu, install via sysstat package)
  75. 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
  76. Monitoring Munin http://munin-monitoring.org/
  77. Monitoring Wormly http://wormly.com/
  78. Whew, Lots of Tools and a lot more out there not in this talk Find any cool ones, let me know!
  79. Normal Profiling • Start with XHProf/XHGui and FireBug • Avoid Premature Optimization • Complicated Change • Little Reward • Use siege, or alternative, to simulate load.
  80. 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
  81. Emergency Profiling • What Changed? • Increased Traffic? • New Feature? • Something Failed/Down? • Don’t Panic & Start Wildly Guessing
  82. Few Final Thoughts
  83. The Better You Understand The Problem The Better You Can Fix It
  84. Don’t Put Off Profiling Until there is an Emergency
  85. You Can Throw Hardward at the Problem, but Avoid IT
  86. Ask Others for Ideas aka Brainstorming
  87. Good Luck!
  88. Questions?
  89. Thanks! Twitter: JustinCarmony IRC: carmony #uphpu #phpc #joind.in Website: http://www.justincarmony.com/blog Email: justin@justincarmony.com

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. \n
  89. \n
  90. \n
  91. \n
  92. \n
Advertisement