36
Common Optimization Mistakes Dutch PHP Conference 2010 Ilia Alshanetsky http://ilia.ws 1

Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Common Optimization Mistakes

Dutch PHP Conference 2010

Ilia Alshanetskyhttp://ilia.ws

1

Page 2: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

PrematureOptimization

=

Solve the business case, before optimizing the

solution

2

Page 3: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

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

3

Page 4: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

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 LOC since 2000 and has become at least 4 times faster.Linux kernel code base increase by 40% since 2005 and still managed to improve performance by roughly the same margin.

LOC stats came from ohloh.net

4

Page 5: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Hardware is Cheaper!

VS

In most cases applications can gain vast performance gains by improving hardware,

quickly rather than through slow, error prone code optimization efforts.

5

Page 6: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Hardware• CPU bottlenecks can be resolved by

more cores and/or CPUs. Typically each year yields a 20-30% speed improvement over past year’s CPU speeds.

• Ability to handle large amounts of traffic is often hampered by limited RAM, and thanks to 64bit, each new server can have tons of it.

6

Page 7: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Hardware

• Drives are often the most common bottleneck, fortunately between RAID and Solid State you can solve that pretty easily now a days.

SSD

SCSI

7

Page 8: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

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 number sessions per server

8

Page 9: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

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

9

Page 10: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

• This cycle happens for every include file, not just for the "main" script.

• Compilation can easily consume more time than execution.

PHP Script

Zend Compile

Zend Execute

@includ

e/require

methodfunction

call

How PHP works in 30 seconds

10

Page 11: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Opcode Cache

• Each PHP script is interpreted only once for each revision.

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

• Opcodes can optimized for faster execution.

• Yields a minimum 20-30% speed improvement and often as much as 200-400%

11

Page 12: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Quick Comparison

0

50

100

150

200

FUDforum Smarty phpMyAdmin

Regular PHPZend PlatformAPCX-Cache

12

Page 13: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

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 multiple servers for improved reliability and performance.

session.save_handler = “memcache”

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

13

Page 14: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Everything has to beReal-time

14

Page 15: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Complete Page Caching

• Caching Proxy (Ex. Squid)

• Page pre-generation

• On-demand caching

15

Page 16: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Partial Cache - SQL

• In most applications the primary bottleneck can often be traced to “database work”.

• Caching of SQL can drastically reduce the load caused by unavoidable, complex queries.

16

Page 17: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

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);}

17

Page 18: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Partial Cache - Code

• Rather than optimizing complex PHP operations, it is often better to simply cache their output for a period of time.

• Faster payoff

• Lower chance of breaking the code

• Faster then any “code optimization”

18

Page 19: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

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

$key = __FUNCTION__ . serialize(func_get_args());if (!($result = memcache_get($key))) {

$result = // function code// cache query result for 1 hourmemcache_set($key, $result, NULL, 3600);

}return $result;

}

19

Page 20: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Compile Your Tools

• Distribution binaries suck!

• More often than not you can realize 10-15% speed increase by compiling your own Apache/PHP/Database from source. (unless you are using Gentoo)

20

Page 21: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Output Buffering

• Don’t fear output buffering because it uses ram, ram is cheap. IO, not so much.

21

Page 22: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

22

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

• Optimizes PHP to OS Communication

• Reduces Number Of System Calls

Matching Your IO Sizes

PHP Apache OS Client

22

Page 23: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

23

• Efficient

• Flexible

• In your script, with ob_start()

• Everywhere, with output_buffering = Xkb

• Improves browser’s rendering speed

PHP: Output Control

PHP Apache

23

Page 24: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Apache: Output Control

• The idea is to hand off entire page to the kernel without blocking.

• Set SendBufferSize = PageSize

Apache OS

24

Page 25: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

OS: Output ControlOS (Linux)

/proc/sys/net/ipv4/tcp_wmem

4096 16384 maxcontentsize

min default max

/proc/sys/net/ipv4/tcp_mem

(maxcontentsize * maxclients) / pagesize

✴ Be careful on low memory systems!

OS Client

25

Page 26: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Upgrade Your PHP• Before “upgrading” your code, upgrade

your PHP. Newer versions are typically faster!

0

37.5

75

112.5

150

Speed % (base line of PHP 4.4)

PHP 4.4PHP 5.0PHP 5.1PHP 5.2PHP 5.3PHP-CSV

26

Page 27: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Don’t Assume

• One of the most common mistakes made even by experienced developers is starting to optimize code without identifying the bottleneck first.

Assume nothing, profile everything!

27

Page 28: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Profile, Profile & Profile

Xdebug and XHProf extensions provide a very helpful mechanism for identifying TRUE bottlenecks in your code.

28

Page 29: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Kcachegrind

Xdebug provides kcachegrind analyzable output that offers an easy visual overview of your performance problems

29

Page 30: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

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!

30

Page 31: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Watch Your Errors

• Excessive, non-critical errors, such as E_NOTICE or E_STRICT can only be detected via error-logging.

• PHP code that generates any errors is going to impact performance!

Not Easily Detectable by Profilers

31

Page 32: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Micro Optimization

• Takes a long time

• Won’t solve your performance issues

• Almost guaranteed to break something

• Cost > Reward

32

Page 33: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

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.

33

Page 34: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Don’t Re-invent the Wheel

• Most attempts to make “faster” versions of native PHP functions using PHP code are silly exercises in futility.

34

Page 35: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

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

35

Page 36: Common Optimization Mistakes - Ilia · PHP has grown from less than 1 million LOC to over 2 million LOC since 2000 and has become at least 4 times faster. Linux kernel code base increase

Thank You!Any Questions?

Slides @ www.ilia.wsComments @ joind.in/1535

36