A PHP 5.4 library for refactoring, performance and issue testing new/old code in your production projects.
For example, let's pretend you're changing caching backend. The following code example will help you to test and compare performance of new code under load.
$experiment = new \FeatureScience\Test('cacher.save', [
'control' => function() { $this->filecache->save() }, // old code
'candidate' => function() { $this->memcached->save() }, // new code
]);
$experiment->run();
$experiment->run()
will randomly select one of the testing subjects from array, run it and return result. Behind the scenes Test::run
will collect duration, memory usage, exceptions of both behaviors and save it to the storage
.
To not load server by writing files each request, FeatureScience will store temporary data in APC storage and after experiment has been run 100 times, will save its results to the specified directory. You can change this limit by calling $experiment->setPayloadLimit(number).
By default payload.saver
saves results into system temp dir.
But you can configure your own path.
Note: Remember to make that path writable by the web server.
use FeatureScience\PayloadSaver;
\FeatureScience\DI::set('payload.saver', new PayloadSaver('/path/to/save/results'));
You can view with your favorite editor or by typing command:
vendor/bin/feature-science /path/to/feature.name.json
It will look like:
{
"name":"array.performance",
"control":{
"duration":0.01359,
"exception":null
},
"candidate":{
"duration":0.00261,
"exception":{
"message": "Ooops! Something went wrong",
"code": "503",
"file": "test.php",
"line": "27"
}
}
}
Add FeatureScience to composer.json and run installation.
{
"require": {
"firstrow/feature-science": "dev-master"
}
}
Each benchmark runs test code 1000 times.
php benchmarks/clean.php 13.37s
php benchmarks/experiment.php 13.80s
So, its around +0.4s total execution time for each 1000 requests or 0.0004ms per one request.
- Track memory usage
- Save min, avg, max duration
Inspired by ruby gem dat-science
Also, you may find useful athletic