Eric Hogue on May 9th, 2011

In a previous post, I talked about Continuous Integration. If your Continuous Integration server runs on every commits, it will help you keep your code quality high. It will also make integration a non issue.

However, when I make a mistake, produce sub-optimal code or if I write code that does not respect our coding standards I want to know before I commit. I would rather fix my errors before they break the build. I can run all the tools that the server runs. But running them on my machine and analyzing the results every time I save will be painful. Fortunately, there is an easy solution.

PHPsrc

PHPsrc is a plugin that allow you to run PHP_CodeSniffer, PHPUnit, PHP Depend and PHP Copy/Paste Detector directly in Eclipse. The site also says that more tools should come. As you work, you will see any transgression you make. That will save you from breaking the build, but it also makes it easier to fix the problem. After all, you just wrote the faulty lines of code.

You install it like any other Eclipse plugin. Go to the Help menu and click on “Install new software…” In the Install dialog, click on “Add…”, choose a name for the repository and enter “http://www.phpsrc.org/eclipse/pti/” in the location. After you click on OK, Eclipse will detect “PHP Tool integration. Select it and click on Next twice. Accept the condition and click on Finish. After the installation is completed, you will have to restart Eclipse.

After the installation, in the preferences you will have a new “PHP Tools” menu. Make sure that every tools have an PHP executable and a PEAR library selected. For PHP CodeSniffer, you will also need to pick a standard.

PHPsrc Configuration

PHPsrc Configuration

Your Eclipse should now have new buttons in the toolbar and a new “PHP Tools” sub menu in the PHP Explorer.

PHPsrc Toolbar

PHPsrc Toolbar

PHPsrc Menu

PHPsrc Menu

PHPsrc will now check your code every time you save. Errors and warnings will be added to the Problems tab and the Type will be the tool that found the problem. The lines that causes problems will also be underlined like a syntax error. The copy paste detector will send the results to the console.

Here’s what Eclipse looks like with PHPsrc:

PHPsrc Results

PHPsrc Results

If you develop PHP code in eclipse, you should definitely give PHPsrc a try. If you use a continuous integration server, being warned about potential issues before you commit to your source control repository can save you from becoming the one who broke the build.

Tags: , ,

Eric Hogue on May 3rd, 2011

According to Wikipedia, continuous integration

implements continuous processes of applying quality control — small pieces of effort, applied frequently.

In simple terms, you verify that your project meets the quality standards frequently. This way, you can catch any deviation early. Doing the integration in small increments makes it easier. Implementing a continuous integration server can look difficult when you’ve never done it. In this post I will try to document how to set it up. I take for granted that you already have a LAMP server configured.

The Tools

PHP has many tools that can be used to analyse your code. There are tools for running unit tests, detecting copy/paste, checking the coding standards and more. We have to thank Sebastian Bergmann for this because he wrote most of those tools. I’ll cover some of them here, but there are more.

PHPUnit

PHPUnit is the PHP version of the xUnit framework. It allows running automated tests on code. To install it, use those commands:

sudo apt-get install php5-curl php-pear php5-dev
sudo pear upgrade pear

sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover components.ez.no
sudo pear channel-discover pear.symfony-project.com

sudo pear install phpunit/PHPUnit

After you installed PHPUnit, you can run it with this command:

phpunit .

It will look for test cases in the current folder and sub folders.

You can also use PHPUnit to generate a code coverage report.

phpunit --coverage-html ../CodeCoverage .

This will generate an HTML report of the code that is covered by the tests. The files will be located in the path you passed it. In this case, a CodeCoverage folder located in the parent folder. The reports will look like this:

Coverage index page

Coverage index page

If you click on a file, you will get the coverage by function and the content of the file. The code will be highlighted to show what is covered and what is not.

Coverage Of a File

Coverage of a file

Code not covered

Code not covered

PHP CodeSniffer

PHP Code Sniffer analyse you code and detect violations to coding standards. It comes bundle with a few standards like the Zend and PEAR standards. But you can write your own standard by expanding an existing standard.

It’s a simple PEAR install. Than you can run it by calling phpcs with the standard to use and the path to check.

sudo pear install PHP_CodeSniffer
phpcs --standard=Zend .

You will get a result like this:

CodeSniffer results

CodeSniffer results

If you don’t want to specify the standard to use on every call, you can set a default with:

phpcs --config-set default_standard Zend

PHP Depend

PHP Depend is a tool that will perform a static analysis of you code base. It generates software metrics that can be used to evaluate the complexity and quality of your code. Install it with PEAR:

sudo pear channel-discover pear.pdepend.org
sudo pear install pdepend/PHP_Depend-beta

To execute it, you call pdepend and pass it the type of reports you want it to generate. Execute it without parameters to view a brief description of the possible parameters.

pdepend --jdepend-xml=../jdepend.xml --jdepend-chart=../dependencies.svg --overview-pyramid=../overview-pyramid.svg .

This will generate an XML with the packages dependencies, a diagram of the dependencies and the overview pyramid of the project.

