PHP:
Debugging, Testing, and Profiling
ATLPHP - Atlanta PHP Users Group
January 2007
Who Am I?
Alan Pinstein
Born 1974, programming since 1980
C, C++, Obj-C, PHP, Java, etc...
PalmOS, Apple (Cocoa), Web Applications
PHP since 1998
Owner / Developer @ Showcase Web Sites
Involved in open source community:
PHOCOA - http://phocoa.com
StatViz - http://statviz.sourceforge.net/
Propel - http://propel.phpdb.org/trac/
Overview
Debugging
Finding and Fixing Problems
Logic Testing
Automating Code Testing
Performance Testing
Benchmarking
Profiling
Audience Survey
Who has used these tools?
An IDE? (Zend, PHPEd, Komodo, Eclipse)
Debug extensions? (apd, xdebug, dbg)
error_reporting(E_STRICT)?
Test rigs? (PHPUnit)
Platform?
Windows?
Unix / Linux / Mac?
PHP5?
Debugging: Environment
Text Editor
IDE - Integrated Development Environment
Text editor with code completion, syntax
coloring, documentation, etc.
Visual Debugger
Examples: Zend, Komodo, PHPEd, Eclipse, etc.
Eclipse is free, but tricky
The rest are commercial, but tend to have
trials and cheap personal licenses (<$100)
Debugging: Tools
Built-in PHP functions
View data
Verbose warnings
Debugging Extensions
Stack traces
Command-line debuggers
Examples: apd, xdebug, dbg
PHP Modules
PEAR::Log
PHPUnit3
Debugging: Setup
Debug Extensions
pecl install xdebug-beta
pecl install apd
fix formatting bug:
http://pecl.php.net/bugs/bug.php?id=4569
Need to edit php.ini to activate
PHP Modules
pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit
Debugging: PHP Functions
error_reporting(E_ALL | E_STRICT);
Display helpful warnings such as use of uninitialized
variables
ini_set(¡¥display_errors¡¦, true);
Print errors to output rather than just in web
server log
Output Functions:
print
print_r - print data formatted
var_dump - print data formatted (more info)
die - print and stop execution
throw - throw an exception (catchable)
PEAR::Log - write to file, display, etc
Debugging: XDebug
XDebug enhances normal PHP error capabilities
Enable xdebug in php.ini:
zend_extension=/opt/local/lib/php/extensions/no-debug-nonzts-
20060613/xdebug.so
Examples:
Stack traces on errors and warnings
Pretty-print version of var_dump
Debugging: ZDE
View source code with syntax coloring
Set breakpoints
See call stack and view data
Testing
Why write tests?
Makes you think about all possible inputs/
outputs
Spot new bugs quickly - ¡§regression testing¡¨
Testing: Automation
Unit Test - test a single ¡§subunit¡¨ of a program
programmatically
Integration Test - test the integration between
multiple units
Browser Test - test the way the app performs
in a browser
Testing: Writing Unit Tests
Write a unit test for a Calculator class
Test both success and failure
Run test(s) with PHPUnit
Testing with PHPUnit3
PHPUnit3 has comment-based testing!
No need to write any code to perform simle
tests
Note: PHPUnit3 and Xdebug are incompatible
Testing: Example
PHPUnit3 Can build tests from comments for simple
¡§assert¡¨ tests
Or you can write specific
test classes
Testing: Notes
When an error occurs, is the bug in the code
or in the test itself?
When to write tests?
Before or After writing the ¡§real¡¨ code?
Over time - when new situations arise
Who should write tests?
Ideally, someone besides the unit
programmer
Higher level testing with other tools
Selenium - automate browser-based tests
Code Coverage
Profiling
What is Profiling?
Examines the p a program erformance characteristics of
Records every function call
Tracks execution time of all function calls
Tracks memory usage
Preferred tool: apd
XDebug generates only cachegrind files
Requires kcachegrind (X-windows) to view
apd is simpler to use but just as powerful
Profiling and Benchmarking
Why profile?
Faster / more efficient code
How do you measure speed?
Benchmarking!
How fast does it need to be?
Anticipate your needs; meet your needs
Make it scalable
Beware!
Perfection is the enemy of good enough
Know when to quit
No function taking more than 1-2%
Profiling and Benchmarking:
Example
How can we measure performance?
Single script execution time
only useful on long-execution-time scripts
Time to execute same script multiple times
Benchmarking tools - establish a baseline
ab = Apache Benchmark
ab -n 100 -c 1 http://10.0.1.202:8080/php-talk/profile.php
ab output
Profiling and Benchmarking:
Example
Turn on profiling programmatically with:
apd_set_pprof_trace('.');
Run script once
Evaluate ¡§dump¡¨ with pprofp
Profiling and Benchmarking:
Example
Look for:
Functions with high % of time
Functions that take a lot of time
Functions called lots of times
real - total elapsed time from start to finish
system - I/O, kernel, etc.
user - YOUR CODE!
real != system + user
Why? Resource contention, I/O bottlenecks
pprofp -z pprofp.1234
Profiling and Benchmarking:
Example
Better, but... % of time in makeWorldPeace() is still high...
Profiling is an art and a science...
Benchmark to confirm
Elapsed time down to 0.01s from 0.19s
Re-profile with performance improvements
ab results from: to:
Resources
Debug Extensions
apd - http://pecl.php.net/package/apd
xdebug - http://xdebug.org
IDEs
Zend
http://www.zend.com
Komodo
http://www.activestate.com/Products/Komodo
PHPEd
http://www.nusphere.com
Resources
Testing
PHPUnit
http://www.phpunit.de/
Great PHPUnit Presentation:
http://sebastian-bergmann.de/talks/2006-11-02-
PHPUnit.pdf
Selenium
http://www.openqa.org/selenium-rc/
Benchmarking
ab
http://httpd.apache.org/docs/2.0/programs/ab.html
siege
http://www.joedog.org/JoeDog/Siege
PEAR::Benchmark
http://pear.php.net/package/Benchmark
Q & A
Sunday, October 25, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment