profiling for grown-ups

108
Profiling for Gr!w"#p$ (107 slides to go ... )

Upload: johann-peter-hartmann

Post on 18-May-2015

5.957 views

Category:

Technology


8 download

DESCRIPTION

If you profile your applications, symfonys builtin profiler, the web debug bar and the xhprof bundle provide a lot of service to introspect your application. But what happens if you can't optimize your php code anymore? If there is no clear bottleneck inside the database or in the php code? If the system only slows down under certain, "phase of the moon" circumstances? This talk shows profiling solutions beyond sql-logs, xdebug and xhprof. It demonstrates how to get an overview of everything that happens inside your webserver or database nodes. Tools like cachegrind, callgrind, sysprof, mytop and perf are shown.

TRANSCRIPT

Page 1: Profiling for Grown-Ups

Profiling for Grownups

(107 slides to go ... )

Page 2: Profiling for Grown-Ups

Hi!

Hi Everybody! Nice to be here in berlin!

Page 3: Profiling for Grown-Ups

Let‘s wait until everybody is seated - or le, :-)

Let‘s wait until everybody is here. Actually i haven‘t enough slides, so i try to spend some time useless slides.

Page 4: Profiling for Grown-Ups

Hi, great to see You here!

A, so that‘s why we waited. Great you came, too.

Page 5: Profiling for Grown-Ups

Hey good lookin‘!I am only in software business for all the pretty people around

Page 6: Profiling for Grown-Ups

Good morning!

Who is tired?

Page 7: Profiling for Grown-Ups

Yeah, i am tired, too

Yep, me too. That‘s the bad thing about berlin. In munich - where i am from - you get enough sleep because there is no

Page 8: Profiling for Grown-Ups

Ok, let‘s start

But let‘s start anyway

Page 9: Profiling for Grown-Ups

With a short story.

I am old. And you know what old people do. They tell you war stories

Page 10: Profiling for Grown-Ups

Actually two short storys.

two war stories, actually.

Page 11: Profiling for Grown-Ups

War Story 1The first one, already some years ago.

Page 12: Profiling for Grown-Ups

Big On-Demand Video Streaming Platform

It was one of the first video on demand platforms around. Large scale, with some unique licensed live events that were only streamed using this platform.

Page 13: Profiling for Grown-Ups

Based on an enterprise ecommerce solution

To build a solid solution they decided to use an enterprise ecommerce solution as the core. So basic performance problems shouldn‘t happen, since it was already enterprise.

Page 14: Profiling for Grown-Ups

We are professionals!

The other reason it should scale was us. they wanted to have mayflower in the team to care about performance und scaling issues.

Page 15: Profiling for Grown-Ups

Server Setup

done right

So we cared about the server setup. We implemented a small testing environment, benchmarked the resource usage and calculated that we‘d need 12 4-core machines for a start.

Page 16: Profiling for Grown-Ups

Performance Testing

done right, too

When the cluster was ready. We did a classic check to see if it does scale.

Page 17: Profiling for Grown-Ups

root@local:/# ab2 -n 50000 -c 500 http://lb.ours.tld/start This is ApacheBench, Version 2.3 <$Revision: 655654 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/...Requests per second: A_LOT [#/sec] (mean)Time per request: REALLY_FAST [ms] (mean)Time per request: EVEN_FASTER [ms] (mean, across all concurrent requests)

We first run Apache-Bench. Everything looked pretty good.

Page 18: Profiling for Grown-Ups

Then we simulated user traffic using jmeter to create realistic traffic.

Page 19: Profiling for Grown-Ups

Profiling included!

So we tough: hey, how professional we are

Page 20: Profiling for Grown-Ups

Everything was fine.

Until 10 minutes before the life event.

Then everything was stuck.

Page 21: Profiling for Grown-Ups

Oops.

That wasn‘t this brilliant

Page 22: Profiling for Grown-Ups

War Story 2Second war story, not this long ago.

Page 23: Profiling for Grown-Ups

Cloud Project

100% HipsterIt was a classic hipster cloud project. Yes, there is a memcache, a rabbitmq,mogilefs, ejabberd, varnish, nginx and cloud! Uh, and a deployment pipeline, of course, all managed by puppet.

Page 24: Profiling for Grown-Ups

Internal Cloud

We started on our internal cloud - based on eucalyptus 1.6. Anybody else worked with eucalyptus? Hate it? However, we developed it there. It was fast and snappy. We were happy, and so was our customer.