Abstraction Instability Chart

Abstraction Instability Chart

The Abstraction Instability Chart was invented by Robert C. Martin (uncle Bob) as a mean of measuring the instability and abstraction of the packages of a project. You should try to keep your packages as close as possible to the green line. An abstract package should have more classes depending on it, so it should be very stable. Concrete packages can be unstable, because nothing should directly depends on them if you code against abstractions. Having said that, I am not sure how it breaks my code into packages. I would guess it uses folders.

Overview Pyramid

Overview Pyramid

The Overview Pyramid contains the metrics extracted by PHP Depend. The PHP Depend documentation has a great explanation of the pyramid, but I’ll try to give a brief overview.

The top part displays the metrics about inheritance. They are Average Number of Derived Classes (ANDC) and Average Hierarchy Height (AHH). ANDC tells you how many of your classes are derived from other classes. AHH is a measure of how deep your hierarchy is.

The bottom right part shows the coupling metrics. NOM represents the numbers of methods in your project. CALLS is the number of methods calls. FANOUT counts the types that are references by your classes. It counts only references that are not part of the same class hierarchy.

The bottom left part of the pyramid contains the metrics that are used the most. NOP is the number of packages, NOC the number of classes, NOM the number of methods, LOC the lines of code and CYCLO is the cyclomatic complexity.

The numbers in the middle represents the actual count and the numbers on either sides are averages of the metrics. They represent the value of the row under it divided by the value of the current row. So in my graph, I have a total of 168 lines of code and 26 methods, for an average of 6.462 lines per method.

PHP Mess Detector

PHPMD is used to detect problems in your code. It uses the same metrics as PHP Depends to give you feedback on your code. It can detect possible bugs and common issues. Install it with PEAR.

sudo pear channel-discover pear.phpmd.org
sudo pear channel-discover pear.pdepend.org
sudo pear install --alldeps phpmd/PHP_PMD

To execute PHPMD you need to give it the files to parse, a format for the output and the rule sets to use.

phpmd . html codesize,unusedcode,naming,design --reportfile ../messdetector.html --exclude Tests/

This will run it on the current folder and generate html. It will use all 4 rule sets install with PHPMD. The reportfile option tells PHPMD to send the output to the specified file instead of stdout and the exclude options tells it to ignore files in the Tests folder. It generates something like this:

PHP Mess Detector Output

PHP Mess Detector Output

PHP Copy/Paste Detector

As the name implies, PHPCPD detects when code has been copied over in your code. This gives you great candidates for refactoring your code.

Install it with PEAR. If you already installed PHPUnit, you should not need the two channel-discover commands.

sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover components.ez.no

sudo pear install phpunit/phpcpd

When it’s installed you can just run phpcpd by giving it the path to check. It will returns something like this:

PHPCPD Output

PHPCPD Output

PHP Dead Code Detector

PHPDCD scan you project and detect functions that are not called. This might not look like a big deal, but dead code can really slow down the maintenance of a project. Developers have to scan and understand this code, and they might end up fixing bugs in code that is never called. Delete this code, you can get it back from your source control if you need it.

sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover components.ez.no

sudo pear install phpunit/phpdcd-beta

Run it by calling phpdcd with the path to scan. I make it ignore my tests. If not, it will flag them all because they are not called anywhere in the code, phpunit runs them.

PHPDCD Output

PHPDCD Output


 

Putting It All Together With Jenkins

All those tools are very good, but having to call them one by one is painful. Jenkins is the continuous integration server that will manage continuously running all of those tools. It will be a central point of access for all the reports and send notifications when a build failed.

Building Your Project

Jenkins can build a project in many ways. It can use a Windows batch file, execute a shell script, Ant or Maven build files.

For my project I used Ant. To install Ant, make sure you have the Java JDK installed, then you can install it with apt.

sudo apt-get install default-jdk
sudo apt-get install ant

Then, you need to create your build file. The Ant site has a great introduction on creating build files. Here’s what my build file looks like:


<project name="Test" default="build" basedir=".">
	<property name="output" location="${basedir}/buildOutput/"/>

	<target name="init">
		<mkdir dir="${output}"/>
		<mkdir dir="${output}/phpcs/"/>
		<mkdir dir="${output}/pdepend/"/>
	</target>

	<target name="build" depends="init, test, phpcs, phpmd, phpcpd, pdepend">
	</target>

	<target name="test">
		<exec executable="phpunit" failonerror="true">
			<arg line="--coverage-clover ${output}/CodeCoverage/clover.xml 
				--coverage-html ${output}/CodeCoverage/ 
				."/>
		</exec>
	</target>

	<target name="phpcs">
		<exec executable="phpcs"> 
			<arg line="--report=checkstyle
              			--report-file=${output}/phpcs/checkstyle.xml
              			--standard=Zend
              			${basedir}" />
		</exec>
	</target>

	<target name="phpmd">
		<exec executable="phpmd">
			<arg line="
				 . xml codesize,unusedcode,naming,design --reportfile ${output}/messdetector.xml --exclude Tests/
			" />
		</exec>
	</target>

	<target name="phpcpd">
		<exec executable="phpcpd">
			<arg line="
				 --log-pmd ${output}/phpcpd.xml .
			" />
		</exec>
	</target>

	<target name="pdepend">
		<exec executable="pdepend">
			<arg line="
				--jdepend-xml=${output}/pdepend/jdepend.xml 
				--jdepend-chart=${output}/pdepend/dependencies.svg 
				--overview-pyramid=${output}/pdepend/overview-pyramid.svg 
				--ignore=Tests/ 
				.
			" />
		</exec>
	</target>
