This document provides an overview of profiling PHP applications for performance. It begins by discussing common myths about PHP optimizations that provide little real performance benefit. Effective profiling is based on measuring actual performance results using tools. The document outlines different profiling modes for normal development and emergency situations. It then describes various tools that can be used to profile different parts of a PHP application, including the browser, web server, PHP code, database, and operating system. It emphasizes finding and addressing bottlenecks. The document concludes by offering advice like avoiding premature optimization, understanding problems fully before attempting to fix them, and asking others for help.
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...
7. 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
8.
9. 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...
10.
11. So Lets Get Started!
php /lets/get/started.php now
13. 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
23. 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
24. How About
Driving Angry?
After Testing, Tory & Grant Used
33% More Fule While Driving Angry
29. 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.
44. 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
47. siege
• Command Line Tool
• Run Concurrent HTTP Requests
• Runs on Linux & Mac OS
• Windows Users: Run a VM
• Great Way to Test End Result
48. 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
54. 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
55. XHGui
• Improved GUI
• Easy to Setup
• Built In Sampling
• Advanced Configuration
• MySQL Backend
• I recommend using this!
https://github.com/preinheimer/xhprof
57. Xdebug Profiler
• Install Xdebug
• Enable Xdebug Profiling
• Outputs a Cachegrind Log
• Use KCachegrind / WinCachegrind /
Webgrind to view
• For the Rich, MacCallGrind for $150
58. 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)
60. 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
61. 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);
62. Other Tools
• Inclued
http://php.net/manual/en/book.inclued.php
• Memtrack
http://php.net/manual/en/
book.memtrack.php
63. Browser Static Files
Database and/or
Data Store
Web Server
PHP App
Server OS
Web Services &
Resources Hardware
Cache
64. Databases
• Typically, First Thing to Slow Down
• Things to that will Kill the DB:
• Missing Indexes
• Nested Queries
• Large Joins
• Locked Queries
65. Jet Profiler
• MySQL Profiler
• Free Version (Okay) & Paid (Awesome)
• Not Cheap ($399)
• But Worth It
• Analytics Over Time
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
81. Whew, Lots of Tools
and a lot more out there not in this talk
Find any cool ones, let me know!
82. Normal Profiling
• Start with XHProf/XHGui and FireBug
• Avoid Premature Optimization
• Complicated Change
• Little Reward
• Use siege, or alternative, to simulate load.
83. 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