common optimization mistakes - php quebec 2009

34
1 Friday, March 6, 2009

Upload: kaplumbaga

Post on 30-May-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 1/34

1Friday, March 6, 2009

Page 2: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 2/34

PrematureOptimization

=

Solve the business case,

before optimizing thesolution

2Friday, March 6, 2009

Page 3: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 3/34

Don’t Over Engineer 

• Understand your audience

• Estimate the scale and growth of your

application (based on facts, not

marketing fiction)

• Keep timelines in mind when setting

the project scope

3Friday, March 6, 2009

Page 4: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 4/34

Simplify, Simplify &

Simplify!• Break complex tasks into simpler sub-

components

• Don’t be afraid to modularize the code

• More code does not translate to slower

code (common misconception)PHP has grown from less than 1 million LOC to over 2 million LOCsince 2000 and has become at least 4 times faster.

Linux kernel code base increase by 40% since 2005 and stillmanaged to improve performance by roughly the same margin.

LOC stats came from ohloh.net

4Friday, March 6, 2009

Page 5: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 5/34

Hardware is Cheaper!

VS

In most cases applications can gain vastperformance gains by improving hardware,quickly rather than slow, error prone code

optimization efforts.5Friday, March 6, 2009

Page 6: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 6/34

Hardware

• CPU bottlenecks can be resolved by

more cores and/or CPUs. Typically

each year yields 20-30% speedimprovements over past year’s CPU

speeds.

• Ability to handle large amounts of 

traffic is often hampered by limited

6Friday, March 6, 2009

Page 7: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 7/34

Hardware

• Drives are often the

most common

 bottleneck,fortunately between

RAID and Solid State

you can solve thatpretty easily now a

days.

7Friday, March 6, 2009

Page 8: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 8/34

Hardware Caveat

• While quick to give results, in some

situations it will not help for long:

• Database saturation

• Non-scalable code base

• Network bound bottleneck • Extremely low sessions - per - server

ratio

8Friday, March 6, 2009

Page 9: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 9/34

Optimize, but don’t

touch the code• Typically introduces substantial

efficiencies

• Does not endanger code integrity

• Usually simple and quick to deploy• In the event of problems, often simple

to revert

9Friday, March 6, 2009

Page 10: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 10/34

• This cycle happensfor every include

file, not just for the"main" script.

• Compilation caneasily consumemore time thanexecution.

  i n c  l u d

 e / r e q 

 u  i r e

methodfunction

call10Friday, March 6, 2009

Page 11: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 11/34

Compiler/Opcode Cache

• Each PHP script is compiled only once for each

revision.

• Reduced File IO, opcodes are being read frommemory instead of being parsed from disk.

• Opcodes can optimized for faster execution.

• Yields a minimum 20-30% speed improvementand often as much as 200-300%

11Friday, March 6, 2009

Page 12: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 12/34

Quick Comparison

0

50

100

150

200

FUDforumSmarty

phpMyAdmin

Stock PHP

APC

PHP Accelerator 

eAccelerator 

Zend Platform

12Friday, March 6, 2009

Page 13: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 13/34

Use In-Memory Caches

• In-memory session storage is MUCH

faster than disk or database equivalents.

• Very simple via memcache extension

Also allows scaling across multipleservers for improved reliability and

performance.

session.save_handler = “memcache”

session.save_path = “tcp://localhost:11211”

13Friday, March 6, 2009

Page 14: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 14/34

Everything has to be

Real-time

14Friday, March 6, 2009

Page 15: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 15/34

Complete Page Caching

• Squid Proxy

• Page pre-generation

• On-demand caching

15Friday, March 6, 2009

Page 16: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 16/34

Partial Cache - SQL

• In most applications the primary

 bottleneck can often be traced to

“database work”.

• Caching of SQL can drastically reducethe load caused by unavoidable,

complex queries.

16Friday, March 6, 2009

Page 17: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 17/34

SQL Caching Example

