Slide 1

Slide 1 text

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

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

Questions? Ask

Slide 14

Slide 14 text

http://speakerdeck.com/u/bastianhofmann

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Why should you care?

Slide 19

Slide 19 text

It is really important

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Every ms counts

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

So what is pagespeed?

Slide 27

Slide 27 text

Server

Slide 28

Slide 28 text

Your PHP application Request Response

Slide 29

Slide 29 text

Your PHP application Request Response Load balancer

Slide 30

Slide 30 text

Your PHP application Request Response Load balancer

Slide 31

Slide 31 text

web server db http service http service cache user request

Slide 32

Slide 32 text

But there is more

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

So what happens on your server is really important

Slide 39

Slide 39 text

But the rest is as well

Slide 40

Slide 40 text

Step 1

Slide 41

Slide 41 text

Measuring

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

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

So you need to measure at your users side

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

For older browsers you have to do it yourself

Slide 55

Slide 55 text

https://github.com/lognormal/boomerang

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

Getting it back to the server

Slide 58

Slide 58 text

Tracking request

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

logstash http://logstash.net/

Slide 61

Slide 61 text

input filter output

Slide 62

Slide 62 text

Very rich plugin system

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

Graphing it

Slide 65

Slide 65 text

Graphite http://graphite.wikidot.com/

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 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 70

Slide 70 text

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

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

Can we measure more?

Slide 73

Slide 73 text

Load balancer

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 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 77

Slide 77 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 78

Slide 78 text

From within your PHP app

Slide 79

Slide 79 text

From your JS frontend

Slide 80

Slide 80 text

Slow query logs

Slide 81

Slide 81 text

More fine grained metrics

Slide 82

Slide 82 text

By pages

Slide 83

Slide 83 text

By browser

Slide 84

Slide 84 text

By country

Slide 85

Slide 85 text

Logged in / out

Slide 86

Slide 86 text

Heavy users

Slide 87

Slide 87 text

Define goals

Slide 88

Slide 88 text

Anomaly detection

Slide 89

Slide 89 text

http://codeascraft.com/2013/06/11/introducing-kale/

Slide 90

Slide 90 text

Step 2

Slide 91

Slide 91 text

How to find out where the problems are

Slide 92

Slide 92 text

Profiling

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

Use it locally on your dev machine

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

Use it in production for a subset of requests

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

Blackfire https://blackfire.io/

Slide 107

Slide 107 text

Tideways https://tidyways.io/

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

New Relic http://newrelic.com/

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

Symfony Debug Toolbar

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

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

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

No content

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

Aggregate it

Slide 123

Slide 123 text

No content

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

Some hints on better performance

Slide 129

Slide 129 text

GZIP

Slide 130

Slide 130 text

CDNs

Slide 131

Slide 131 text

DNS provider

Slide 132

Slide 132 text

Test them

Slide 133

Slide 133 text

DB Indexes

Slide 134

Slide 134 text

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

Slide 135

Slide 135 text

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

Slide 136

Slide 136 text

Concatenate JS/ CSS/...

Slide 137

Slide 137 text

Correct caching headers

Slide 138

Slide 138 text

Opcache

Slide 139

Slide 139 text

Data Caching

Slide 140

Slide 140 text

APCU

Slide 141

Slide 141 text

memcached

Slide 142

Slide 142 text

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

Slide 143

Slide 143 text

Are you using your libraries correctly?

Slide 144

Slide 144 text

Symfony

Slide 145

Slide 145 text

Doctrine

Slide 146

Slide 146 text

DI container

Slide 147

Slide 147 text

SPDY/HTTP 2.0

Slide 148

Slide 148 text

Minimize redirects

Slide 149

Slide 149 text

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

Slide 150

Slide 150 text

Compress HTML

Slide 151

Slide 151 text

Check YSlow, Pagespeed

Slide 152

Slide 152 text

DNS prefetch

Slide 153

Slide 153 text

Slide 154

Slide 154 text

Slide 155

Slide 155 text

Move logic to async workers

Slide 156

Slide 156 text

Precompute expensive stuff

Slide 157

Slide 157 text

Varnish

Slide 158

Slide 158 text

ESI

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 Institution

Slide 161

Slide 161 text

Load content asynchronously

Slide 162

Slide 162 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu Institution

Slide 163

Slide 163 text

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

Slide 164

Slide 164 text

Flush content early

Slide 165

Slide 165 text

Move logic to shutdown handlers

Slide 166

Slide 166 text

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

Slide 167

Slide 167 text

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

Slide 168

Slide 168 text

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

Slide 169

Slide 169 text

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

Slide 170

Slide 170 text

pushState

Slide 171

Slide 171 text

Bigpipe

Slide 172

Slide 172 text

No content

Slide 173

Slide 173 text

Profile Menu Header LeftColumn RightColumn

Slide 174

Slide 174 text

No content

Slide 175

Slide 175 text

No content

Slide 176

Slide 176 text

No content

Slide 177

Slide 177 text

No content

Slide 178

Slide 178 text

Generate code

Slide 179

Slide 179 text

TSL early termination

Slide 180

Slide 180 text

Keep up to date

Slide 181

Slide 181 text

http://www.perfplanet.com/

Slide 182

Slide 182 text

Regardless of what you do: Measure it

Slide 183

Slide 183 text

Remember

Slide 184

Slide 184 text

Speed matters

Slide 185

Slide 185 text

https://joind.in/14255

Slide 186

Slide 186 text

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