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. Profiling PHP
    Applications
    Justin Carmony - Lone Star PHP `14
    @JustinCarmony

    View full-size slide

  2. About Me
    •Director of Development

    @ Deseret Digital Media

    •President of the Utah PHP Usergroup

    •Make (and Break) Web Stuff ~10 years

    View full-size slide

  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.

    View full-size slide

  4. Lets Start With a Story!

    View full-size slide

  5. You Work for an
    Awesome Tech Company

    View full-size slide

  6. Team Is Working Hard to
    Build New Things!

    View full-size slide

  7. You launch your
    awesome product!

    View full-size slide

  8. A Few More Features…

    View full-size slide

  9. … and next thing you know…

    View full-size slide

  10. Awesome Job
    Team, We Rock!

    View full-size slide

  11. We Need !
    Real-Time XYZ Feature!
    ASAP!
    $%!

    View full-size slide

  12. “I fixed it by turning

    off caching…”
    - Dev @ 80th Hour This Week

    View full-size slide

  13. “I’m sure this
    will work…”

    View full-size slide

  14. “Our servers are melting!”

    View full-size slide

  15. Let’s Learn How to Profile

    View full-size slide

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

    View full-size slide

  17. We Have Similar Jobs
    Awesome, huh?

    View full-size slide

  18. 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

    View full-size slide

  19. 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...

    View full-size slide

  20. So Lets Get Started!
    php /lets/get/started.php now

    View full-size slide

  21. First Off, Some
    PHP Myths
    and there are a lot of them!

    View full-size slide

  22. 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

    View full-size slide

  23. These

    “Performance Tips” 

    Rarely Make a Difference

    View full-size slide

  24. Thats Like Debating the Windshield Wipers’s

    Effect on this Car’s Performance

    View full-size slide

  25. Intuition Based 

    Optimizations
    i.e. how to not effectively

    improve performance
    Phrase Coined by Dave Smith

    http://thesmithfam.org/blog/2011/02/

    View full-size slide

  26. WRONG
    90% of the time
    Your Intuitions are

    View full-size slide

  27. Example:
    Fuel Efficiency Myths

    View full-size slide

  28. 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

    View full-size slide

  29. BUSTED
    They Didn’t Make a Difference,

    Or Worsened Fuel Efficiency
    All Were

    View full-size slide

  30. After Testing, Tory & Grant Used

    33% More Fuel While Driving Angry
    Driving
    Angry?
    Tory & Grant used 33% more

    fuel while driving angry.

    View full-size slide

  31. Effective Profiling
    is based on
    Actual Results

    View full-size slide

  32. Two
    “Profiling”
    Modes

    View full-size slide

  33. Normal Profiling
    Lets Try this Configuration Change

    and Measure the Performance Difference

    View full-size slide

  34. Emergency Profiling
    Aaaaaaaaaaaaah the Website is Down!!!!

    Why is it so slow??? Fix it!!!!!!

    View full-size slide

  35. 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.

    View full-size slide

  36. The Full
    Picture

    View full-size slide

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

    View full-size slide

  38. Don’t Panic!

    View full-size slide

  39. Goal: Find Bottlenecks

    View full-size slide

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

    View full-size slide

  41. Browser Dev Tools

    View full-size slide

  42. Google Page Speed
    Web Site & Browser Plugin

    View full-size slide

  43. HTTP Debugging
    Lower Level Fun Stuff

    View full-size slide

  44. WireShark
    Windows / Mac / Linux

    View full-size slide

  45. Charles

    Web Debugging Proxy
    Windows / Mac / Linux

    View full-size slide

  46. Other Tools
    • YSlow

    http://developer.yahoo.com/yslow/

    • Pingdom Tools

    http://tools.pingdom.com/

    • Windows HTTP Debugger: Fiddler2

    View full-size slide

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

    View full-size slide

  48. apachetop
    apachetop -f /var/log/apache/access.log

    View full-size slide

  49. Apachetop works
    with nginx

    View full-size slide

  50. Apache’s mod_status

    SetHandler server-status
    !
    Order Deny,Allow
    Deny from all
    Allow from 25.131.42.122

    !
    # ExtendedStatus On

    View full-size slide

  51. siege
    • Command Line Tool

    • Run Concurrent HTTP Requests

    • Runs on Linux & Mac OS

    • Windows Users: Run a VM

    • Great Way to Test End Result

    View full-size slide

  52. • 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

    View full-size slide

  53. 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

    View full-size slide

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

    View full-size slide

  55. XHProf / XHGui
    My Favorite PHP Profiler

    View full-size slide

  56. 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

    View full-size slide

  57. XHGui 1.0
    • Better GUI

    • Easy to Setup

    • Built In Sampling

    • Advanced Configuration

    • MySQL Backend
    https://github.com/preinheimer/xhprof

    View full-size slide

  58. 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

    View full-size slide

  59. Xdebug Profiler
    • Install Xdebug

    • Enable Xdebug Profiling

    • Outputs a Cachegrind Log

    • Use KCachegrind / WinCachegrind / Webgrind to view

    • MacCallGrind for $40

    View full-size slide

  60. 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)

    View full-size slide

  61. 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

    View full-size slide

  62. 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);

    View full-size slide

  63. Other Tools
    • Inclued

    http://php.net/manual/en/book.inclued.php

    • Memtrack

    http://php.net/manual/en/book.memtrack.php

    View full-size slide

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

    View full-size slide

  65. Databases
    • Typically, First Thing to Slow Down

    • Things to that will Kill the DB:

    • Missing Indexes

    • Nested Queries

    • Large Joins

    • Locked Queries

    View full-size slide

  66. Jet Profiler
    • MySQL Profiler

    • Free Version (Okay) & Paid (Awesome)

    • Not Cheap ($399)

    • But Worth It

    • Analytics Over Time

    View full-size slide

  67. Jet Profiler

    View full-size slide

  68. MySQL Commands
    • explain

    • show processlist

    • show variables

    • show status

    View full-size slide

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

    View full-size slide

  70. vmstat
    command: vmstat 1

    View full-size slide

  71. 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

    View full-size slide

  72. vnstat
    command: vnstat -l -i eth0

    View full-size slide

  73. Other Tools
    • netstat

    • iostat

    • mpstat

    • pidstat

    • (on Debian, install via sysstat package)

    View full-size slide

  74. 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

    View full-size slide

  75. Graphite & StatsD

    View full-size slide

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

    View full-size slide

  77. Wormly
    http://wormly.com/

    View full-size slide

  78. Whew, Lots of Tools
    and a lot more out there not in this talk

    Find any cool ones, let me know!

    View full-size slide

  79. Normal Profiling
    • Start with XHProf/XHGui and FireBug

    • Avoid Premature Optimization

    • Complicated Change

    • Little Reward

    • Use siege, or alternative, to simulate load.

    View full-size slide

  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

    View full-size slide

  81. Emergency Profiling
    • What Changed?

    • Increased Traffic?

    • New Feature?

    • Something Failed/Down?

    • Don’t Panic & Start Wildly Guessing

    View full-size slide

  82. Few Final
    Thoughts

    View full-size slide

  83. The Better You
    Understand
    The Problem
    The Better You Can
    Fix It

    View full-size slide

  84. Don’t
    Put Off Profiling
    Until there is an
    Emergency

    View full-size slide

  85. “Throwing Hardware”
    at it typically

    Masks the Problem

    View full-size slide

  86. Ask Others for Ideas
    !
    aka
    Brainstorming

    View full-size slide

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

    View full-size slide

  88. Thanks!
    Twitter: JustinCarmony

    !
    IRC: carmony

    #uphpu #salt #phpmentoring

    !
    Website:

    http://www.justincarmony.com/blog

    !
    Email:

    [email protected]

    View full-size slide