Skip to content

Instantly share code, notes, and snippets.

@kanian
Last active March 21, 2019 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanian/6bb8d87dcb1a2436ebfb8e53b9199a5f to your computer and use it in GitHub Desktop.
Save kanian/6bb8d87dcb1a2436ebfb8e53b9199a5f to your computer and use it in GitHub Desktop.
Deep object graph resolution performance test
<?php
require __DIR__ . '/vendor/autoload.php';
use Kanian\ContainerX\ContainerX;
class A
{
public function __construct()
{
}
}
class B extends A
{
public function __construct(A $a)
{
}
}
class C extends B
{
public function __construct(B $b)
{
}
}
class D extends C
{
public function __construct(C $c)
{
}
}
class E extends D
{
public function __construct(D $d)
{
}
}
class F extends E
{
public function __construct(E $e)
{
}
}
class G extends F
{
public function __construct(F $f)
{
}
}
class H extends G
{
public function __construct(G $g)
{
}
}
class I extends H
{
public function __construct(H $h)
{
}
}
class J extends I
{
public function __construct(I $i)
{
}
}
$c = new ContainerX();
$c['A'] = function ($c = null) {
return new A;
};
$c['B'] = function ($c = null) {
return new B($c['A']);
};
$c['C'] = function ($c = null) {
return new C($c['B']);
};
$c['D'] = function ($c = null) {
return new D($c['C']);
};
$c['E'] = function ($c = null) {
return new E($c['D']);
};
$c['F'] = function ($c = null) {
return new F($c['E']);
};
$c['G'] = function ($c = null) {
return new G($c['F']);
};
$c['H'] = function ($c = null) {
return new H($c['G']);
};
$c['I'] = function ($c = null) {
return new I($c['H']);
};
$c['J'] = function ($c = null) {
return new J($c['I']);
};
// Let loading happen to ensure that any required classes were autoloaded before measuring the time.
$u = $c['J'];
unset($u);
$t1 = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$u = $c['J'];
}
$t2 = microtime(true);
$results = [
'time' => $t2 - $t1,
'files' => count(get_included_files()),
'memory' => memory_get_peak_usage() / 1024 / 1024,
];
echo json_encode($results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment