Created
October 4, 2012 11:46
-
-
Save igorw/3833123 to your computer and use it in GitHub Desktop.
Benchmark: Pimple vs Symfony/DependencyInjection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"pimple/pimple": "1.0.*", | |
"symfony/dependency-injection": "2.1.*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./run-tests.sh | |
Pimple | |
265K | |
579K | |
0.00086s | |
DependencyInjection | |
267K | |
931K | |
0.006124s | |
DependencyInjection (dumped) | |
688K | |
738K | |
0.000495s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
FLAGS="-d xdebug.max_nesting_level=300" | |
echo "Pimple" | |
php $FLAGS test-pimple.php | |
echo "DependencyInjection" | |
php $FLAGS test-di.php | |
echo "DependencyInjection (dumped)" | |
php $FLAGS test-di-dumped.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Service { | |
private $service; | |
private $param; | |
public function __construct(Service $service, $param) | |
{ | |
$this->service = $service; | |
$this->param = $param; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
require 'service.php'; | |
require 'dumped.php'; | |
$start = microtime(true); | |
echo intval(memory_get_usage() / 1024).'K'."\n"; | |
$container = new ProjectServiceContainer(); | |
echo intval(memory_get_usage() / 1024).'K'."\n"; | |
echo round(microtime(true) - $start, 6).'s'."\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
require 'service.php'; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\DependencyInjection\Reference; | |
$start = microtime(true); | |
echo intval(memory_get_usage() / 1024).'K'."\n"; | |
$container = new ContainerBuilder(); | |
$container->setParameter('param', 'foo'); | |
$container | |
->register('service_1', 'Service') | |
->addArgument(null) | |
->addArgument('%param%'); | |
foreach (range(2, 200) as $i) { | |
$container | |
->register('service_'.$i, 'Service') | |
->addArgument(new Reference('service_'.($i - 1))) | |
->addArgument('%param%'); | |
} | |
echo intval(memory_get_usage() / 1024).'K'."\n"; | |
echo round(microtime(true) - $start, 6).'s'."\n"; | |
use Symfony\Component\DependencyInjection\Compiler\Compiler; | |
$compiler = new Compiler(); | |
$compiler->compile($container); | |
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; | |
$dumper = new PhpDumper($container); | |
file_put_contents('dumped.php', $dumper->dump()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
require 'service.php'; | |
$start = microtime(true); | |
echo intval(memory_get_usage() / 1024).'K'."\n"; | |
$container = new Pimple(); | |
$container['param'] = 'foo'; | |
$container['service_0'] = null; | |
foreach (range(1, 200) as $i) { | |
$container['service_'.$i] = $container->share(function ($container) use ($i) { | |
return new Service($container['service_'.($i - 1)], $container['param']); | |
}); | |
} | |
echo intval(memory_get_usage() / 1024).'K'."\n"; | |
echo round(microtime(true) - $start, 6).'s'."\n"; |
and this does not benchmark the use of the container, only its creation
Compilation is an optional step. I did not want to benchmark it in this case. But fair enough, benchmarking with compilation and actually accessing a service would be interesting things to benchmark as well.
which container should I choose?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code for the DI test is wrong. It does not run the compiler passes in the benchmark code, thus taking into account only part of the building