35
Profiling PHP Applications Sam Keen @samkeen pdxphp.org: May 2009 Meeting

Profiling PHP with Xdebug / Webgrind

  • Upload
    sam-keen

  • View
    64.526

  • Download
    1

Embed Size (px)

DESCRIPTION

Presentation given at the pdxphp.org May 2009 meeting.Introduction to using Xdebug and Webgrind to profile PHP applications

Citation preview

Page 1: Profiling PHP with Xdebug / Webgrind

Profiling PHP Applications

Sam [email protected]: May 2009 Meeting

Page 2: Profiling PHP with Xdebug / Webgrind

Summary• Huge topic, so we will settle on one aspect

of it

• Concentrate on the Code aspect of profiling

• We’ll concern ourselves more with ‘tools to get started’ rather than ‘Preferment code best practices’ (see last slide for that)

• Will introduce a ‘secret ingredient’ that makes profiling super delicious!

Page 3: Profiling PHP with Xdebug / Webgrind

The Scenario

You build a killer PHP site

Works great out of the gate but then slowly degrades as more people use it until...

Page 4: Profiling PHP with Xdebug / Webgrind
Page 5: Profiling PHP with Xdebug / Webgrind

How do we avoid this

Or at least lesson the chance of it happening or at a minimum reduce the amount of “material” hitting the fan

when it inevitably occurs

Page 6: Profiling PHP with Xdebug / Webgrind

Profiling

- should be done during development

- allows you to spot inefficiencies and bottlenecks in code rather than your clients

Photo: http://www.flickr.com/photos/chermida/2913511936/

Page 7: Profiling PHP with Xdebug / Webgrind

Facilitating Profiling During Development

• Make it as easy as possible to set up and use

• This way it can become part of your daily (OK, maybe weekly) routine.

Page 8: Profiling PHP with Xdebug / Webgrind

Many Aspects of Profiling

Code Db

SystemCPU/RAM

Network

Page 9: Profiling PHP with Xdebug / Webgrind

First steps for Db and System/Network

Query logs: slow and index-less

top, vmstat, dstat$ dstat

@see http://dag.wieers.com/home-made/dstat

Page 10: Profiling PHP with Xdebug / Webgrind

Profiling Code-Baseline-

Before you make changes, you need to get some sort of baseline of the performance of the application

Otherwise, you cannot measure improvement

So start with profiling the site as a “whole”

Page 11: Profiling PHP with Xdebug / Webgrind

Web Server Profiling Tools

Web Server

Apache BenchHTTP_loadSiege

Page 13: Profiling PHP with Xdebug / Webgrind

HTTP_LoadAfter creating for URL file (simple txt file with list o URLs (one per line) that will be randomly chosen from by http_load)

The run something like: (runs for ten seconds, with five parallel requests)

$ http_load -parallel 5 -seconds 10 urls.txt

Page 14: Profiling PHP with Xdebug / Webgrind

HTTP_Load

Output$ http_load -parallel 5 -seconds 10 urls.txt90 fetches, 5 max parallel, 805770 bytes, in 10 seconds8953 mean bytes/connection8.99999 fetches/sec, 80576.9 bytes/secmsecs/connect: 241.704 mean, 958.418 max, 73.01 minmsecs/first-response: 252.075 mean, 1067.61 max, 83.833 minHTTP response codes: code 200 -- 90

Page 15: Profiling PHP with Xdebug / Webgrind

Xdebug ProfilingFar more than just a profiler: * stack traces and function traces in error messages with: o full parameter display for user defined functions o function name, file name and line indications o support for member functions * memory allocation * protection for infinite recursions * profiling information for PHP scripts * code coverage analysis * debug your scripts interactively with a debug client

@see xdebug.org

Page 17: Profiling PHP with Xdebug / Webgrind

Xdebug Configure;##### START XDEBUG SECTION ######;zend_extension=/usr/lib/php/extensions/xdebug.soxdebug.remote_enable=onxdebug.remote_handle=dbgpxdebug.remote_host=localhostxdebug.remote_port=9000xdebug.profiler_enable = 1xdebug.profiler_output_name = cachegrind.out.%t-%sxdebug.profiler_output_dir="/Users/sam/grind-out";##### END XDEBUG SECTION ######;

http://www.xdebug.org/docs/all_settings#profiler_output_name

Page 18: Profiling PHP with Xdebug / Webgrind

Xdebug Local Setupphp.ini@ -> /usr/local/php5/lib/php.ini.zenddebugphp.ini.xdebugphp.ini.xdebug.profilephp.ini.zenddebug

~/bin (in my PATH)-rwxr-xr-x@ php-xdebug*-rwxr-xr-x@ php-xdebug-profile*-rwxr-xr-x@ php-zenddebug*