$key = md5(“some sort of sql query”);

if (!($result = memcache_get($key))) {$result = $pdo->query($qry)->fetchAll();

  // cache query result for 1 hour 

memcache_set($key, $result, NULL, 3600);}

17Friday, March 6, 2009

Page 18: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 18/34

Partial Cache - Code

• Rather than optimizing complex PHP

operations, it is often better to eliminate

them entire via the use of cache.

• Faster payoff 

• Lower chance of code breakage

• More speed than code optimization

18Friday, March 6, 2009

Page 19: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 19/34

Code Caching Examplefunction complex_function_abc($a, $b, $c) {

$key = __FUNCTION__ .serialize(func_get_args());

if (!($result = memcache_get($key))) {$result = // function code // cache query result for 1 hour 

memcache_set($key, $result, NULL, 3600);}return $result;

}

19Friday, March 6, 2009

Page 20: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 20/34

Database before code

• One of the most common mistakes

people make is optimizing code before

even looking at the database.

• Vast majority of applications have the bottleneck in the database not the code!

20Friday, March 6, 2009

Page 21: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 21/34

Compile your environment

• Distribution binaries suck!

• More often than not you can realize

10-15% speed increase by compiling

your own Apache/PHP/Database fromsource. (unless you are using Gentoo)

21Friday, March 6, 2009

Page 22: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 22/34

Output Buffering

• Don’t fear output buffering because it

uses ram, ram is cheap. IO, not so

much.

22Friday, March 6, 2009

Page 23: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 23/34

23

• The goal is to pass off as much work to the kernelas efficiently as possible.

• Optimizes PHP to OS Communication

• Reduces Number Of System Calls

Matching Your IO Sizes

23Friday, March 6, 2009

Page 24: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 24/34

24

• Efficient

• Flexible

• In your script, with ob_start()

• Everywhere, with output_buffering = On

• Improves browser’s rendering speed

PHP: Output Control

24Friday, March 6, 2009

Page 25: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 25/34

Apache: Output Control

• The idea is to hand off entire page to the

kernel without blocking.

• Set SendBufferSize = PageSize

25Friday, March 6, 2009

Page 26: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 26/34

Page 27: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 27/34

Don’t  Assume

• One of the most

common mistakes done

even by experienced

developers is starting to

optimize code without

identifying theproblem.

Assume nothing,

profile everything!

27Friday, March 6, 2009

Page 28: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 28/34

Profile, Profile & Profile

• Xdebug and APD extensions provide a

very helpful mechanism for identifying

TRUE bottlenecks in your code.

28Friday, March 6, 2009

Page 29: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 29/34

Kcachegrind

Xdebug provides kcachegrind analyzable output that offers

an easy visual overview of your performance problems29Friday, March 6, 2009

Page 30: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 30/34

Micro Optimization

• Takes a long time

• Won’t solve your performance issues

• Almost guaranteed to break something

• Cost > Reward

30Friday, March 6, 2009

Page 31: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 31/34

Speed vs Scale

• If you are planning for growth, scale is

far more important than speed!

• Focus on scalability rather than speed,

you can always increase scalable app,

 by simply adding more hardware.

31Friday, March 6, 2009

Page 32: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 32/34

Don’t Re-invent the wheel

• Most attempts to

make “faster”versions of native

PHP functions using

PHP code are sillyexercises in futility.

32Friday, March 6, 2009

Page 33: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 33/34

Write Only Code

• Removing comments won’t make code

faster

• Neither will removal of whitespace

• Remember, you may need to debug that

mess at some point ;-)• Shorter code != Faster Code

33Friday, March 6, 2009

Page 34: Common Optimization Mistakes - PHP Quebec 2009

8/14/2019 Common Optimization Mistakes - PHP Quebec 2009

http://slidepdf.com/reader/full/common-optimization-mistakes-php-quebec-2009 34/34

Thank You!Any Questions?

Slides @ www.ilia.ws