Slide 1

Slide 1 text

My site is slow, what can I do? @BastianHofmann Profiling PHP applications

Slide 2

Slide 2 text

This talk is all about...

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Speed of your web application

Slide 5

Slide 5 text

We'll talk about...

Slide 6

Slide 6 text

Why it matters

Slide 7

Slide 7 text

How to measure it

Slide 8

Slide 8 text

How to find out where the problems are

Slide 9

Slide 9 text

Before we start

Slide 10

Slide 10 text

A few words about me

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Questions? Ask

Slide 18

Slide 18 text

http://speakerdeck.com/u/bastianhofmann

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Why should you care?

Slide 21

Slide 21 text

It is really important

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Every ms counts

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

So what is pagespeed?

Slide 29

Slide 29 text

Server

Slide 30

Slide 30 text

Your PHP application Request Response

Slide 31

Slide 31 text

Your PHP application Request Response Load balancer

Slide 32

Slide 32 text

Your PHP application Request Response Load balancer

Slide 33

Slide 33 text

web server db http service http service cache user request

Slide 34

Slide 34 text

But there is more

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

http://www.stevesouders.com/blog/2012/02/10/the-performance-golden-rule/

Slide 40

Slide 40 text

So what happens on your server is really important

Slide 41

Slide 41 text

But the rest is as well

Slide 42

Slide 42 text

Step 1

Slide 43

Slide 43 text

Measuring

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

So you need to measure at your users side

Slide 52

Slide 52 text

http://www.soasta.com/

Slide 53

Slide 53 text

https://www.librato.com

Slide 54

Slide 54 text

Navigation Timing API https://developer.mozilla.org/en-US/docs/ Navigation_timing

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Resource Timing API http://www.w3.org/TR/resource-timing/

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

Getting it back to the server

Slide 59

Slide 59 text

Tracking request

Slide 60

Slide 60 text

https://tracking.example.com/? page=profile&backend=123&co mplete=890&domReady=580

Slide 61

Slide 61 text

logstash http://logstash.net/

Slide 62

Slide 62 text

input filter output

Slide 63

Slide 63 text

Very rich plugin system

Slide 64

Slide 64 text

browser JS: boomerang logstash trackingServer access log requests tracking image with timing information as query parameters

Slide 65

Slide 65 text

Graphing it

Slide 66

Slide 66 text

Graphite http://graphite.wikidot.com/

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

browser JS: boomerang logstash trackingServer access log requests tracking image with timing information as query parameters graphite statsd

Slide 69

Slide 69 text

input {
 file {
 type => "pagespeed-access"
 path => [ "/var/log/nginx/ access_log/monitoring-access.log" ]
 }
 }

Slide 70

Slide 70 text

filter{
 grok {
 type => "pagespeed-access"
 pattern => "^.*\s\"[A-Z]+\s[^\?\s]+ \?page=%{DATA:page}\&connectTime=% {NUMBER:connectTime}...)?\sHTTP\/\d\.\d\".* $"
 }
 grok {
 type => "pagespeed-access"
 match => ["page", "^(profile| home|...)\.logged(In|Out)$"]
 exclude_tags => ["_grokparsefailure"]
 }
 }

Slide 71

Slide 71 text

output { statsd { type => "pagespeed-access" exclude_tags => ["_grokparsefailure"] host => "localhost" port => 8126 namespace => "pagespeed" sender => "" timing => [ "%{page}.connect", "% {connectTime}", ... ] } }

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Can we measure more?

Slide 75

Slide 75 text

From within your PHP app

Slide 76

Slide 76 text

DB queries

Slide 77

Slide 77 text

HTTP calls

Slide 78

Slide 78 text

Rendering time

Slide 79

Slide 79 text

Slide 80

Slide 80 text

More fine grained metrics

Slide 81

Slide 81 text

By pages

Slide 82

Slide 82 text

By browser

Slide 83

Slide 83 text

By country

Slide 84

Slide 84 text

Logged in / out

Slide 85

Slide 85 text

Heavy users

Slide 86

Slide 86 text

Slow query logs

Slide 87

Slide 87 text

Load balancers

Slide 88

Slide 88 text

web server db http service http service cache user request

Slide 89

Slide 89 text

input {
 file {
 type => "haproxy-http-log"
 path => [ "/var/log/ haproxy-http.log*" ]
 }
 }

Slide 90

Slide 90 text

filter {
 grok {
 type => "haproxy-http-log"
 pattern => "%{HAPROXYHTTP}" 
 }
 mutate {
 type => "haproxy-http-log"
 gsub => [
 "server_name", "\.", "_",
 "client_ip", "\.", "_"
 ]
 }
 }

Slide 91

Slide 91 text

