33
© All rights reserved. Zend Technologies, Inc . PHP Performance Principles and Tools By Kevin Schroeder Technology Evangelist Zend Technologies I have stickers!

PHP Performance: Principles and tools

Embed Size (px)

Citation preview

Page 1: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

PHP PerformancePrinciples and Tools

By Kevin Schroeder

Technology Evangelist

Zend Technologies

I have stickers!

Page 2: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

About Kevin

Past: Programming/Sys Admin

Current: Technology Evangelist/Author/Composer

@kpschrade

Page 3: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Follow us! Zend Technologies

http://twitter.com/zend

http://twitter.com/kpschrade (me!)

Page 4: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Join us at ZendConThe premier PHP conference!

October 17-19, 2011 – Santa Clara, CA

www.zendcon.com

Conference ThemesCloud ComputingLearn about the latest developments in PHP Cloud infrastructure, management and application services

Mobile and User ExperienceLearn how to build engaging mobile apps with the latest PHP technologies and tools

Enterprise and Professional PHPExplore PHP best practices, new technologies and practical tips with industry experts

Conference Highlights•Sessions focused on how to best develop and

deploy PHP

•Sessions designed for all knowledge levels

•Intensive tutorials for accelerated learning

•PHP Certification crash courses and testing

•Exhibit hall showcasing the latest products

•Special networking opportunities during meals and

events

Page 5: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Quiz

Who here is a sysadmin?

Who here is a developer?

Who here is both?

Page 6: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

WARNING!• Performance is a HIGHLY context sensitive topic• There are no answers here; only possible

directions• Do not micro-optimize

20ms cf. 100ms is not a 5 fold improvement in performance, it is a 5 fold waste of your time• Unless you’re Yahoo, Google, Facebook, etc.

The perception of performance is as important as actual performance

• Use the scientific method to find performance problems Observe Hypothesize Experiment

• Solve one problem at a time and MEASURE RESULTS!

Page 7: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

General categories of performance problems

Page 8: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Performance – Database

• Symptoms Pages take a long time to load with Apache/IIS/PHP using

proportionally less of the CPU time compared to DB

• Causes The list could go on for 4.23x105 slides

There are so many ways you can screw up your queries

• Tools Intra-application query monitoring, usually via a DB

adapter abstraction layer

Zend Server – A distinct group of monitoring options for DB calls in PHP

Page 9: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Performance - Database

• Possible remedies Simplify your queries

Make your queries more complex

Tune your database

• Don’t just automatically allocate more memory

• Don’t just create indexes

• Keeping this light because there are other, better, MySQL performance experts (many of them here)

Page 10: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Performance – Network Latency• Symptoms

Depends on where the latency is occurring

Often slow page responses with low CPU usage

• Causes Network Saturation (probably not too likely)

DNS Reverse Lookups

TCP Handshakes

High number of “hops”

TCP Backlog

… and much more

• Tools Wireshark

Tcpdump

Zend Server – monitor slow socket function execution

Page 11: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

The TCP handshake

T/TCP SYN + Response Data + FIN

T/TCP SYN + Response Data + ACK

+FIN

ACK

Page 12: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Performance – IO Contention

• Symptoms Unpredictable and difficult to reproduce page load times, but

often corresponds to request frequency

• Causes Too few spindles serving too much data

High write concurrency

Insufficient free system RAM

Insufficient depth of data directory structure

• Tools Zend Server (in extreme cases, long fopen() calls)

vmstat & top (Linux)

System Internals Process Explorer (Windows)

Page 13: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Performance – IO Contention

• Remedies Increase the number of disks

Favor directory depth over width for storing raw data files

Increase RAM so read operations can be pulled from memory

Page 14: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Performance – CPU Utilization

• Symptoms Page load times either correspond directly to CPU utilization

or are consistently slow

• Causes Bad code or over-architected solutions

Excessive recursion

Excessive IO

High server utilization• too few servers serving too much traffic

• Tools top & vmstat (Linux)

System Internals Process Explorer (Windows)

Page 15: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Performance – CPU Utilization

• There are often 2 different paths this will usually follow User time

• Time spent executing the code that you wrote

System time• Time spent running functionality that your code calls, such as

networking, file system, etc.

Page 16: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Performance – Network Connectivity

• Symptoms Database or web service calls take long to execute

• Causes If read functions die unexpectedly, bad network hardware

might be a cause Slow DNS queries Full TCP backlog

• Tools Zend Server – monitor failed calls to socket_connect(), fsockopen() or fread()

Wireshark Tcpdump

Page 17: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

OO

• Beware of magic! __get, __set, __call

