The way I see it Website development (PHP, JS, MySQL) by Damian Sromek

3Jan/119

[How to] Run PHPUnit tests using database 10x faster

Overview

PHPUnit tests that are heavily using the database can run like 10x faster when you run the database from the RAMDisk.
It's the easiest way I know to improve the speed of the tests execution.

Problem - PHPUnit tests can run long time when they are using the database

Project I'm contributing at my work - License Statistics - uses quite a lot of unit tests.
One of the reasons is that we try to do the Test Driven Development (TDD). But this method of creating the software has some drawbacks.

Right now we have something around 1 000 tests that check around 3 000 assertions. Much of them involve database operations. That's why running all tests on Windows machine can take up to 25 minutes.

Tests on Linux machine are much faster - around 7-10 minutes but we still need to run tests under Windows environment due to fact we have to support both platforms and we have to be sure that both of them work perfectly fine.

3Jan/110

[Watch out] Zend_Date performance

Problem - Zend_Date offers small performance

I always try to use objects when I see they are giving me more flexibility than arrays or primitive types.

During my work often I have to operate on dates so I use Zend_Date quite a lot - our project is based on the Zend Framework. But recently I've run a profiler for one of the pages I created and I saw huge impact on the performance that Zend_Date had.

It did speed up development and made it easier but it also made the code much slower. Zend_Date Comparing with the plain PHP DateTime was like 5 to 10 times slower. Even Zend_Date::toString took quite large amount of CPU time.

Solution - use PHP DateTime instead of Zend_Date

Solution for that part of code was replacing the Zend_Date used in the loops (executed even couple hundred times) with DateTime. It was very simple. Also date operations like adding one day is very easy when we have DateTime object - we just need to pass a DateInterval into DateTime::add().

I'm still using Zend_Date for example as the method parameter but in code where I do many date operations I replace it with the DateTime. Thanks to that my scripts are executed much faster.

Some of DateTime features are available since PHP > 5.3 but you can easily use DateTime with older PHP doing small changes in the code.