Flickr: The Real World PHP 5 Benchmark

Sebastian Bergmann » 26 March 2009 » in Benchmarks » 1 Comment

Benchmarks such as the PHP / GCC / ICC Benchmark I posted quite a while ago on this blog are synthetic. They test "raw bytecode execution" speed that cannot be translated into real-world situations per se.

Flickr recently migrated from PHP 4 to PHP 5. And here are their real world numbers:

Drop in CPU usage after migration of Flickr from PHP 4 to PHP 5

Wow.

I am looking forward to John Allspaw's slides from his presentation on "operational efficiency hacks" that he will be giving at next week's Web2.0 Expo.

Defined tags for this entry: , ,

Quality Assurance in PHP Projects

Sebastian Bergmann » 18 March 2009 » in Presentations » 1 Comment

Defined tags for this entry: , ,

Quality Assurance Tools for PHP

Sebastian Bergmann » 15 March 2009 » in Articles » 20 Comments

Map of PHP QA Tools

The map above contains the following tools that are useful for quality assurance in PHP projects:

  • PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.
  • phpmd scans PHP source code and looks for potential problems such as possible bugs, suboptimal code or overcomplicated expressions.
  • phpcpd is a Copy/Paste Detector (CPD) for PHP code.
  • PHP_Depend is a PHP software metrics tool.
  • PHPUnit is the de-facto standard unit test framework for PHP.

Build Automation is the act of scripting or automating a wide variety of tasks that a software developer will do in their day-to-day activities, including tasks that involve the tools listed above.

Apache Ant is a Java-based build tool that is similar to make, but without make's wrinkles. Its build files are XML-based, calling out a target tree where various tasks get executed.

The listing below shows an Apache Ant build file for a project named Money.

<project name="Money" default="build">
 <target name="clean">
  <delete dir="${basedir}/build"/>
 </target>

 <target name="prepare">
  <mkdir dir="${basedir}/build/logs"/>
 </target>

 <target name="phpcs">
  <exec dir="${basedir}"
        executable="phpcs"
        output="${basedir}/build/logs/checkstyle.xml"
        failonerror="false">
   <arg line="--report=checkstyle ."/>
  </exec>
 </target>

 <target name="phpmd">
  <exec dir="${basedir}"
        executable="phpmd"
        failonerror="false">
   <arg line=". xml codesize
              --reportfile ${basedir}/build/logs/pmd.xml"/>
  </exec>
 </target>

 <target name="phpcpd">
  <exec dir="${basedir}"
        executable="phpcpd"
        failonerror="false">
   <arg line="--log-pmd=${basedir}/build/logs/pmd-cpd.xml ."/>
  </exec>
 </target>

 <target name="pdepend">
  <exec dir="${basedir}"
        executable="pdepend"
        failonerror="false">
   <arg line="--jdepend-xml=${basedir}/build/logs/jdepend.xml ."/>
  </exec>
 </target>

 <target name="phpunit">
  <exec dir="${basedir}"
        executable="phpunit"
        failonerror="true">
   <arg line="--log-xml         ${basedir}/build/logs/junit.xml
              --coverage-clover ${basedir}/build/logs/clover.xml
              MoneyTest"/>
  </exec>
 </target>

 <target name="build"
         depends="clean,prepare,phpcs,phpmd,phpcpd,pdepend,phpunit"/>
</project>

The main target, build, depends on the targets that

  1. Delete the build directory, if it exists.
  2. Prepare the build directory, if it does not exist.
  3. Run PHP_CodeSniffer on the project's sourcecode and write a logfile in Checkstyle XML format.
  4. Run phpmd on the project's sourcecode and write a logfile in PMD XML format.
  5. Run phpcpd on the project's sourcecode and write a logfile in PMD-CPD XML format.
  6. Run PHP_Depend on the project's sourcecode and write a logfile in JDepend XML format.
  7. Run the project's tests using PHPUnit and write logfiles in JUnit XML and Clover XML format.

Below is the output for invoking Apache Ant in the project directory:

sb@ubuntu Money % ant
Buildfile: build.xml

clean:
   [delete] Deleting directory /home/sb/Money/build

prepare:
    [mkdir] Created dir: /home/sb/Money/build/logs

phpcs:

phpmd:

phpcpd:
     [exec] phpcpd 1.1.0 by Sebastian Bergmann.
     [exec] 
     [exec] 0.00% duplicated lines out of 722 total lines of code.

pdepend:
     [exec] PHP_Depend 0.9.4 by Manuel Pichler
     [exec] 
     [exec] Executing Dependency-Analyzer:
     [exec]                                                                 16
     [exec] 
     [exec] Generating pdepend log files, this may take a moment.

phpunit:
     [exec] PHPUnit 3.4.0 by Sebastian Bergmann.
     [exec] 
     [exec] ......................
     [exec] 
     [exec] Time: 0 seconds
     [exec] 
     [exec] OK (22 tests, 34 assertions)
     [exec] 
     [exec] Writing code coverage data to XML file, this may take a moment.

build:

BUILD SUCCESSFUL
Total time: 4 seconds

The generated logfiles can be processed by CruiseControl because it already knows the XML formats used. phpUnderControl is a customization of CruiseControl that caters to the needs of PHP projects and makes a lot of things easier.

Sonar enables to collect, analyze and report metrics on source code. It not only offers consolidated reporting on and across projects throughout time, but it becomes the central place to manage code quality. The developers of Sonar are working on out-of-the-box support for PHP projects.

Speaking at OSCON 2009

Sebastian Bergmann » 10 March 2009 » in Events » 0 Comments

OSCON 2009

I will present the following sessions at this year's O'Reilly Open Source Convention (OSCON):

Workshop: Quality Assurance in PHP Projects

Now that we know how to build applications with PHP that “just work”, are fast and scalable, as well as secure, the next logical step is to implement processes and use techniques that help us assure that the software works correctly throughout the software’s lifecycle.

This tutorial introduces the audience to the testing of modern web applications using PHPUnit for testing the backend components and Selenium for end-to-end testing of the whole application.

But testing is only one aspect of controlling the quality of a software project. This is why concepts such as continuous integration and software metrics as well as tools such as PHP_CodeSniffer and PHP_Depend are also covered in the tutorial.

Untestable Code

How do you write untestable code and anger an ancient goddess? These and other questions will guide us while we discuss testability, an often forgotten attribute of software design and quality.

Starting from untestable code fragments, the audience will learn why the code is untestable and how it can be refactored for testability.

See you in San Jose, CA in July!

Defined tags for this entry: ,

Do Not Micro-Optimize

Sebastian Bergmann » 10 March 2009 » in PHP » 5 Comments

Whenever I read blog postings such as that one (in the spirit of John McCain's that one reference to Barack Obama) I get a little bit angry.

"calling a static method is faster than an object method"

It is called Object-Oriented Programming. Not "Class-Oriented Programming". Nor "Procedural Progamming with Classes as Namespaces".

The small performance gain you may or may not get at deployment-time by using static method calls instead of using instance method calls comes at the price of problems at development-time.

One of the problems at development-time that you are digging yourself into is that of untestable code: static methods are death to testability. Use them when it makes sense to use them and not because you think you can improve the performance of your website by using them.

I bet that your problems lie elsewhere when you think that you can solve them by a micro-optimization such as this. Have a look at Ilia's Common Optimization Mistakes slides for more information on this topic.

Defined tags for this entry: , ,

PHP Code Review

Sebastian Bergmann » 09 March 2009 » in Events » 2 Comments

Last week, Stefan Priebsch and myself gave a presentation titled "PHP Code Review" the the Conférence PHP Québec 2009.

Not only did we receive great feedback from our audience in Montreal ("This presentation should always be at every PHP conference." was one of the many positive remarks we got), but these slides also prompted quite a few questions on Twitter and SlideShare due to the fact that we did not publish any comments alongside the code examples.

Just like during the presentation, we wanted to let the code speak for itself. In the presentation this meant that the audience commented on the code and pointed out flaws in it. Together with the audience we then went on and discussed how to improve the code with regard to aspects such as maintainability, testability, and security.

Together with Arne Blankerts, the third Co-Founder and Principal Consultant of thePHP.cc - The PHP Consulting Company, we will give an updated version of this presentation at php|tek 2009 and are planning to publish a series of blog postings that will discuss a specific code example each.

Defined tags for this entry: ,