+ All Categories
Home > Technology > Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Date post: 01-Nov-2014
Category:
Upload: jonathan-klein
View: 3,646 times
Download: 0 times
Share this document with a friend
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.
Popular Tags:
81
Web Performance, Scalability, and Testing Techniques Boston PHP Meetup Group – April 2011 Jonathan Klein [email protected] @jonathanklein
Transcript
Page 1: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Web Performance, Scalability, and Testing Techniques

Boston PHP Meetup Group – April 2011Jonathan Klein

[email protected] @jonathanklein

Page 2: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 3: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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!

Page 4: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 5: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 6: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Why Do We Care about Performance?

A Faster Website Will Make You More Money

Page 7: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 8: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 9: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Google

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

0.76% after 6 weeks

After delay was removed, 0.21% fewer searches

Page 10: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Yahoo!

400ms delay

5-9% drop in full-page traffic

Page 11: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Direct Relationship Between Speed and Dollars

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

Page 12: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 13: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 14: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Server Side Monitoring

<?php$start = microtime(true);

…script content…

$end = microtime(true);

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

Page 15: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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/

Page 16: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

StatsD/Graphite

Page 17: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Be Careful What You Watch

Averages can be misleading

Better to look at percentiles

Page 18: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 19: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 20: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

What Does a Scalability Problem Look Like?

Page 21: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

What Does a Scalability Problem Look Like?

Page 22: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

What Does a Scalability Problem Look Like?

Page 23: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Ahh, much better!

Page 24: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 25: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Okay, I’ll “Optimize”

• Look at execution plan– Remove calls to remote servers

Page 26: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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)

Page 27: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Example

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

NOT

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

Page 28: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Example

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

NOT

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

Page 29: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Caching

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

Page 30: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Memcached

• Caching layer between database and webserver

• Can hold PHP objects and arrays

Page 31: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Memcached

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

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

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

Page 32: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

PHP Optimizations

Page 33: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Install APC

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

Page 34: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Opcode cache

Page 35: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

User Cache

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

Outputs…

string(3) "BAR"

Page 36: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 37: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 38: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 39: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 40: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Webserver Considerations

Page 41: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Webserver Optimizations

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

• Lighttpd Used By:– Youtube– Wikipedia– Meebo

Page 42: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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.

Page 43: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 44: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Load Testing

Page 45: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

JMeter

Page 46: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

JMeter

Page 47: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

JMeter

Page 48: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 49: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Frontend Optimization

Page 50: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Client side Optimization is Critical

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

• For mobile 97%

Page 51: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Best Practices

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

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

Page 52: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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…

Page 53: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 54: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Smush Your Images! - smushit.com

Page 55: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

JPEG Quality

100% 80%

182 KB 48 KB

Page 56: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Measuring Frontend Performance

Page 57: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

How Do You Measure Load Time?

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

Page 58: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 59: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 60: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 61: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

CDN – Content Delivery Network

Page 62: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Lots of Options

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

Page 63: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 64: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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

Page 65: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Google Page Speed

Page 66: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 67: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Firebug Net Panel

Page 68: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 69: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 70: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 71: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 72: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Webmaster tools

Page 73: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

Webmaster tools

Page 74: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 75: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 76: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 77: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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)

Page 78: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Page 79: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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)

Page 80: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

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)

Page 81: Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

?>We’re Hiring!

www.csnstores.com/careers

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

[email protected]@jonathanklein


Recommended