</project>

It defines a default target that make sure the needed folders for the output are there, then it calls PHPUnit, PHP Code Sniffer, PHP Mess detector, PHP Copy Paste Detector and PHP Depend.

Installing Jenkins

Installing it it pretty easy. There are package for many OS. For Ubuntu, just follow the instructions from the Jenkins site. Download the key and add it to apt.

wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -

Add this line to /etc/apt/sources.list

deb http://pkg.jenkins-ci.org/debian binary/

Update your apt index and install Jenkins

sudo apt-get update
sudo apt-get install jenkins

This will install Jenkins, set it as a daemon and start it. After, you can navigate to http://localhost:8080 to access your Jenkins installation.

Jenkins Home Page

Jenkins Home Page

Configuring Jenkins

Jenkins has many configuration options. I won’t go through all of them, but I’ll try to cover those I thinks are important. First click on “Manage Jenkins” then “Configure System”. Go through the settings there and configure Jenkins according to your needs. You should probably enable the security and pick how your users will log in. You should also configure the SMTP servers so Jenkins can send you emails.

After, look at the plugins and enable those you need. I use Git for my source control, so I enabled the Git plugin. All I needed to do was select the plugin, click on the Install button at the bottom of the list and click on the restart button when it was done.

You will also need a bunch of plugins to manage the data produced by the tools. Clover will display a code coverage graph from with the data built by PHPUnit. Checkstyle to display the results of PHP Code Sniffer. PMD for the PHP Mess Detector results. DRY to show duplicated code. And finally JDepend to show the metrics produces by PHP Depend.

Creating You Project

This is where you will configure Jenkins to build your project. Click on the “New Job” link. Give your job a name, select the free style project and click on OK. You will be taken the Configure page of the project. There are many options available and they all have a description if you click on the question mark icon.

First thing I did is a trick I got from Sebastian Bergmann’s Template for Jenkins Jobs for PHP Projects. I added an embed tags with links to the svg files created by PHP Depend. Since they are not showed by the plugins and they contains valuable information, it’s nice to see them on my project page.

Next, I needed to configure the source control repository. I gave Jenkins the path to my Git repository on the same machine. I left the branch blank so it check for changes in all the branches and build them. I entered the URL to my Gitweb interface so my builds can contains links to the repository and the commit that triggered the build.

Git requires that you set a user name and an email. The advanced section of the Git repository setup have fields for them, but they are not saved correctly, so you will have to do it manually.

su - jenkins
git config --global user.name "Jenkins"
git config --global user.email "jenkins@yourdomain.com"

This problem should be fixed in the next version.

You needs to tell Jenkins when to build your project. I decided to have Jenkins poll Git every minutes and launch a build every time something changed. I your project has many commits and takes time to build, you might want to make interval between checks longer.

In the build section, I just told Jenkins to invoke Ant. Since I want it to launch the default action in my build file, I did not need to give it a target.

Post Build Actions

This is where most of the configuration for the project is needed. All the plugins we installed for displaying the results of the build need to be configured. Make sure, you select all the plugins you have installed.

Many plugins will requires a results file. This is the files produce by the tests tools. Enter the relative path the results files so Jenkins can read them. There are also “Run always” check-boxes. By default, some plugins don’t run on failed build. When “Run always” is checked, the plugin will run no matter the status of the build.

Some plugins also requires some thresholds. Those are the values that tell Jenkins when to consider a build unstable, or failed. Configure them to values that make sense for your project. For the code coverage, set the conditionals to 0%, because they are not reported.

At the bottom of the project settings, you can also configure Jenkins to send emails on failed build. It will ask you for a list of addresses where to send the email and you can make it send another email to those who break the build.

Jenkins Project Settings

Jenkins Project Settings

 

The End Result

Keep Jenkins running for a while. Work on your project and make a few commits. After some time, you will have a project page that looks like this:

Jenkins Project Page

Jenkins Project Page

If you click on a build in the Build History, you will get a page like this one:

Jenkins Build Results

Jenkins Build Results

If you have a team that commit early and often, Jenkins will provide some invaluable information. You can use it to look for part of your code where you need improvements. You can always know if your new developer is not following the team coding standards. And you can pinpoint area that might be too complicated and should be refactored. With a good test suite in place that automatically runs on every commit, you can detect regressions as soon as they are committed.