Page 25: Profiling for Grown-Ups

At some day it went live, and it worked great. Just like we expected it to work. Just a bit less snappy. Responsivity wasn‘t as good.

Page 26: Profiling for Grown-Ups

Fscking Slow

Actually, it was very slow, more than 2 seconds for the start page, more than a second for every logged in page.

Page 27: Profiling for Grown-Ups

Performance Optim4ation FTW!

MemCache

APCORM-Level-Caching

Template-Caching

So we did a lot of work. There was a caching-layer. And another one to support it. And one in the ORM. And one for the database data. And so on.

Page 28: Profiling for Grown-Ups

Still sloooooow.

But it still was slow. We tried it again at our local cloud, where it was fast.

Page 29: Profiling for Grown-Ups

Test:Put it on

1 LaptopSo we took an really old Lenovo R61 Laptop, and installed everything on it. MySQL, MogileFS, RabbitMQ, eJabberD, and the whole set of production data.

Page 30: Profiling for Grown-Ups

Bl5ingly fast again.

And it was blazingly fast again. On a laptop, with 4G ram, not even a SSD was installed.

Page 31: Profiling for Grown-Ups

And we were like „What the fuck?“ This was a solid medium sized amazon ec2 installation, mostly based on large vms. and it was slower than an old laptop?

Page 32: Profiling for Grown-Ups

So, that‘s what this talk is about.

Stuff about profiling we never wanted to know.

That were the stories i wanted to tell. It‘s about the things i never wanted to know about profiling

Page 33: Profiling for Grown-Ups

Johann-Peter Hartmann:Hacker-turned-Manager

Hacker at heart.

Company: Mayflower GmbH

(founded SektionEins GmbH, too)

I am actually an old school php hacker, i even did a talk at the first php conference ever in 2000. Now i am the CTO of mayflower gmbh, we do php development. a number of php release manager has been working for our company. we love php. I founded sektioneins together with stefan esser, too. Ask me about security.

Page 34: Profiling for Grown-Ups

And this is our logo done with bacon.

And this is the mayflower logo, done with bacon! We do agile, devops, slacktime with nodecopter projects.

Page 35: Profiling for Grown-Ups

Every new important project is done with symfony 2.

We do every new, important project with symfony 2.

Page 36: Profiling for Grown-Ups

Symfony 2: I‘ve got only basic knowledge

Symfony CMF sandb7 was the official lab rat for this talk

But i don‘t know a lot about it. My colleagues do, like paul, who is going to do a short talk about fancy reactive programming symfony stutt tomorrow.

Page 37: Profiling for Grown-Ups

Zend Framework 1: a lot of knowledge

i did a lot of stuff with zend framework, though, maybe that‘s the reason i end up with a profiling talk here :-) For sure it‘s one of the reasons we do symfony now :-)

Page 38: Profiling for Grown-Ups

Profiling

Who in here uses the zend Profiler? Who uses XDebug? Who xhprof? Symfony web debug toolbar? Web Debug toolbar with xhprof? Server site profiling like Valgrind? Sysprof? oprofile?

Page 39: Profiling for Grown-Ups

Why profiling anyway?

Why are you doing it? Imho there are two major reasons:

Page 40: Profiling for Grown-Ups

To figure out how to survive the launch / update / new popularity

Reason No 1

First, you really want to survive the launch. or a new version update. or a tv spot for your website tonight.

Page 41: Profiling for Grown-Ups

Reason No 2To figure out why you did not survive the

launch / update / new popularity

Second reason: you want to know why it did not work out.

Page 42: Profiling for Grown-Ups

The first moment You are certain about your applications performance is

one day after the launch

9e ugly truth about profiling:

The bad thing about profiling is that you really, really want to know if your website stays online, all of your customers are happy and everything is up and running. Even with a television campaign. But you won‘t get a guarantee anyway, and in fact everybody knows this.

Page 43: Profiling for Grown-Ups

9ats why your b:s thinks it‘s the responsibility of the so,ware guys.

And that‘s why your boss thinks you are the one to blame. Maybe some very stupid feature just needs a lot of computation work to be done, the reason for a bad responsivity of the server is always slow code, not a slow feature.

Page 44: Profiling for Grown-Ups

9ats why you think it‘s theresponsibility of the operation guys.

