Sunday 12 April 2020

Benchmark between Symfony DependencyInjection ContainerBuilder AND League Container

<?php

require_once 'vendor/autoload.php';
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;


class Bar {}

class Foo
{
    public $bar;

    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }
}

   
$bench = new Ubench;

$bench->start();


$container = new League\Container\Container;

$container->add(\Foo::class)->addArgument(\Bar::class);
$container->add(\Bar::class);

$foo = $container->get(\Foo::class);

var_dump($foo instanceof \Foo);      // true
var_dump($foo->bar instanceof \Bar); // true
$bench->end();
echo '<br>';
echo '<br>';
echo '<br>';
echo '<br>League';
// Get elapsed time and memory
echo  '<br>' . $bench->getTime() . '<br>'; // 156ms or 1.123s
echo $bench->getTime(true) . '<br>'; // elapsed microtime in float

echo $bench->getMemoryPeak() . 'Peak memory<br>';

echo $bench->getMemoryPeak(true) . '<br>'; // memory peak in bytes

// Returns the memory usage at the end mark
echo $bench->getMemoryUsage() . '<br>';


echo '<br>';
echo '<br>';
echo '<br>';
echo '<br>Symfony';
$bench->start();

$containerBuilder = new ContainerBuilder();
$containerBuilder
    ->register('bar', 'Bar');


$containerBuilder
    ->register('foo', 'Foo')
    ->addArgument(new Reference('bar'));

$foo = $containerBuilder->get('foo');

var_dump($foo instanceof \Foo);      // true
var_dump($foo->bar instanceof \Bar); // true
$bench->end();

// Get elapsed time and memory
echo  '<br>' . $bench->getTime() . '<br>'; // 156ms or 1.123s
echo $bench->getTime(true) . '<br>'; // elapsed microtime in float

echo $bench->getMemoryPeak() . 'Peak memory<br>';

echo $bench->getMemoryPeak(true) . '<br>'; // memory peak in bytes

// Returns the memory usage at the end mark
echo $bench->getMemoryUsage() . '<br>';

 Result:

League

C:\wamp64\www\mecrud\index1.php:33:boolean true
C:\wamp64\www\mecrud\index1.php:34:boolean true

6ms
0.0061249732971191
Peak memory : 2097152

Symfony

C:\wamp64\www\mecrud\index1.php:68:boolean true
C:\wamp64\www\mecrud\index1.php:69:boolean true

6ms
0.0063478946685791
Peak memory: 2097152



Composer require

 "require": {
        "php""^7.2.5",

        "league/container""^3.3",
        "symfony/dependency-injection""^5.0"
    },
    "require-dev": { 
        "devster/ubench""~2.0.0"
    },


Multiple run gives mixed result. I decided to use league (https://container.thephpleague.com/).

No comments:

Post a Comment