-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the charonlab/charon-container. | ||
* | ||
* Copyright (C) 2023-2024 Charon Lab Development Team | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE.md file for details. | ||
*/ | ||
|
||
namespace Charon\Tests\Performance; | ||
|
||
use Charon\Container\Container; | ||
use Charon\Container\ContainerInterface; | ||
use Charon\Tests\Performance\Fixture\Service; | ||
use PHPUnit\Framework\Attributes\CoversNothing; | ||
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; | ||
use PHPUnit\Framework\Attributes\Group; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[Group('performance')] | ||
#[CoversNothing] | ||
class ContainerPerformanceTest extends TestCase | ||
{ | ||
private ?ContainerInterface $container = null; | ||
|
||
public function setUp(): void { | ||
$this->container = new Container(); | ||
$this->container->set(\stdClass::class, new \stdClass()); | ||
$this->container->factory('factory', fn (ContainerInterface $container) => $container); | ||
$this->container->factory('fixture', function (ContainerInterface $container): Service { | ||
return new Service( | ||
$container->get(\stdClass::class) | ||
); | ||
}); | ||
} | ||
|
||
#[DoesNotPerformAssertions] | ||
public function testContainerGetPerformance(): void { | ||
$start = \microtime(true); | ||
|
||
for ($i = 1; $i <= 20000; $i++) { | ||
$this->container?->get('fixture'); | ||
} | ||
|
||
echo 'Get 20000 objects in ' . (\microtime(true) - $start) . ' seconds' . PHP_EOL; | ||
} | ||
|
||
public function tearDown(): void { | ||
$this->container = null; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the charonlab/charon-container. | ||
* | ||
* Copyright (C) 2023-2024 Charon Lab Development Team | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE.md file for details. | ||
*/ | ||
|
||
namespace Charon\Tests\Performance\Fixture; | ||
|
||
class Service | ||
{ | ||
public function __construct(private readonly \stdClass $stdClass) { | ||
} | ||
|
||
public function value(): \stdClass { | ||
return $this->stdClass; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the charonlab/charon-container. | ||
* | ||
* Copyright (C) 2023-2024 Charon Lab Development Team | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE.md file for details. | ||
*/ | ||
|
||
namespace Charon\Tests\Performance; | ||
|
||
use Charon\Container\Container; | ||
use Charon\Container\ContainerInterface; | ||
use Charon\Tests\Performance\Fixture\Service; | ||
use PHPUnit\Framework\Attributes\CoversNothing; | ||
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; | ||
use PHPUnit\Framework\Attributes\Group; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[Group('performance')] | ||
#[CoversNothing] | ||
class MemoryUsageTest extends TestCase | ||
{ | ||
#[DoesNotPerformAssertions] | ||
public function testMemoryUsage(): void { | ||
$container = new Container(); | ||
$container->set(\stdClass::class, new \stdClass()); | ||
$container->factory('factory', function (ContainerInterface $container) { | ||
return new Service( | ||
$container->get(\stdClass::class) | ||
); | ||
}); | ||
$memoryUsage = []; | ||
for ($i = 0; $i < 200; $i++) { | ||
$container->get('factory'); | ||
|
||
\gc_collect_cycles(); | ||
$memoryUsage[] = \memory_get_usage(); | ||
} | ||
|
||
$start = \current($memoryUsage); | ||
$end = \end($memoryUsage); | ||
|
||
echo \sprintf('Memory increased by %s', ($end - $start) / 1024 . ' KB') . PHP_EOL; | ||
} | ||
} |