And, of course, there is the operations team and the operations infrastructure. If the system slows down, it‘s their job to make it fast again. So if their hardware is not fast enough, they should get some faster iron.

Page 45: Profiling for Grown-Ups

On the other hand side, the operation guys think the developers

don‘t care about performance anyway.On the other hand site the ops guys thinks that the software developers simply write slow code. which, in a lot of cases, they actually do.

Page 46: Profiling for Grown-Ups

Strategy No 1

Make it hard to blame You.

Page 47: Profiling for Grown-Ups

Create an impressive presentation!

So we use a common, well established strategy: CYA.

Page 48: Profiling for Grown-Ups

We do 300 Req/s!

And we do a little benchmark based on apachebench. And we prove that we are able to service 300 requests in one second. How great we are!

Page 49: Profiling for Grown-Ups

No, you don‘t

Actually, we did not. You measured something that does not exist.

Page 50: Profiling for Grown-Ups

Unless somebody ...

... hacked your network

... started 50 w3m in parallel and

... pressed „reload“ 6 times a second

Page 51: Profiling for Grown-Ups

What everynbody really wants to know

Page 52: Profiling for Grown-Ups

What are the odds that everything is up and running a,er the launch? 1

Page 53: Profiling for Grown-Ups

2Is there anything we should ; before we launch?

Page 54: Profiling for Grown-Ups

3Do we know how to ; any performance issue fast?

Page 55: Profiling for Grown-Ups

1Real TestScenario

Because 1 request != 1 out of 1000 requestsFirst you need a test scenario that is close to your current or expected reality.

Page 56: Profiling for Grown-Ups

1JMeter

Virtual Users(Personas)

First: use a proper Load generation tool like jmeter, webload, silk performer or similar. Create different thread groups . Personas: In marketing and user-centered design, personas are fictional characters created to represent the different user types within a targeted demographic, attitude and/or behavior set that might use a site, brand or product in a similar way.

Page 57: Profiling for Grown-Ups

1That‘s a simple jmeter setup. Who already uses jmeter?

Page 58: Profiling for Grown-Ups

Are my tests realistic?

Page 59: Profiling for Grown-Ups

1If you‘re already online

If you already launched it‘s easy to answer. Simply take a look at your existing web traffic.

Page 60: Profiling for Grown-Ups

WebAnalytics for Validation

Google Analytics

Webalizer

Analog

AWStats

If you already know what is happening on your website: great.Just do a webalizer or analogue run on the access_log of a normal hour, and another one done with your benchmark. If the result looks similar, you are generating the right kind of traffic.

Page 61: Profiling for Grown-Ups

Correlation: 99,5%

In a few cases we did some validation using proper statistics, where we did analyzed the correlation.

Page 62: Profiling for Grown-Ups

1If you haven‘t launched yet

If you haven‘t got any data, and we are not talking about an intranet application for exactly 50 people who start working at 9 am, than you have to guess was is going to happen

Page 63: Profiling for Grown-Ups

1Create Scenarios based on Personas:

1 Worst Case Users doing all the expensive stuff

2 Expected Average „Normal“ user behavior

Since you don‘t know where you are going to end up, simply create 3 scenarios that try to show the range of your applications performance.

Page 64: Profiling for Grown-Ups

MonitoringCacti 1

Munin

JMeter-Perfmon

On the other hand side you need to figure out what happens on the server side. Maybe you already got a monitoring solution installed, like cacti or munin. Cacti anybody? Planning to change? Jmeter provides an agent based monitoring solution, it‘s part of the jmeter plugin package.

Page 65: Profiling for Grown-Ups

MuninMost of the time we use munin, since it‘s imply already there

Page 66: Profiling for Grown-Ups

source: http://www.methodsandtools.com/tools/jmeterplugins.php

You can configure perfmon to show graphs like „CPU load based on number of users“ and the like

Page 67: Profiling for Grown-Ups

Interesting Stuff

CPU User, System, IOWait, Interrupts, Context switches, Forks

Memory PageFaults, Swap, Free Memory

IO Network,Connections, Harddisk, SSD

Page 68: Profiling for Grown-Ups

A lot of consoles.

And of course a lot of consoles. To monitor everything. I am not talking about game consoles, btw. :_

Page 69: Profiling for Grown-Ups

topiotop

vmstatsysprof

Tools to do some analysis:

Top - everybody should know this one. iotop: a small python script, already part of all mayor distributionsvmstat display was originally about virtual memory statistics (hence the name), but does processes, interrupts, paging, block io and a lot of device statistics as well. Sysprof is a statistical, systemwide profiler i‘ll show later.

Page 70: Profiling for Grown-Ups

Ramp up? app/console does the cache warming!

Now test all your scenarios. you don‘t need any ramp up since your favourite framework already does the cache warmup :-)

Page 71: Profiling for Grown-Ups

Application still works

System is stable

Good service quality

P:itive outcome

Maybe there is some high load at the beginning, but maybe the systems get stable again. If everything is alright, we probably bought too much hardware.

Page 72: Profiling for Grown-Ups

Now we have a setup for realistic profiling

But that‘s cool, that‘s the perfect starting point for realistic profiling.

Page 73: Profiling for Grown-Ups

Application still works

System is stable

Good service quality

Negative outcome

But what happens if it goes wrong? if we are not able to provide the scalability or responsitivity needed?

Page 74: Profiling for Grown-Ups

Now we have a need for realistic profiling

Now we have to do some profiling anyway :-)

Page 75: Profiling for Grown-Ups

Hey, it‘s already in my IDE!

And on my Server!

Default

By default you do the profiling while development in your IDE. Who has a profiler enabled? On your local or on a close-to-production system? Lets have a closer look.

Page 76: Profiling for Grown-Ups

Single Request in jmeter: 223ms

Page 77: Profiling for Grown-Ups

Single Request in Xdebug: 1408ms

Page 78: Profiling for Grown-Ups

Everything is up to 12 times slower

But hey, it‘s relative performance!

At least that‘s what our benchmarks tell.

We did some benchmarking in different environments, and the slowdown caused by xdebug was up to 12 times. That‘s quite a big difference. Now you could say: yeah, but that is just relative.

Page 79: Profiling for Grown-Ups

20 ms database query in a 100 ms request vs

20 ms database query in a 1.200 ms request

20% vs 1.6 %The problem is, that this makes it close to impossible to find race conditions. or to measure the influence of external inout. See the example: a slow query my result in 20% of the request time, or in below 2% in yur profiling environment.

Page 80: Profiling for Grown-Ups

Your PHP profiler lies.

Page 81: Profiling for Grown-Ups

Hey, it‘s the Autoloader!

What does really happen in your application?

So, what does really happen in your application. You can see it here - xdebugs callgrind-format export. Who knows this one? Obviously most of the time is spend inside the autoloader. That‘s wellknown, i know. So, do we know everything that is happening there?

Page 82: Profiling for Grown-Ups

Wall Clock Time

We know that time was spent, but we don‘t know

how the time was spentxdebug measures wall clock time, and that is fine. but that means you only know that time was spent, not how it was spent.

Page 83: Profiling for Grown-Ups

One level deeper

Page 84: Profiling for Grown-Ups

Install valgrind and all needed debugging-symbols

start the jmeter test

run one apache child separatelyvalgrind --tool=callgrind /usr/sbin/apache2 -f /etc/apache2/apache2_single.conf -X

So let‘s have a look at valgrind / callgrind /cachegrind. It‘s nice to have all debugging symbols installed to see what‘s happening. Start a single apache child in valgrind while the jmeter test is running. Attention: this single request is a lot slower, just like xdebug.

Page 85: Profiling for Grown-Ups

Hey, it‘s the Parser!

What does really happen in your application?

Again we see what really happens - now most of the (self) time is spend inside the parser.

Page 86: Profiling for Grown-Ups

APC FTW!

Luckily both issues can be fixed with APC and the ApcUniversalClassLoader

Page 87: Profiling for Grown-Ups

One level deeper

But anyway, there is more way to go.

Page 88: Profiling for Grown-Ups

SysprofProfiling the entire lin< system, user- and kernelspace

fast profiler with low fingerprintSysprof tells you about everything that is happening inside your cpu. and, the performance impact is okeyish

Page 89: Profiling for Grown-Ups

root@local:/# sysprof-cli myoutfile

... wait some seconds to gather enough data ...

... stop with ctrl-c when you are done ...

root@local:/# sysprof myoutfile

And that‘s how it works: when the jmeter test is running, sysprof-cli is started for some time. It‘s stopped with ctrl-c and writes the data to the filesystem. this file can be viewed with sysprof itself, which is a X11 software.

Page 90: Profiling for Grown-Ups

18% of the time spent to look at the watch?!

Test run with xdebug loaded

Most of the time is spent within PHP, and that‘s expected to happe. but half of the time is spent within gettimeofday, a system call that is rather slow, especially when used in 32bit environments. so 18% of the whole cpu time is used to look at the wallclock.

Page 91: Profiling for Grown-Ups

perfSampling current calls or events every few seconds

fast profiler with low fingerprint

Page 92: Profiling for Grown-Ups

root@local:/# perf record -a -F 10000 sleep 60... creates 600.000 samples ...... saving result in perf.data ...root@local:/# perf report

Because the memory bus is slow ...

Page 93: Profiling for Grown-Ups

War Story 2Now we have got the toolset to look at our war stories.

Page 94: Profiling for Grown-Ups

Lenovo R61: fastEnterprise Cloud Cluster: slow

Page 95: Profiling for Grown-Ups

Xdebug profiling

xhprof profiling

iotop, vmstat, ...

Default profiling

Everything is fine, but still slow?

Page 96: Profiling for Grown-Ups

page_fault is the reason?!

sysprof

Reason: XEN memory sharing on am5on ec2

We saw a lot of page_faults. but there was no swapping. So this should not happen?!_very_ expensive page_faults due to memory sharing in XENSolution: memory sharing, pages marked as read_only -> page_fault and this very page_fault was slow in 32bit guests in 64bit hosts.

Page 97: Profiling for Grown-Ups

War Story 1The first one, already some years ago.

Page 98: Profiling for Grown-Ups

Everything was fine.

Until 10 minutes before the life event.

Then everything was stuck.

You remember? Where we went not only live, but dead.

Page 99: Profiling for Grown-Ups

Xdebug profiling

xhprof profiling

iotop, vmstat, ...

cacti monitoring ...

Default profiling

Everything is fine, but still stuck when it matters?

And we have had the same problem - everything looked fine in normal profiling.

Page 100: Profiling for Grown-Ups

50 terminals running top, atop, htop, iotop, mytop etc

Wait for trouble

And we waited, and when the next wave hit us we could see it in mytop: next slide

Page 101: Profiling for Grown-Ups

source: http://www.mysqlfanboy.com/2010/06/mytop/suddenly a lot of mysql queries waiting for a lock

Page 102: Profiling for Grown-Ups

smart mysql-pr7y-logging

See https://github.com/patrickallaert/MySQL-Proxy-scripts-for-devs

root@local:/# mysql-proxy -P 192.168.178.32:4400 \ --admin-username=root --admin-password=mypw \ --admin-lua-script=/usr/share/mysql-proxy/admin.lua \ -s /usr/share/mysql-proxy/debug-blind.luaab2 -n 50000 \ -c 500 http://lb.ours.tld/start 12,345678 ms select * from mytable ... 2,123456 ms insert into mytable ...

This creates a mysql proxy listener on port 4400, that forwards all request to the local server. Logging all queries with execution time, removing all unneeded whitespaces. for the _whole_ system, nod for a single process.

Page 103: Profiling for Grown-Ups

> 500 registering and > 1000 logging in usersin the same minute

==Table lockup

(Yep, solution was simple: MyISAM -> InnoDB)

Page 104: Profiling for Grown-Ups

Other war stories:

IBM kernel module screwing IO in concurrency, but hiding any iowait

In-memory-Caching layers does more bad than good

We‘ve got more war stories of this kind, mostly fixed with tools like oprofile, sysprof, vmstat and the like.

Page 105: Profiling for Grown-Ups

Why should i care?Because:

Todays system architecture is created by developers

DevOps: You are responsible for production, too

You can‘t ; a operational bug caused by application code otherwise

Now you might ask: why should i care? i am a developer, not a system administrator?Because:

Page 106: Profiling for Grown-Ups

Setup: Symfony CMF sandbox

1400 requests with concurrency 20

What‘s in for you?

A recommended setup(but not very surprising)

Page 107: Profiling for Grown-Ups

0

1250

2500

3750

5000

Low Average High

Pure PHP With APCWith Xdebug 6 APC With APC, with New RelicWith APC,XHProf

2698

new relic is too slow for production right now.

Page 108: Profiling for Grown-Ups

Thanks!Take the red pill

Profiling?

9anks!Sysprof Perf Valgrind/Callgrind Mytop Mysql-proxy Xdebug