output {
 statsd {
 type => "haproxy-http-log"
 exclude_tags => ["_grokparsefailure"]
 host => "localhost"
 port => 8125
 namespace => "lb"
 sender => ""
 increment => [
 "haproxy.%{backend_name}.%{server_name}.% {client_ip}.hits",
 "haproxy.%{backend_name}.%{server_name}.% {client_ip}.responses.%{http_status_code}"
 ]
 timing => [
 "haproxy.%{backend_name}.%{server_name}.% {client_ip}.time_request", "%{time_request}",
 "haproxy.%{backend_name}.%{server_name}.% {client_ip}.time_backend_connect", "%{time_backend_connect}",
 "haproxy.%{backend_name}.%{server_name}.% {client_ip}.time_backend_response", "%{time_backend_response}",
 "haproxy.%{backend_name}.%{server_name}.% {client_ip}.time_queue", "%{time_queue}",
 "haproxy.%{backend_name}.%{server_name}.% {client_ip}.time_duration", "%{time_duration}"
 ]
 }


Slide 92

Slide 92 text

browser JS: boomerang logstash trackingServer access log requests tracking image with timing information as query parameters graphite statsd logstash load balancer access log logstash service access log

Slide 93

Slide 93 text

Define goals

Slide 94

Slide 94 text

Step 2

Slide 95

Slide 95 text

How to find out where the problems are

Slide 96

Slide 96 text

Profiling

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

xdebug.profiler_enable_trigger = 1 http://url?XDEBUG_PROFILE

Slide 99

Slide 99 text

Webgrind https://github.com/jokkedk/webgrind

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

Use it locally on your dev machine

Slide 102

Slide 102 text

XHProf https://github.com/facebook/xhprof

Slide 103

Slide 103 text

Use it in production for a subset of requests

Slide 104

Slide 104 text

XHGUI https://github.com/perftools/xhgui

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

php-meminfo https://github.com/BitOne/php-meminfo

Slide 110

Slide 110 text

Symfony Debug Toolbar

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

Extend it http://symfony.com/doc/current/cookbook/profiler/ data_collector.html

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

Use it in production for a subset of requests

Slide 121

Slide 121 text

https://qafoolabs.com/

Slide 122

Slide 122 text

https://blackfire.io/

Slide 123

Slide 123 text

http://newrelic.com/

Slide 124

Slide 124 text

Step 3

Slide 125

Slide 125 text

Fix it

Slide 126

Slide 126 text

That's something you have to do

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

Keep up to date

Slide 129

Slide 129 text

http://www.perfplanet.com/

Slide 130

Slide 130 text

Some hints on better performance

Slide 131

Slide 131 text

The obvious

Slide 132

Slide 132 text

GZIP

Slide 133

Slide 133 text

CDNs

Slide 134

Slide 134 text

DB Indexes

Slide 135

Slide 135 text

Minify JS https://github.com/mishoo/UglifyJS

Slide 136

Slide 136 text

Minify CSS http://sass-lang.com/

Slide 137

Slide 137 text

Concatenate JS/ CSS/...

Slide 138

Slide 138 text

Correct caching headers

Slide 139

Slide 139 text

Opcache

Slide 140

Slide 140 text

Data Caching

Slide 141

Slide 141 text

APCU

Slide 142

Slide 142 text

memcached

Slide 143

Slide 143 text

PHP 5.6 https://blog.asmallorange.com/2013/08/php-roadmap- performance/

Slide 144

Slide 144 text

The not so well known

Slide 145

Slide 145 text

SPDY/ HTTP2

Slide 146

Slide 146 text

Minimize redirects

Slide 147

Slide 147 text

Check image compression https://github.com/gruntjs/grunt-contrib-imagemin

Slide 148

Slide 148 text

Compress HTML

Slide 149

Slide 149 text

Check YSlow, Pagespeed

Slide 150

Slide 150 text

DNS prefetch

Slide 151

Slide 151 text

Slide 152

Slide 152 text

Move logic to async workers

Slide 153

Slide 153 text

The "crazy" stuff

Slide 154

Slide 154 text

Varnish

Slide 155

Slide 155 text

ESI

Slide 156

Slide 156 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu Institution

Slide 157

Slide 157 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu Institution

Slide 158

Slide 158 text

Load content asynchronously

Slide 159

Slide 159 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu Institution

Slide 160

Slide 160 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu
loadWidget('/aboutMe', function(w) { w.render({ replace : '#placeholder' }); }) Institution

Slide 161

Slide 161 text

Cache ajax requests

Slide 162

Slide 162 text

Flush content early

Slide 163

Slide 163 text

Move logic to shutdown handlers

Slide 164

Slide 164 text

http://www.php.net/manual/en/function.register-shutdown- function.php

Slide 165

Slide 165 text

http://www.php.net/manual/en/function.fastcgi-finish- request.php

Slide 166

Slide 166 text

Promises / Futures https://github.com/facebook/libphutil

Slide 167

Slide 167 text

$future  =  new  HTTPFuture(       'http://www.example.com/'   );   list($status,  $body,  $headers)  =       $future-­‐>resolve();

Slide 168

Slide 168 text

pushState

Slide 169

Slide 169 text

Bigpipe

Slide 170

Slide 170 text

No content

Slide 171

Slide 171 text

Profile Menu Header LeftColumn RightColumn

Slide 172

Slide 172 text

No content

Slide 173

Slide 173 text

No content

Slide 174

Slide 174 text

No content

Slide 175

Slide 175 text

No content

Slide 176

Slide 176 text

Remember

Slide 177

Slide 177 text

Speed matters

Slide 178

Slide 178 text

http://twitter.com/BastianHofmann http://lanyrd.com/people/BastianHofmann http://speakerdeck.com/u/bastianhofmann [email protected]