Wrapping It Up

The subject of Continuous Integration is broad and this is already a long post. And there is still much I need to learn about it. Creating the build script and configuring the Jenkins project was a lot of work. It was fun to do and I learned a lot, but doing it manually for every project would be daunting. I already mentioned it earlier, there is a great template job for Jenkins. It has all the instruction for installing it, a build script and the Jenkins project.

Tags: , , ,

Eric Hogue on April 14th, 2011

Late static binding is one of the new features of PHP 5.3. It came out almost 2 years ago, but it to me that many programmers around me have no idea about it. Myself, I have learned about it around 6 months ago.

The PHP documentation defines late static binding as a way to “reference the called class in a context of static inheritance.” This definition didn’t really help me the first time I read it. Fortunately, there are more explanations in the documentation, and there are good examples. If you haven’t, you should read it.

How Does It Work

To use late static binding, you need a class that inherits from another one and some static methods that are overridden. Take the following code:

class ParentClass {
    public static function normalCall() {
        self::calledMethod();
    }   

    public static function lateStaticCall() {
        static::calledMethod();
    }   

    public static function calledMethod() {
        echo "Called in Parent\n";
    }   
}

class ChildClass extends ParentClass {
    public static function calledMethod() {
        echo "Called in Child\n";
    }   
}

The method normallCall() represent the traditional way of using static functions. The self keyword will call the function in the current class. So no matter if I call it with ParentClass::normalCall() or ChildClass::normalCall(), the calledMethod() of the ParentClass will be called and “Called in Parent” will be printed.

However, the lateStaticCall() method uses the new “static” keyword. With this keyword, the call will be forwarded to the class on witch the original method was called. So ParentClass::lateStaticCall() will end up calling ParentClass::calledMethod() and print “Called in Parent”. But ChildClass::lateStaticCall() will call calledMethod() in the ChildClass and print “Called in Child”.

When Should I Use It

I am really not sure. Personally, I try to avoid static methods. They are often better ways to do it. However, if you have a class hierarchy with static methods and you need to override how they act for some child classes, it can be a solution.

If you used in production code, or know a good reason to use it, I would really appreciate if you leave a comment with it.

Tags: ,

Eric Hogue on April 6th, 2011

Many developers I know never attempt any tech event. I am lucky to live in Montréal where we have plenty of them. The Montreal NewTech calendar shows that we have activities for everyone. And it does not have everything. Some meetings cover programming languages, others are targeted at startups, or tools like WordPress. Even with all those choices, most of the developers around me never go to any of those events.

User Group Meetups

Most user group have monthly meetings. They meetup at a certain day every month to discuss about their subject. I have a family, so I cannot attempt to all the meeting I would like, but I try to go to PHPQuébec, < span=""> href=”http://js-montreal.org/”>JS-Montreal and Montreal.rb meetings. We also have meetings for Java, .Net, augmented reality, Clojure and many more. All those events are free. They are great opportunities to learn about the subjects you like and what surrounds it. The meetups are not always focused 100% on the subject of the group. I have seen presentations about startups and how to finance them at PHPQuébec and a presentation about a freelancer association at Montreal.rb. And most of these events are followed by a visit to a bar, where you can discuss with other passionated developers around a drink.

Conferences

Tech conferences are bigger events. They are usually held once a year. Some are always presented in the same city, and other moves from city to city. Those events bring together many great developers. You get to be around very intelligent people. Not only the speakers, but the audience is also very knowledgeable. When you spend a day learning about 5 or 6 different subjects, you go home tired, but with many ideas on how to improve as a developer. This year, I was lucky enough to have my employer sends the entire team to Confoo, but last year I paid for my ticket, and it was worth every dollars.

Conferences can appear a little expensive, but developers should be able to afford it. Most of them offers an early bird rate that allow to save a few hundred dollars. And some of them are really not that expensive. Last year, I attempted Agile Tour Montreal. For 50$, I had a a full day of training. There is also Day Camp 4 Developers, an online conference that should be held 2 or 3 time a year. On this one, you get 5 sessions of an hour for 35$, and you can also download the videos after the event.

For those who don’t have local conferences, it’s harder. You have to add the price of the plane tickets and a few night at the hotel. Even with this, I think it’s worth the price. I am saving some money to go to an event outside of Montreal next year. I haven’t decided yet, but the PHP Community Conference and Tek are pretty tempting.

What Do I Get Out Of It

The obvious answer is learning. When I go to these types of events, I learn a lot. I am exposed to knowledge I would never come across at work. But it’s only a small part of it. To me, the social aspect of it is as important. Conferences and meetups gave me a chance to meet many developers that share the same passion for programming and learning. During the discussions in the pauses or around a beer after the events, I have had the chance to debate about ideas and learn even more. They also offers great opportunities to add to your network. Who knows, the next time you will need a job, maybe someone you met at an event will remember you.

