Skip to content

Commit

Permalink
Merge pull request #11 from charonlab/phpbench
Browse files Browse the repository at this point in the history
Migrate to PHPBench for performance tests
  • Loading branch information
nuldark committed Mar 6, 2024
2 parents 1b88b5f + 10d627d commit ebfec3c
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 112 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/phpbench.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "Performance benchmark"

on:
pull_request:
branches:
- "*.x"
paths:
- composer.*
- phpbench.json
- src/**
- tests/**
push:
branches:
- "*.x"
paths:
- composer.*
- phpbench.json
- src/**
- tests/**

jobs:
phpbench:
name: "PHPBench"
runs-on: "ubuntu-latest"

strategy:
matrix:
php-version:
- "8.3"

steps:
- uses: "actions/checkout@v4"
with:
fetch-depth: 0
- uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"
coverage: "pcov"
ini-values: "zend.assertions=1, apc.enable_cli=1"
- uses: "ramsey/composer-install@v3"
- name: "Run PHPBench"
run: "composer run phpbench"
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"require-dev": {
"charonlab/coding-standard": "1.2.x-dev",
"phpbench/phpbench": "^1.2@dev",
"phpunit/phpunit": "11.1.x-dev",
"vimeo/psalm": "6.x-dev"
},
Expand Down Expand Up @@ -40,6 +41,7 @@
"lint-check": "phpcs",
"lint-fix": "phpcbf",
"static-analysis": "psalm --shepherd --stats",
"test": "phpunit --colors=always"
"test": "phpunit --colors=always",
"phpbench": "phpbench run --report=compressed"
}
}
22 changes: 22 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json",

"runner.bootstrap": "./vendor/autoload.php",
"runner.path": "tests/Performance",
"runner.progress": "plain",
"runner.iterations": 20,
"runner.revs": 1000,
"runner.file_pattern": "*Bench.php",
"report.generators": {

"compressed": {
"title": "Charon Container Benchmark Tests",
"generator": "expression",
"cols": [ "benchmark", "subject", "mem_peak", "mode", "mean", "best", "worst" ]
}
},

"core.extensions": [
"PhpBench\\Extensions\\XDebug\\XDebugExtension"
]
}
53 changes: 0 additions & 53 deletions tests/Benchmark/ContainerPerformanceTest.php

This file was deleted.

48 changes: 0 additions & 48 deletions tests/Benchmark/MemoryUsageTest.php

This file was deleted.

29 changes: 29 additions & 0 deletions tests/Performance/AbstractBenchCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\Container\Performance;

use Charon\Container\Container;
use Charon\Container\ContainerInterface;

class AbstractBenchCase
{
/** @psalm-suppress PropertyNotSetInConstructor */
protected ContainerInterface $container;

public function setup(): void {
$this->container = new Container();
}

public function tearDown(): void {
unset($this->container);
}
}
29 changes: 29 additions & 0 deletions tests/Performance/Cases/ContainerBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\Container\Performance\Cases;

use Charon\Tests\Container\Performance\AbstractBenchCase;
use Charon\Tests\Container\Performance\Stubs\FooBar;
use PhpBench\Attributes\AfterMethods;
use PhpBench\Attributes\Assert;
use PhpBench\Attributes\BeforeMethods;

class ContainerBench extends AbstractBenchCase
{
#[BeforeMethods('setup')]
#[AfterMethods('tearDown')]
#[Assert('mode(variant.time.avg) < 5ms')]
#[Assert('mode(variant.mem.real) < 10mb')]
public function benchResolvePerformance(): void {
$this->container->resolve(FooBar::class);
}
}
16 changes: 16 additions & 0 deletions tests/Performance/Stubs/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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\Container\Performance\Stubs;

class Bar
{
}
16 changes: 16 additions & 0 deletions tests/Performance/Stubs/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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\Container\Performance\Stubs;

class Foo
{
}
21 changes: 21 additions & 0 deletions tests/Performance/Stubs/FooBar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\Container\Performance\Stubs;

class FooBar
{
public function __construct(
public Foo $foo,
public Bar $bar
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
* of the MIT license. See the LICENSE.md file for details.
*/

namespace Charon\Tests\Container;
namespace Charon\Tests\Container\Unit;

use Charon\Container\BindableContainerInterface;
use Charon\Container\Container;
use Charon\Container\Exception\CircularDependencyException;
use Charon\Tests\Container\Fixtures\ClassACircularDependencies;
use Charon\Tests\Container\Fixtures\SampleClass;
use Charon\Tests\Container\Fixtures\SampleInterface;
use Charon\Tests\Container\Unit\Stubs\ClassACircularDependencies;
use Charon\Tests\Container\Unit\Stubs\SampleClass;
use Charon\Tests\Container\Unit\Stubs\SampleInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

Expand Down
2 changes: 1 addition & 1 deletion tests/ContainerTest.php → tests/Unit/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* of the MIT license. See the LICENSE.md file for details.
*/

namespace Charon\Tests\Container;
namespace Charon\Tests\Container\Unit;

use Charon\Container\Container;
use Charon\Container\Exception\NotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* of the MIT license. See the LICENSE.md file for details.
*/

namespace Charon\Tests\Container\Fixtures;
namespace Charon\Tests\Container\Unit\Stubs;

class ClassACircularDependencies
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* of the MIT license. See the LICENSE.md file for details.
*/

namespace Charon\Tests\Container\Fixtures;
namespace Charon\Tests\Container\Unit\Stubs;

class ClassBCircularDependencies
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* of the MIT license. See the LICENSE.md file for details.
*/

namespace Charon\Tests\Container\Fixtures;
namespace Charon\Tests\Container\Unit\Stubs;

class SampleClass implements SampleInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* of the MIT license. See the LICENSE.md file for details.
*/

namespace Charon\Tests\Container\Fixtures;
namespace Charon\Tests\Container\Unit\Stubs;

interface SampleInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* of the MIT license. See the LICENSE.md file for details.
*/

namespace Charon\Tests\Container\Fixtures;
namespace Charon\Tests\Container\Unit\Stubs;

class Service
{
Expand Down

0 comments on commit ebfec3c

Please sign in to comment.