Passed
Push — feature/gacela-cached-class-na... ( 44c645 )
by Chema
26:13
created

ClassNameCacheBench::bench_without_cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Benchmark\Framework\ClassResolver\ClassNameCache;
6
7
use Gacela\Framework\Bootstrap\GacelaConfig;
8
use Gacela\Framework\Gacela;
9
use GacelaTest\Fixtures\StringValue;
10
use GacelaTest\Fixtures\StringValueInterface;
11
12
final class ClassNameCacheBench
13
{
14
    public function bench_with_cache(): void
15
    {
16
        $this->gacelaBootstrapWithCache(true);
17
        $this->loadAllModules();
18
    }
19
20
    public function bench_without_cache(): void
21
    {
22
        $this->gacelaBootstrapWithCache(false);
23
        $this->loadAllModules();
24
    }
25
26
    private function gacelaBootstrapWithCache(bool $withCache): void
27
    {
28
        Gacela::bootstrap(__DIR__, static function (GacelaConfig $config) use ($withCache): void {
29
            $config->addAppConfig('config/*.php');
30
            $config->setResetCache(!$withCache);
31
32
            $config->addMappingInterface(StringValueInterface::class, new StringValue('testing-string'));
33
34
            $config->addSuffixTypeFactory('FactoryA');
35
            $config->addSuffixTypeFactory('FactoryB');
36
            $config->addSuffixTypeFactory('FactoryC');
37
            $config->addSuffixTypeFactory('FactoryD');
38
            $config->addSuffixTypeFactory('FactoryE');
39
40
            $config->addSuffixTypeConfig('ConfigA');
41
            $config->addSuffixTypeConfig('ConfigB');
42
            $config->addSuffixTypeConfig('ConfigC');
43
            $config->addSuffixTypeConfig('ConfigD');
44
            $config->addSuffixTypeConfig('ConfigE');
45
46
            $config->addSuffixTypeDependencyProvider('DepProvA');
47
            $config->addSuffixTypeDependencyProvider('DepProvB');
48
            $config->addSuffixTypeDependencyProvider('DepProvC');
49
            $config->addSuffixTypeDependencyProvider('DepProvD');
50
            $config->addSuffixTypeDependencyProvider('DepProvE');
51
        });
52
53
        $this->removeCacheFile($withCache);
54
    }
55
56
    private function removeCacheFile(bool $withCache): void
57
    {
58
        $cacheFilename = __DIR__ . '/' . '.gacela-class-names.cache';
59
        if (!$withCache && file_exists($cacheFilename)) {
60
            unlink($cacheFilename);
61
        }
62
    }
63
64
    private function loadAllModules(): void
65
    {
66
        (new ModuleA\Facade())->loadGacelaCacheFile();
67
        (new ModuleB\Facade())->loadGacelaCacheFile();
68
        (new ModuleC\Facade())->loadGacelaCacheFile();
69
        (new ModuleD\Facade())->loadGacelaCacheFile();
70
        (new ModuleE\Facade())->loadGacelaCacheFile();
71
        (new ModuleF\Facade())->loadGacelaCacheFile();
72
        (new ModuleG\Facade())->loadGacelaCacheFile();
73
    }
74
}
75