And recently, I realized that it changed me. I have never been a social person. I don’t like talking to people I don’t know. But this changed a little. I now look forward to those events. They offer me a chance to talk to new peoples, chance I use to dread. And it’s not only for technical stuff, I am not as reluctant as I use to be to talk to strangers.

Tags: , ,

Eric Hogue on March 30th, 2011

When developing web applications, we often run into performance issues. People often blame PHP or MySQL for bad performance, but in most case the answer is not that easy. Blindly trying to optimize random parts of our applications can lead to some uneven results.

There are many available tools to profile a PHP application. Learning how to use them can help us pinpoint which parts are slow. With this information we can pick the optimizations that will give us the best results.

This post describes the installation and configuration of some of them. I tested them in a Ubuntu 10.10 virtual machine. If you want to try those tools, don’t forget that they can greatly impact the performance of you web server when they are active. Installing a tool like Xdebug on a production server is not recommended.

Benchmark Your Application

First you need to benchmark your application. If you don’t, you won’t be able to know if you improve the performance or degrade it. If you make some changes, a gut feeling that the site is faster is not enough, you need to have numbers. Siege is a load testing tool. It will tell you how many request a second your application handle and the response time.

Installing Siege is pretty straightforward, download it, untar it and compile it with make.

tar -xvf siege-latest.tar.gz
cd siege-2.70/
./configure
make
sudo make install
siege.config

Running siege.config will generate a .siegerc file. You might need to edit it and change where the log file is generated. By default, the logs where stored in /var/siege.log and I didn’t want to run it as root. So I uncommented the logfile line to store it in my home folder. You might want to set verbose to false also.

When Siege is install, you can use the following command to execute it for 30 seconds with 5 concurrent connections.

siege -c 5 -b -t30s http://localhost
** SIEGE 2.70
** Preparing 5 concurrent users for battle.
The server is now under siege...
Lifting the server siege.. done.
Transactions: 143 hits
Availability: 100.00 %
Elapsed time: 29.75 secs
Data transferred: 0.27 MB
Response time: 1.02 secs
Transaction rate: 4.81 trans/sec
Throughput: 0.01 MB/sec
Concurrency: 4.92
Successful transactions: 143
Failed transactions: 0
Longest transaction: 1.54
Shortest transaction: 0.94

The results will be appended to the log file. This makes it easy to compare the numbers after you make some changes. You can also see when the performances really start to degrade if you add users.

Xdebug

Xdebug is a debugger for PHP. I already talked about it in my post on Debugging PHP In Eclipse. It can also be used for profiling. You can install it with Apt.

sudo apt-get install php5-xdebug

To configure it, open the Xdebug ini file, on standard Ubuntu it’s in ‘/etc/php5/conf.d/xdebug.ini’. Add these lines:

xdebug.default_enable=1
xdebug.scream=1
xdebug.profiler_enable=1
xdebug.profiler_output_dir=/tmp/xdebug

This will make sure that Xdebug append a stacktrace to errors. And the scream option will disable the PHP error control operator (@). This way all errors will be displayed. It enable the profiler and change it’s output folder. Make sure the folder exits and that apache can write in it.

When Xdebug is installed and configured, restart Apache and browse to your web application. Xdebug will create some files in the folder you picked. You can then use KCachegrind to view those files. You can install it with this command:

sudo apt-get install kcachegrind

On a Gnome machine, this will install lots of dependencies. But after, you will be able to open the output file from Xdebug. You should get a window that looks like this:

KCachegrind

KCachegrind Output

In KCachegrind you can see where your program spent most of it’s time. How many time a function is called. This information should help identify where you should start optimizing to get the most out of your time.

If you prefer a web front end, you can use Webgrind to inspect the result of Xdebug.

XHProf

Xdebug is a very good tool to check where your bottlenecks are in development. However, you should not use it in production. Facebook developed XHProf a profiling tool that can be used on a production server.

To install it, download the latest version and run those commands:

tar -xvf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension/
phpize
./configure --with-php-config=/usr/bin/php-config5
make
make test
sudo make install

Edit your php.ini and add those line at the end:

[xhprof]
extension=xhprof.so
;
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
;
xhprof.output_dir=/tmp/xhprof

Make sure to change the output dir to something appropriate on your machine.

To profile your application, you now need to add some code at the beginning and at the end of you page execution. Add this at the beginning:

xhprof_enable();

And this at the end:

$data = xhprof_disable();

$XHPROF_ROOT = '/home/testing/Downloads/xhprof-0.9.2';
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

$xhprof_runs = new XHProfRuns_Default();

// Save the run under a namespace "xhprof".
$run_id = $xhprof_runs->save_run($data, "xhprof");

XHGui

XHGui is a tool that allow saving the data from XHProf in a database and displaying it in a nice UI. It also comes with two files that facilitates adding XHProf to your silte. To install it, clone the repository from GitHub. And follow the instructions in the INSTALL file. Here are the instructions for a machine using MySQL for storage.