Situations where they are often used• When representing another object that it has no knowledge of

- I.e. Soap requests, COM objects, Java objects, database tables

• When there are property-level permissions on an object’s properties. Keeping the properties private and using __get/set to determine access

If used sparingly they won’t affect you, but many modern applications have lots of recursion or large underlying architectures that can have unpredicted consequences

Page 18: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

!Preg

• Use preg_match for data checking only when you have to stripos(‘http://’, $website) is over twice as fast as preg_match(‘/http:\/\//i’, $website)

ctype_alnum() is almost 5 times as fast as preg_match(‘/^\s*$/’);

Casting “if ($test == (int)$test)” is well over 5 times faster than preg_match(‘/^\d*$/)

• Preg will probably not bite you unless you have a lot of recursive logic that depends on it

Page 19: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Tools

Page 20: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

OS tools to use

• Devs are really bad about thinking about the OS

• Use OS tooling!!! vmstat

top

tcpdump & wireshark

strace• Try strace httpd –X at least once

lsof• You can map file descriptors found in strace to descriptors in

lsof

Page 21: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Zend Server

• Monitors for several events like slow requests, slow DB calls, high memory usage, etc.

• Provides the context for those requests

• Can be configured to provide profile-like data from production installations

• Has a built in queuing mechanism that allows you to offload non-immediate processing (which can actually be quite a lot)

Page 22: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Do you queue?

• Offloading long running processes to a queuing system can help make your front end servers scale more vertically

• Examples include Job Queues

• Zend Server 5 (http://www.eschrade.com/ tag: job-queue)

• Gearman

• Beanstalk

Message Queues• ActiveMQ

• RabbitMQ

Page 23: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

TODO || ! TODO

Page 24: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

What not to do

This is not complete!!!• “Oh we’ll just cache it”• “Our NFS server is just fine for serving PHP files”

Use a frigging deployment mechanism• “That LIKE query is necessary”

You may need to structure your data a little different• Don’t make your DB gues

You might need to use a search engine instead• Zend_Search_Lucene• SOLR

• “Using arrays is faster than objects, we’ll use them instead”

• “Monitoring/diagnostic tools are too expensive”

Page 25: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

List of things to do

This is not complete!!!

• THINK before you code!!

• Minimize require_once calls/use an autoloader

• Try to make your architecture more horizontal rather than vertical People from Java backgrounds tend to go more vertical

There is often a correlation between long stack traces and poor performance

• Use Lazy Loading/Load on Demand SPL arrays and such are great for this

Page 26: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

List of things to do

• Use diagnostic tools! Zend Server Monitoring and Code Tracing

Some kind of profiler

• Utilize compiled PHP functionality for iterative complex functionality.

• Use Ajax Allows you to make a more responsive page by having fast

content available immediately and slow content available as it’s ready

Page 27: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

List of things to do

• With multiple service calls, consider batching multiple NB-IO requests Facebook does this, so it must be cool

Synchronous requests – 4.2 seconds

Asynchronous requests using curl – 1.5 seconds

Page 28: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Fin

Page 29: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Questions?

Page 30: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

To contact me after my presentation, text XIK to INTRO (46876)

Follow us! Zend Technologies

http://twitter.com/zend

http://twitter.com/kpschrade (me!)

Page 31: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

Join us at ZendConThe premier PHP conference!

October 17-19, 2011 – Santa Clara, CA

www.zendcon.com

Conference ThemesCloud ComputingLearn about the latest developments in PHP Cloud infrastructure, management and application services

Mobile and User ExperienceLearn how to build engaging mobile apps with the latest PHP technologies and tools

Enterprise and Professional PHPExplore PHP best practices, new technologies and practical tips with industry experts

Conference Highlights•Sessions focused on how to best develop and

deploy PHP

•Sessions designed for all knowledge levels

•Intensive tutorials for accelerated learning

•PHP Certification crash courses and testing

•Exhibit hall showcasing the latest products

•Special networking opportunities during meals

and events

Page 32: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

List of things to do

• Similarly, be careful when dealing with lots of Ajax data Developers running test data often under-estimate how

much data their code will send back

The browser needs to process that data

With large data sets the browser may take a while to update the DOM

2 options• Use staggered loading

• Pass rendered content to the browser and attach with innerHTML

Page 33: PHP Performance: Principles and tools

©All rights reserved. Zend Technologies, Inc.

List of things to do

• Be aware of your page size Oracle – 104kb – 861ms

Zend – 183kb – 2.4 seconds

Microsoft – 238kb – 4.4 seconds

IBM – 640 kb – 4.6 seconds

• There is a relatively direct relationship between the amount of data on your web page and the render time