Contents of php-xdebug-profile#!/bin/shrm /usr/local/php5/lib/php.iniln -s /usr/local/php5/lib/php.ini.xdebug.profile /usr/local/php5/lib/php.inisudo apachectl restart

$ php-xdebug-profile Now running php with Xdebug PROFILE: hurray for open source

Page 19: Profiling PHP with Xdebug / Webgrind

Profiling a specific page

With Xdebug profiling enabled

Simply request the web page in question using browser

Look in your xdebug.profiler_output_dir for output

cachegrind.out.1242152836-_Library_WebServer_Documents_persist_better_see_signups_php

Page 20: Profiling PHP with Xdebug / Webgrind

Examine the output

• Traditionally: Kcachegrind

• Install on Linux, or Windows: easy

• Install on OSX: #&^!*&^!!

• The Kcachegrind UI...

Page 21: Profiling PHP with Xdebug / Webgrind
Page 22: Profiling PHP with Xdebug / Webgrind

And the Secret Ingredient

Page 23: Profiling PHP with Xdebug / Webgrind

Webgrind

Webgrind is an Xdebug profiling web frontend in PHP5

@see http://code.google.com/p/webgrind/

Simple Installation on any platform that can run WebServer/PHP5 stack

and the UI...

Page 24: Profiling PHP with Xdebug / Webgrind
Page 25: Profiling PHP with Xdebug / Webgrind

Webgrind InstallInstall (Typical WebApp install: put the folder in your webroot and edit a config file)

1. Download to your web root

* edit config.php* be sure $storageDir is writable by web server

Page 26: Profiling PHP with Xdebug / Webgrind

Extend UI

UI is HTML and js (jquery) so trivial to make changes

Page 27: Profiling PHP with Xdebug / Webgrind

The victimhttp://local.persist.com/could_improve/

Page 28: Profiling PHP with Xdebug / Webgrind

BaseLine$ http_load -parallel 5 -seconds 10 urls_could_improve.txt 1928 fetches, 5 max parallel, 1.92362e+07 bytes, in 10.0002 seconds9977.27 mean bytes/connection192.796 fetches/sec, 1.92358e+06 bytes/secmsecs/connect: 0.456549 mean, 9.326 max, 0.055 minmsecs/first-response: 21.7894 mean, 599.711 max, 0.862 minHTTP response codes: code 200 -- 1928

*you would also be watching CPU and RAM with something like dstat during this test to determine if we are CPU and/or memory bound (see resources on last slide)

Page 29: Profiling PHP with Xdebug / Webgrind

Explore the output using WebGrind

Page 30: Profiling PHP with Xdebug / Webgrind

Lots of MDB2 at the top of the list

Page 31: Profiling PHP with Xdebug / Webgrind

Static Candidate (no code)

Page 32: Profiling PHP with Xdebug / Webgrind

Push Work to Client

Push all this work to the client

$('dd.note').each(function(i){$(this).html($(this).text().replace(/(e\b)/ig, '<span style="color:red;font-weight:800;">$1</span>'));

});

Page 33: Profiling PHP with Xdebug / Webgrind

Make Adjustments• Switch to PDO

• typically lean towards php built-ins that abstract a great deal of functionality (rather than libs built in php).

• Use static (.htm) pages if we don’t need DB

• output buffer caching another alternative

• Use js to ‘markup’ content

• fastest way a web server can do work is not to do it

changes took about 40 min of work

Page 34: Profiling PHP with Xdebug / Webgrind

Re-profile

$ http_load -parallel 5 -seconds 10 urls_better.txt 5008 fetches, 5 max parallel, 3.40076e+07 bytes, in 10.0003 seconds6790.65 mean bytes/connection500.785 fetches/sec, 3.40066e+06 bytes/secmsecs/connect: 0.401153 mean, 9.334 max, 0.055 minmsecs/first-response: 8.39298 mean, 259.332 max, 0.141 minHTTP response codes: code 200 -- 5008

Improvementfetches/sec: ~250%first-response: ~275%

Page 35: Profiling PHP with Xdebug / Webgrind

ResourcesProfiling articles form eZ Publish * http://ez.no/developer/articles/ez_publish_performance_optimization_part_1_of_3_introduction_and_benchmarking * http://ez.no/developer/articles/ez_publish_performance_optimization_part_2_of_3_identifying_trouble_spots_by_debugging * http://ez.no/developer/articles/ez_publish_performance_optimization_part_3_of_3_practical_cache_and_template_solutions

Profiling articles form IBM * http://www.ibm.com/developerworks/linux/library/l-tune-lamp-1/ * http://www.ibm.com/developerworks/linux/library/l-tune-lamp-2.html * http://www.ibm.com/developerworks/library/l-tune-lamp-3.html

Excellent Open Source PHP IDE which utilizes Xdebug (step through, profile, code coverage) * http://www.netbeans.org/features/php/index.html

Profiling Presentation from core PHP folks * http://talks.php.net/index.php/Performance