Passed
Push — master ( 51a94b...0f3d7a )
by Jesús
01:31 queued 13s
created

FileCacheBench   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 38
c 2
b 0
f 0
dl 0
loc 64
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A removeFiles() 0 10 2
A bench_with_cache() 0 4 1
A bench_without_cache() 0 4 1
A gacelaBootstrapWithCache() 0 26 1
A loadAllModules() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Benchmark\Framework\ClassResolver\FileCache;
6
7
use Gacela\Framework\Bootstrap\GacelaConfig;
8
use Gacela\Framework\ClassResolver\Cache\ClassNamePhpCache;
9
use Gacela\Framework\ClassResolver\Cache\CustomServicesPhpCache;
10
use Gacela\Framework\Gacela;
11
use GacelaTest\Fixtures\StringValue;
12
use GacelaTest\Fixtures\StringValueInterface;
13
14
/**
15
 * @Revs(25)
16
 * @Iterations(10)
17
 * @BeforeClassMethods("removeFiles")
18
 */
19
final class FileCacheBench
20
{
21
    public static function removeFiles(): void
22
    {
23
        $removeFile = static function (string $filename): void {
24
            $filenameFullPath = __DIR__ . '/.gacela/cache/' . $filename;
25
            if (file_exists($filenameFullPath)) {
26
                unlink($filenameFullPath);
27
            }
28
        };
29
        $removeFile(ClassNamePhpCache::FILENAME);
30
        $removeFile(CustomServicesPhpCache::FILENAME);
31
    }
32
33
    public function bench_without_cache(): void
34
    {
35
        $this->gacelaBootstrapWithCache(false);
36
        $this->loadAllModules();
37
    }
38
39
    public function bench_with_cache(): void
40
    {
41
        $this->gacelaBootstrapWithCache(true);
42
        $this->loadAllModules();
43
    }
44
45
    private function gacelaBootstrapWithCache(bool $cacheEnabled): void
46
    {
47
        Gacela::bootstrap(__DIR__, static function (GacelaConfig $config) use ($cacheEnabled): void {
48
            $config->resetInMemoryCache();
49
            $config->addAppConfig('config/*.php');
50
            $config->setFileCacheEnabled($cacheEnabled);
51
52
            $config->addMappingInterface(StringValueInterface::class, new StringValue('testing-string'));
53
54
            $config->addSuffixTypeFactory('FactoryA');
55
            $config->addSuffixTypeFactory('FactoryB');
56
            $config->addSuffixTypeFactory('FactoryC');
57
            $config->addSuffixTypeFactory('FactoryD');
58
            $config->addSuffixTypeFactory('FactoryE');
59
60
            $config->addSuffixTypeConfig('ConfigA');
61
            $config->addSuffixTypeConfig('ConfigB');
62
            $config->addSuffixTypeConfig('ConfigC');
63
            $config->addSuffixTypeConfig('ConfigD');
64
            $config->addSuffixTypeConfig('ConfigE');
65
66
            $config->addSuffixTypeDependencyProvider('DepProvA');
67
            $config->addSuffixTypeDependencyProvider('DepProvB');
68
            $config->addSuffixTypeDependencyProvider('DepProvC');
69
            $config->addSuffixTypeDependencyProvider('DepProvD');
70
            $config->addSuffixTypeDependencyProvider('DepProvE');
71
        });
72
    }
73
74
    private function loadAllModules(): void
75
    {
76
        (new ModuleA\Facade())->loadGacelaCacheFile();
77
        (new ModuleB\Facade())->loadGacelaCacheFile();
78
        (new ModuleC\Facade())->loadGacelaCacheFile();
79
        (new ModuleD\Facade())->loadGacelaCacheFile();
80
        (new ModuleE\Facade())->loadGacelaCacheFile();
81
        (new ModuleF\Facade())->loadGacelaCacheFile();
82
        (new ModuleG\ModuleGFacade())->loadGacelaCacheFile();
83
    }
84
}
85