Slide 1

Slide 1 text

Profiling PHP Applications Justin Carmony - Lone Star PHP `14 @JustinCarmony

Slide 2

Slide 2 text

About Me •Director of Development
 @ Deseret Digital Media •President of the Utah PHP Usergroup •Make (and Break) Web Stuff ~10 years

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

Lets Start With a Story!

Slide 5

Slide 5 text

You Work for an Awesome Tech Company

Slide 6

Slide 6 text

Team Is Working Hard to Build New Things!

Slide 7

Slide 7 text

You launch your awesome product!

Slide 8

Slide 8 text

A Few More Features…

Slide 9

Slide 9 text

… and next thing you know…

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Awesome Job Team, We Rock!

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

“I fixed it by turning
 off caching…” - Dev @ 80th Hour This Week

Slide 15

Slide 15 text

“I’m sure this will work…”

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

“Our servers are melting!”

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Let’s Learn How to Profile

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

We Have Similar Jobs Awesome, huh?

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

These “Performance Tips” 
 Rarely Make a Difference

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Immaterial

Slide 40

Slide 40 text

Intuition Based 
 Optimizations i.e. how to not effectively improve performance Phrase Coined by Dave Smith http://thesmithfam.org/blog/2011/02/

Slide 41

Slide 41 text

WRONG 90% of the time Your Intuitions are

Slide 42

Slide 42 text

Example: Fuel Efficiency Myths

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

BUSTED They Didn’t Make a Difference, Or Worsened Fuel Efficiency All Were

Slide 45

Slide 45 text

After Testing, Tory & Grant Used 33% More Fuel While Driving Angry Driving Angry? Tory & Grant used 33% more fuel while driving angry.

Slide 46

Slide 46 text

Effective Profiling is based on  Actual Results

Slide 47

Slide 47 text

Two “Profiling” Modes

Slide 48

Slide 48 text

Normal Profiling Lets Try this Configuration Change and Measure the Performance Difference

Slide 49

Slide 49 text

Emergency Profiling Aaaaaaaaaaaaah the Website is Down!!!! Why is it so slow??? Fix it!!!!!!

Slide 50

Slide 50 text

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.

Slide 51

Slide 51 text

The Full Picture

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

Don’t Panic!

Slide 54

Slide 54 text

Goal: Find Bottlenecks

Slide 55

Slide 55 text

Tools!

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Browser Dev Tools

Slide 58

Slide 58 text

Google Page Speed Web Site & Browser Plugin

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

HTTP Debugging Lower Level Fun Stuff

Slide 62

Slide 62 text

WireShark Windows / Mac / Linux

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Charles
 Web Debugging Proxy Windows / Mac / Linux

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

Other Tools • YSlow
 http://developer.yahoo.com/yslow/ • Pingdom Tools
 http://tools.pingdom.com/ • Windows HTTP Debugger: Fiddler2

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Apachetop works  with nginx

Slide 71

Slide 71 text

Apache’s mod_status SetHandler server-status ! Order Deny,Allow Deny from all Allow from 25.131.42.122 ! # ExtendedStatus On

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

siege

Slide 74

Slide 74 text

siege • Command Line Tool • Run Concurrent HTTP Requests • Runs on Linux & Mac OS • Windows Users: Run a VM • Great Way to Test End Result

Slide 75

Slide 75 text

• 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

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

XHProf / XHGui My Favorite PHP Profiler

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

XHGui 1.0 • Better GUI • Easy to Setup • Built In Sampling • Advanced Configuration • MySQL Backend https://github.com/preinheimer/xhprof

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

Demo Time

Slide 86

Slide 86 text

Xdebug Profiler • Install Xdebug • Enable Xdebug Profiling • Outputs a Cachegrind Log • Use KCachegrind / WinCachegrind / Webgrind to view • MacCallGrind for $40

Slide 87

Slide 87 text

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)

Slide 88

Slide 88 text

Webgrind

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

Other Tools • Inclued
 http://php.net/manual/en/book.inclued.php • Memtrack
 http://php.net/manual/en/book.memtrack.php

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

Databases • Typically, First Thing to Slow Down • Things to that will Kill the DB: • Missing Indexes • Nested Queries • Large Joins • Locked Queries

Slide 94

Slide 94 text

Jet Profiler • MySQL Profiler • Free Version (Okay) & Paid (Awesome) • Not Cheap ($399) • But Worth It • Analytics Over Time

Slide 95

Slide 95 text

Jet Profiler

Slide 96

Slide 96 text

MySQL Commands • explain • show processlist • show variables • show status

Slide 97

Slide 97 text

mtop

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

vmstat command: vmstat 1

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

vnstat command: vnstat -l -i eth0

Slide 103

Slide 103 text

top

Slide 104

Slide 104 text

htop

Slide 105

Slide 105 text

strace

Slide 106

Slide 106 text

iotop

Slide 107

Slide 107 text

Other Tools • netstat • iostat • mpstat • pidstat • (on Debian, install via sysstat package)

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

Monitoring

Slide 110

Slide 110 text

Graphite & StatsD

Slide 111

Slide 111 text

Munin http://munin-monitoring.org/

Slide 112

Slide 112 text

Wormly http://wormly.com/

Slide 113

Slide 113 text

Whew, Lots of Tools and a lot more out there not in this talk Find any cool ones, let me know!

Slide 114

Slide 114 text

Normal Profiling • Start with XHProf/XHGui and FireBug • Avoid Premature Optimization • Complicated Change • Little Reward • Use siege, or alternative, to simulate load.

Slide 115

Slide 115 text

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

Slide 116

Slide 116 text

Emergency Profiling • What Changed? • Increased Traffic? • New Feature? • Something Failed/Down? • Don’t Panic & Start Wildly Guessing

Slide 117

Slide 117 text

Few Final Thoughts

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

Don’t  Put Off Profiling Until there is an Emergency

Slide 120

Slide 120 text

“Throwing Hardware” at it typically Masks the Problem

Slide 121

Slide 121 text

Ask Others for Ideas ! aka Brainstorming

Slide 122

Slide 122 text

Good Luck!

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

Questions?

Slide 125

Slide 125 text

Thanks! Twitter: JustinCarmony ! IRC: carmony #uphpu #salt #phpmentoring ! Website: http://www.justincarmony.com/blog ! Email: [email protected]