git clone https://github.com/preinheimer/xhprof.git
cd xhprof
ln -s xhprof_lib/utils/xhprof_runs_mysql.php xhprof_runs.php
mv xhprof_lib/config.sample.php xhprof_lib/config.php

Look in the xhprof_runs.php file. There is a create table query. Create a database for XHGui and execute the query. Edit the file xhprof_lib/config.php and enter the information for your system. Don’t forget to set doprofile to true. Also, you need to uncomment one of the section for the paths where XHGui should put its files. There are default values for Windows and Linux.

XHGui comes with two files that makes profiling easier. A header file to start profiling and a footer that will end it and persist the information in your database. To automatically insert them in all your site, add the following lines to you virtual host:

php_admin_value auto_prepend_file "/home/testing/Downloads/xhprof/external/header.php"
php_admin_value auto_append_file "/home/testing/Downloads/xhprof/external/footer.php"

You will also need to create a virtual host for XHGui. Then after your site have been visited a few times, you can navigate to you XHGui installation and you will have a page that list the last 25 runs of your sites. Pick one and you should see a page like this one.

XHGui Run page

From this page you can view a lot of information about your pages. It contains statistics about the time the page took, the CPU and memory usage and the number of functions calls. At the bottom, there is a list of functions with the same information. It makes it easy for you to see where the page takes the most time or resources. This will help you find targets for optimizations that will really impact the use experience on your site or the machine where it runs. You can click on any function and view a break down of the functions it calls.

On the main page of a run and in the functions pages, you can generate a call graph with just on click.
XHGui Call Graph
It gives you a nice visual of the functions called when you display a page. The functions that can be problematics are in red or yellow, so you can spot the easily.

This Is Just The Beginning

This is only an introduction to some of the tools that are available for PHP developers. I want to play more with them, and maybe post more information when I get more familiar with them. If you don’t do any profiling, you should probably try them too.

One important rule when optimizing, always measure. Benchmark before and after every change to make sure you are going in the right direction.

Update 2011-04-01

php|architect announced yesterday their first Annual Impact Awards. They nominated quite a few amazing projects, the choices are not easy. However, I think XDebug is an outstanding tool that impact the work of many PHP developers every day. Derick Rethans is doing some important work with it. He has my vote for the award. If you are a php| architect subscriber, go cast your vote.

Tags: , , , , , ,

Eric Hogue on February 10th, 2011

I have been programming in PHP for 5 years now. I did not choose this language, I had to learn it to work on the code of the company my bosses bought. Since I started, I heard many rants about how bad PHP is. Some where valid, but a lot of them are just blaming the language for bad code written by bad programmers.

I am by no mean an expert in programming languages or PHP, but here is my take on the language I use every day.

What I Love

The first thing that comes to my mind about PHP is it’s ease of use. When I started PHP, I had experience in C++, Delphi, VB and C#. The low barrier of entry allowed me to be productive in it quickly. I lacked a lot of knowledge, especially in web development, but I was able to maintain the existing code right away. I no time I could add new functionalities to the existing code.

The PHP documentation is an amazing resource. It’s available online, but it can also be downloaded in html of chm format. The manual contains everything there is to know about the language fundamentals. It has descriptions of every functions, their parameters and return values. It also contains examples on almost every pages to help understand what the page is about. The manual is maintain by the community, and most of the pages have notes from users that clarifies the subject even more.

Over the last five years I have become a web developer and PHP is targeted at web development. It can be embedded in html to produce dynamic pages quickly. I know this can lead to spaghetti code, but if used correctly if a powerful tool. PHP has many features that makes the life of web programmers easy. Things like writing and reading cookies, sessions handling and file uploading are easy to do.

PHP also has an interactive shell. I learned about this about a month ago. For a long time I hoped that PHP had one. I used the interactive Ruby prompt, I saw it it other languages and every time I was thinking it was a neat thing. But I never though of looking for one in PHP. Then last month I saw it in the manual and now I feel stupid for never searching for it. It’s a great reminder that I know so little and that because of that I am not always using the best solutions for the problems I have. You cannot use a solution you don’t know about. I don’t use the interactive shell much, but it’s a great tool to make a quick test before implementing something.

The community around PHP is fantastic. It is very friendly and forthcoming. There are many use groups around the world. Here in Montréal we have PHP Québec that organize monthly meetups. Once a year, they hold a family barbecue in a park an they occasional do nights in a bar where we can speak with other developers around a beer. And last year with other Montréal user groups they started the new Confoo conference.

This is just the Montréal PHP community. If you look on the web, the worldwide community is nice and helping. In Coder at Work, Joshua Bloch says that when you choose a language, you choose a community. He compares it to choosing a bar, you want good beer, but you also want it to have nice people that hang around. I did not pick the PHP bar, but I am glad I ended up with that bunch.

What I Hate

PHP has a few fiction point. The first thing that comes to my mind is again the low barrier of entry. This is both a blessing and a curse. Because it’s easy to code in PHP, many peoples who don’t know anything about programming are making PHP web sites. Because of this, there is a tremendous amount of bad PHP code out there. Most of the web pages they build have security vulnerabilities. As a result, many people consider PHP a bad language, and think that all PHP programmers are bad. This is far from the true, there are very good PHP developers out there. And there are bad programmers in every languages. PHP just tends to attract the beginners.

One thing that can be confusing in PHP is that the order of some parameters changes between functions. The needle/haystack example is known to all PHP developers. The strpos function takes the haystack parameter first and the needle second. The array_search takes them in the opposite order. The array_map and array_filter have the same kind of inconsistencies with the callback parameter.

Another annoying things to me is that the constructor of a derived class does not call the constructor of the base class. This can lead to some problems because if you forgot to call it explicitly, the properties of the base class might not be initialized. The base constructor should probably be implicitly called on every instantiation. But we would also need a way to pass parameters to the base class constructor. Maybe something like in C++ would do it.

Conclusion

PHP is not a perfect language, there is no such thing. However, I think it is a very good multi purpose language. I was able to be productive with it in a few days when we inherited from a bad code base. Since then I have learn much about it and I still have a lot to learn. The language keeps improving, with version 5 we got decent object oriented programming. With 5.3, closures and namespaces And with 5.4 we will get traits.

PHP as a lot of detractors, it also has some fan boys, don’t believe any of them. Ask for more details, what they think is so bad or good about it. And go ahead and try it for yourself and make your own opinion.

Tags:

Eric Hogue on February 2nd, 2011

Debugging a PHP application can be painful. When I have a bug in my code I tend to use echo and error_log to pinpoint the source of the problem. Then I can usually look at the code and figure out what my mistake is.

But sometime the ability to step through the code is very useful. Looking at the actual values of variable and the actual path taken can point at some wrong assumptions I have about my code.

There is a very good tutorial that explains how to setup Eclipse for debugging PHP. It’s 35 pages long and very easy to follow. This post is just a summary of how I do it on an Ubuntu box. This way next time I need to set it up I won’t need to go through all the document.

Setup XDebug

Make sure that Xdebug is installed on your machine. Execute the following command and verify that you have at least one row with “xdebug” in it.

php -m | grep xdebug -i

If XDebug is not installed on your system, you can can install it with this command:

sudo apt-get install php5-xdebug

You have to allow remote debugging if you want to debug web pages. Edit /etc/php5/conf.d/xdebug.ini and add those lines:

xdebug.remote_enable=On
xdebug.remote_host="localhost"
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"

Configure Eclipse

Open Eclipse preferences and navigate to “PHP/Debug”. Select XDebug in the PHP Debugger drop down. Go to “PHP/PHP Executables” and make sure that you have an executable configured and that the debugger type is set to XDebug. You might want to have two executables. One to debug web pages and one for cli scripts. Set each one to load the appropriate php.ini.

Since I work on many web applications, I have many vhosts on my machine. So I need to set up multiple servers in Eclipse. Go to “PHP/PHP Servers” and click on New to create a new server. Enter a name for the server and the URL to use and click on finish.

Debugging

When debugging, Eclipse overwrite the include_path from your php.ini. At first I though this was really bad, but after I gave it some though it kind of make sense. This way you don’t have to setup your machine like the server where your code will run. This is useful if your projects will run on different severs that can have different configurations.

You need to set up the include_path for each project. Go to the project properties and click on “PHP Include Path”. From there you will be able to add path from the project, others Eclipse projects or paths on your machine.

Then you can go to “Run/Debug Configurations” and create a new configuration. For a PHP script, pick the correct executable, choose the file to debug and you’re set.

For web pages, choose the XDebug debugger, the server to use and the file to launch. Eclipse will generate a URL for you, but you can change it.

That’s it, now you can add breakpoints and step through your code. You can inspect and change the value of your variables. Debugging like this can be a very powerful tools. But you still need to know your code base if you want to know where to look for because stepping through thousands of line of code can be very long and tedious.

Sources

Update 2011-11-07

One problem you can encounter while debugging in Eclipse is that it does not take into account the files in ‘/etc/php5/conf.d’. This is because Eclipse use the -n option when calling PHP. This tell PHP not to use the php.ini. You can avoid this by creating a shell script that removes the option. I found this great answer on Stack Overflow.

Tags: , , ,

Eric Hogue on January 27th, 2011

Wednesday I went to an event called devLAB Montréal. This is a weekly event that is held in Evocatio offices in the old Montréal. It was my first time there, but it was the third time they held it.

What’s devLAB Montréal?

devLAB idea is to get passionate programmers together and let them work on a small project. This idea is really appealing to me. I see this as a great way to work on something I would not normally at work. It also removes all times constraint. In a setup like this we don’t have to deliver anything. So we can take our time to experiment and try new things. It’s also a great way to work with other developers and learn from them.

How It Went

The event was not quite what I was expected. From what I read and heard from it I though we where going to decide on a project or two and work on them. I know they have a list of projects they want to try from previous meetups. That’s not what happened. Basically, we all sit there and played with different things. Some played with Symfony, some with AdWords. I took the opportunity to try Node.js with another developer that was there. It was fun and a great learning experience, but time went by too fast. I definitely need to dig more in Node.

