Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Preview:

DESCRIPTION

I gave this talk on 4/27/11 at the Boston PHP Meetup Group. It covers both server side and client side optimizations, as well as monitoring tools and techniques.

Citation preview

Web Performance, Scalability, and Testing Techniques

Boston PHP Meetup Group – April 2011Jonathan Klein

jklein@csnstores.com @jonathanklein

What We’ll Cover

• Why Listen to Me?• Why Performance Matters• Measuring Server Side Performance• Speeding up the Server• Frontend Optimization• Measuring Full Page Load Time• Homework

What We’ll Cover

• Why Listen to Me?• Why Performance Matters• Measuring Server Side Performance• Speeding up the Server• Frontend Optimization• Measuring Full Page Load Time• Homework Fun Performance Adventure!

Introduction

• Senior Software Engineer/Performance Guy at CSN Stores

• Organizer of the Boston Web Performance Meetup Group

• CSN Stores Stats:– ~1400 requests/sec for static content– ~400 requests/sec for dynamic content– ~10 million unique visitors per month– On a typical Monday we serve 75,000,000 static files

Currently converting all store code to PHP

• Windows Server FreeBSD

• IIS Lighttpd

• ~100,000 lines of ASP Classic ~50,000 lines of PHP code

• ~5 weeks away from launch

Why Do We Care about Performance?

A Faster Website Will Make You More Money

Firefox

Firefox reduced the load time of their download page by 2.2 seconds

Downloads went up 15.4%

This could drive 60 MILLION yearly downloads

Google

Injected a 400ms delay into search0.44% fewer searches/user

0.76% after 6 weeks

After delay was removed, 0.21% fewer searches

Yahoo!

400ms delay

5-9% drop in full-page traffic

Direct Relationship Between Speed and Dollars

http://www.phpied.com/the-performance-business-pitch/

Server Side Monitoring

Lots of Options:• Paid:

– Coradiant– dynaTrace– Correlsense

• http://www.real-user-monitoring.com/ - Free Version

• Free:– Access Logs– Nagios– Ganglia– Hosted WebPagetest– Selenium/dynaTrace Ajax Edition

Server Side Monitoring

<?php$start = microtime(true);

…script content…

$end = microtime(true);

do_stuff(‘Description’, $end - $start);?>

WTH is do_stuff()?

• do_stuff() can do one of:– Log to a database (not ideal)

– Write to a text file (eww)

– Make a StatsD call over UDP (good) -http://codeascraft.etsy.com/2011/02/15/measure-anything-measure-everything/

StatsD/Graphite

Be Careful What You Watch

Averages can be misleading

Better to look at percentiles

What Does a Scalability Problem Look Like?

What Does a Scalability Problem Look Like?

What Does a Scalability Problem Look Like?

Ahh, much better!

What can you do?

• Start with the Database

• Run a database trace – Filter: Queries > 50ms– Filter: Reads > 1000– Start with the worst ones and optimize

Okay, I’ll “Optimize”

• Look at execution plan– Remove calls to remote servers

Database Optimizations

• Reduce Joins• Select * from Select Foo, Bar, Baz from…• Minimize/consolidate subqueries• Add indexes where needed– We added one index: Procedure dropped from 3.5

sec & 4500 reads to .06 sec and 130 reads!• Be careful with where clauses (don’t calculate

stuff in them)

Example

SELECT id, name, salary FROM employee WHERE salary < 25000;

NOT

SELECT id, name, salary FROM employee WHERE salary + 10000 < 35000;

Example

SELECT id, name, salary FROM employee WHERE salary < 25000;

NOT

SELECT id, name, salary FROM employee WHERE salary + 10000 < 35000;

Caching

The Fastest DB Query is the One That’s Never Made

Memcached

• Caching layer between database and webserver

• Can hold PHP objects and arrays

Memcached

$m = new Memcached();$m->pconnect(‘1.2.3.4', 11211);

$m->set(‘foo’, $bar, 600);

$baz = $m->get(‘foo’);

PHP Optimizations

Install APC

• APC – Alternative PHP Cache– Opcode Cache– User Cache– Awesome!

Opcode cache

User Cache

<?php$bar = 'BAR';apc_store('foo', $bar);var_dump(apc_fetch('foo'));?>

Outputs…

string(3) "BAR"

PHP Code Optimizations

• Set the max loop value before the loop:$max = count($rows);for ($i = 0; $i < $max; $i++) {

echo $i;}

• require_once() is slow• Minimize use of define()• Yes, single quotes are slightly faster than double

quotes

PHP Code Optimizations

• Could go on and on…– http://www.wmtips.com/php/tips-optimizing-php-code.htm

• Minimal returns

• Find the hotspots in your application and fix them

Example…

• Homepage takes 5 seconds to load

• Optimize PHP…– Reduce PHP execution time by 50%!

• But wait! PHP execution was only taking 100ms– Saves you 50ms in load time– 1% of total page load

HipHop for PHP

• Built by Facebook and Open Sourced

• Compiles PHP into C++

• Currently supports PHP 5.2

• http://developers.facebook.com/blog/post/358/• https://github.com/facebook/hiphop-php

Webserver Considerations

Webserver Optimizations

• Pick the right one– Lighttpd/Nginx instead of Apache– Designed to solve the C10K problem

• Lighttpd Used By:– Youtube– Wikipedia– Meebo

Lighttpd Benefits

• Event driven model:“Unlike traditional servers, Nginx doesn't rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load.”- http://wiki.nginx.org/

• FastCGI + spawn-fcgi– PHP Process Management– Many child processes – scale out application tier.

If you are stuck on Apache…

• mod_deflate– Gzips content for faster transfer times

• mod_pagespeed– Automatic performance improvements

• KeepAlives on– Server won’t create a new connection for every

resource

Load Testing

JMeter

JMeter

JMeter

Frontend Optimization

Client side Optimization is Critical

• 80-90% of load time takes place on the client

• For mobile 97%

Best Practices

• Reduce HTTP Requests– Combine CSS, JS– Use image sprites

.classname { background: url(sprite.png) no-repeat 0 -432px;}

Best Practices

• Minify CSS/JS– Strip comments and whitespace– Automate this – YUI Compressor• http://developer.yahoo.com/yui/compressor/

• Gzip all text– HTML– CSS– JS

• Optimize Images…

Image Optimization

• For graphics use PNG8 (256 color limitation)– No more .gif (unless animated)

• JPEGs can be saved at lower quality (75%-80%)

• Smush all images

Smush Your Images! - smushit.com

JPEG Quality

100% 80%

182 KB 48 KB

Measuring Frontend Performance

How Do You Measure Load Time?

• Google Webmaster Tools• WebPagetest (www.webpagetest.org)• Yottaa.com• Firebug• YSlow• PageSpeed• Dynatrace Ajax Edition

CDN – Content Delivery Network

Lots of Options

• Amazon CloudFront• MaxCDN• Limelight• Level3• Akamai• Cotendo

Expires Headers

• Set a far future date on static resources– CSS/JS/Images

• Release new version by changing the filename

• Benefits repeat visitors and repeat page views

Google Page Speed

Firebug Net Panel

Webmaster tools

Webmaster tools

Resources

• http://www.webperformancecentral.com/wiki/WebPagetest/Optimization_Help

• http://developer.yahoo.com/performance/• http://code.google.com/speed/• High Performance Websites (Book)• Even Faster Websites (Book)

Conclusion

“Speed is the most important feature. If your application is slow, people won’t use it. I see this more with mainstream users than I do with power users...If something is slow, they’re just gone.”

- Fred Wilson (10 Golden Principles of Web Apps)

Conclusion

“Speed is the most important feature. If your application is slow, people won’t use it. I see this more with mainstream users than I do with power users...If something is slow, they’re just gone.”

- Fred Wilson (10 Golden Principles of Web Apps)

?>We’re Hiring!

www.csnstores.com/careers

Get In Touch:www.meetup.com/Web-Performance-Boston/

jklein@csnstores.com@jonathanklein