Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Profiling php applications

7,557 views

Published on

PHP Profiling Tool

Published in: Technology
  • Great full stack of profiling tools
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Profiling php applications

  1. 1. Profiling PHP Applications July 2011 - Justin Carmony #uphpuwifi: C7bluffdale -- c7dcguest
  2. 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. 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. 4. We’ll Use An Example and an awesome one at that...
  5. 5. We Have Similar Jobs Awesome, huh?
  6. 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. 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. 8. So Lets Get Started! php /lets/get/started.php now
  9. 9. First Off, Some PHP Myths and there are a lot of them!
  10. 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. 11. To QuoteDerick Rethans Author of Xdebug...
  12. 12. “I can only say one thing about this.... Complete & Utter Bull****”
  13. 13. or in MythBusters Terminology....
  14. 14. I’m Serious, There are a LOT of these MythsThese Example Came from Google’s Top 10 results for “php performance tips”
  15. 15. These “PerformanceTips” Rarely Make a Difference
  16. 16. Thats Like Debating the Windshield Wipers’s Effect on this Car’s Performance
  17. 17. Intuition Based Optimizations i.e. how to not effectively improve performance Phrase Coined by Dave Smith http://thesmithfam.org/blog/2011/02/
  18. 18. Your Intuitions are WRONG 90% of the time
  19. 19. Example:Fuel Efficiency Myths
  20. 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. 21. How About Driving Angry? After Testing, Tory & Grant Used33% More Fule While Driving Angry
  22. 22. Effective Profiling is based on Actual Results
  23. 23. There are Two Typesof Profiling “Modes”
  24. 24. Normal Profiling Lets Try this Configuration Changeand Measure the Performance Difference
  25. 25. Emergency ProfilingAaaaaaaaaaaaah the Website is Down!!!! Why is it so slow??? Fix it!!!!!!
  26. 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. 27. Understanding the Full Picture
  28. 28. Browser Static Files Database and/or Data Store Web Server PHP App Server OSWeb Services & Resources Hardware Cache
  29. 29. Don’t Panic!
  30. 30. Goal: Find Slow Parts aka Bottlenecks
  31. 31. Profiling & Testing Different Parts
  32. 32. Tools!
  33. 33. Browser Static Files Database and/or Data Store Web Server PHP App Server OSWeb Services & Resources Hardware Cache
  34. 34. FireBug
  35. 35. FireBug
  36. 36. Google Page Speed
  37. 37. Other Tools• YSlow http://developer.yahoo.com/yslow/• Pingdom Tools http://tools.pingdom.com/• WireShark / Fiddler2
  38. 38. Browser Static Files Database and/or Data Store Web Server PHP App Server OSWeb Services & Resources Hardware Cache
  39. 39. apachetopapachetop -f /var/log/apache/example.com.access.log
  40. 40. Apachetop works with nginx
  41. 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. 42. Apache’s mod_status
  43. 43. siege
  44. 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. 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. 46. siege
  47. 47. siege
  48. 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. 49. Browser Static Files Database and/or Data Store Web Server PHP App Server OSWeb Services & Resources Hardware Cache
  50. 50. My Favorite New Tools: XHProf & XHGui
  51. 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. 52. XHGui• Improved GUI• Easy to Setup• Built In Sampling• Advanced Configuration• MySQL Backend• I recommend using this! https://github.com/preinheimer/xhprof
  53. 53. More Fun to Show Demo Time
  54. 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. 55. Enabling Xdebug Profilingxdebug.profiler_enable=1xdebug.profiler_output_dir=/tmpxdebug.profiler_output_name=cachegrind.out.%p Xdebug Profiling Not for Production (unless you have a 100TB HDD laying around)
  56. 56. Webgrind
  57. 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. 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. 59. Other Tools• Inclued http://php.net/manual/en/book.inclued.php• Memtrack http://php.net/manual/en/ book.memtrack.php
  60. 60. Browser Static Files Database and/or Data Store Web Server PHP App Server OSWeb Services & Resources Hardware Cache
  61. 61. Databases• Typically, First Thing to Slow Down• Things to that will Kill the DB: • Missing Indexes • Nested Queries • Large Joins • Locked Queries
  62. 62. Jet Profiler• MySQL Profiler• Free Version (Okay) & Paid (Awesome)• Not Cheap ($399) • But Worth It• Analytics Over Time
  63. 63. Jet Profiler
  64. 64. MySQL Commands• explain <query>• show processlist• show variables• show status
  65. 65. mtop
  66. 66. Browser Static Files Database and/or Data Store Web Server PHP App Server OSWeb Services & Resources Hardware Cache
  67. 67. vmstatcommand: vmstat 1
  68. 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. 69. vnstatcommand: vnstat -l -i eth0
  70. 70. top
  71. 71. htop
  72. 72. strace
  73. 73. iotop
  74. 74. Other Tools• netstat• iostat• mpstat• pidstat• (on ubuntu, install via sysstat package)
  75. 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. 76. Monitoring Muninhttp://munin-monitoring.org/
  77. 77. Monitoring Wormlyhttp://wormly.com/
  78. 78. Whew, Lots of Tools and a lot more out there not in this talk Find any cool ones, let me know!
  79. 79. Normal Profiling• Start with XHProf/XHGui and FireBug• Avoid Premature Optimization • Complicated Change • Little Reward• Use siege, or alternative, to simulate load.
  80. 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. 81. Emergency Profiling• What Changed? • Increased Traffic? • New Feature? • Something Failed/Down?• Don’t Panic & Start Wildly Guessing
  82. 82. Few Final Thoughts
  83. 83. The Better You Understand The ProblemThe Better You Can Fix It
  84. 84. Don’tPut Off ProfilingUntil there is anEmergency
  85. 85. You Can Throw Hardward at theProblem, but Avoid IT
  86. 86. Ask Others for Ideas akaBrainstorming
  87. 87. Good Luck!
  88. 88. Questions?
  89. 89. Thanks! Twitter: JustinCarmony IRC: carmony #uphpu #phpc #joind.in Website:http://www.justincarmony.com/blog Email: justin@justincarmony.com

×