It’s A Great Event

I liked the time I spent there with this bunch of passionate peoples. I can’t be there every week, I have a family and there are other great tech events in Montréal, but I will go back as often as I can. I want to see how it will evolved. What I would like is to pair with others on a small projects. Something that is different from my day to day. The good news is that they look very open to ideas.

I would really like to thank Evocatio for letting us use their office and making this kind of events possible.

Tags: , , ,

Eric Hogue on January 25th, 2011

Last week I read the book Driving Technical Change by Terrence Ryan. This is a small book. I took me only 4 days to read it. Here’s what I thought of it.

The book is divided into four parts. The first part is an introduction, the second part describes the types of skeptics, the third part the techniques and the last part describe strategies to put it all together.

The Introduction

In the introduction the author explain why the book is necessary. There are also two chapters on defining the problem with try to solve and making sure that the solution we try to push is really relevant to the problem we face. This is probably one of the most important part of the book. Programmers love to learn new things and play with new toys. So we have to make sure that we are not trying to push one of those new toy just for the fun of it. If the solution is not appropriate to the problem we face, it could be bad for the project. It can also damage our reputation and get our team to believe the tool is bad because it was use in the wrong project.

The Skeptics

In the second part of the book, the author describes the different types of skeptics we might encounter when trying to get our team to use a new technique. For each type, he gives an example of how they manifest themselves. The examples are little exaggerations, but they are meant to help us recognize the skeptics when facing them. He also says that someone can fit multiple patterns, he can also change type from a project to another one.

The Techniques

This part is the reason why I bought the book. These are the actual techniques to convinced others to accept the new technology. For each techniques, the author explain how to use it and gives examples of the technique applied. He explains why the technique works and on which type of skeptics it is efficient. He also list the pitfalls of using the techniques and how to avoid them. Then he ends the chapter with tips on how to put the technique in practice.

The strategy

This is where the author puts it all together. Because in any team you might encounter many type of skeptics, and the techniques do not work on all types, you need to focus your actions. Here the author describes which skeptics you should try to persuade first. He then explain how you can use them to help you convince the others and get your changes accepted.

My Take On The Book

I really enjoyed this book. The author uses a lot of story telling. The stories are entertaining and they illustrate the point he is making in the chapter. They help visualize the skeptics and the techniques to counter them.

As someone who tried many time to bring innovations at works, and failed most of the time, I believe this book might help me improve. After all, when I fail to convince my teammates and my boss to use something that will help us deliver a better product, I fail at doing my job. As a developer it’s my job to produce the best possible product in a timely manner. And if I know of a new way that will help my team make a better job but I can’t get it adopt, I don’t do a good job.

Knowing the skeptic types will also help me avoid being one of them. I can use this knowledge to see if I oppose an idea for good reasons or if I am just someone else skeptic.

Tags:

Eric Hogue on January 20th, 2011

About a month ago, a very animated discussion took place on the Software Craftsmanship Google Group. The debate was about studying at home vs spending time with the family.

I know I am late in the discussion, but this is a subject that is important to me, so I wanted to add my two cents.

The Family Is Important

No one is denying that our families are important. However, some people seems to think that those who practice, and try to improve as developers are neglecting their family. This is far from the truth for me and for many more I’m sure. My family is very important to me. My girlfriend and my kids are what’s driving me to improve. I love them very much and I want to spend as much time as I can with them. Because contrary to what some would have you believe quantity is as important as quality in that matter. Quality time does not happen on a schedule, you have to be there to experience it.

Learning Is Important

As developer, self improvement is a must. There are lots of us who don’t make the effort of learning new things and practicing. They can have fulfilling career, but for some it’s not enough. Every day I try to learn new things. And every day I find things that I don’t know. I want to become a master in my profession and the more I learn, the more I find out that I have a long road ahead.

It’s possible to learn on the job, but it’s hard. In most companies, there is no time to experiment. There are deadlines to meet. At home, you can decide what you want to learn. You set the time frame. It doesn’t have to be all in one week. You can take your time, learn at your pace. Create toy projects and throw them when you’re done. And if you discover something great that could help you in your job, you now have some expertise on the subject. That should help you sell that new technique to your colleagues and managers.

We Can Have Both

No one have to pick between their family and self improvement. You just have to manage your time wisely. To me, it means leaving the office at 5pm. Then I can have a nice diner with the family. Give my son his bath, play with the kids and watch tv with them. Then after the kids are in bed, I have an hour or two to read, program and try new things. I also have some time early in the weekends.

Do I have to make sacrifices? Of course. Time is limited. When my son was born I had to give up the gym. It was a hard thing to do. I use to love going to the gym in the morning. But I picked my priorities and I had to let go of something. In the end I spend my time doing what makes me happy in life. Spending time with my family and learning to become a better developer.

Tags: