Skip to content

Commit

Permalink
feat: add some performance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nulxrd committed Jan 28, 2024
1 parent a213bd9 commit df2aca5
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"autoload-dev": {
"psr-4": {
"Charon\\Tests\\Unit\\": "tests/unit/"
"Charon\\Tests\\Unit\\": "tests/unit/",
"Charon\\Tests\\Performance\\": "tests/performance/"
}
},
"config": {
Expand Down
53 changes: 53 additions & 0 deletions tests/performance/ContainerPerformanceTest.php
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;
}
}
22 changes: 22 additions & 0 deletions tests/performance/Fixture/Service.php
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;
}
}
48 changes: 48 additions & 0 deletions tests/performance/MemoryUsageTest.php
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;
}
}

0 comments on commit df2aca5

Please sign in to comment.