Passed
Push — master ( 679088...d84efc )
by
unknown
08:49
created

tests/Performance/ManyToOneBench.php (3 issues)

1
<?php
2
3
4
namespace TheCodingMachine\TDBM\Performance;
5
6
use Doctrine\Common\Cache\ArrayCache;
7
use Doctrine\Common\Cache\VoidCache;
8
use Doctrine\DBAL\Connection;
9
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
10
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
11
use TheCodingMachine\FluidSchema\TdbmFluidSchema;
12
use TheCodingMachine\TDBM\Configuration;
13
use TheCodingMachine\TDBM\ConfigurationInterface;
14
use TheCodingMachine\TDBM\ConnectionFactory;
15
use TheCodingMachine\TDBM\DummyGeneratorListener;
16
use TheCodingMachine\TDBM\SchemaLockFileDumper;
17
use TheCodingMachine\TDBM\TDBMAbstractServiceTest;
18
use TheCodingMachine\TDBM\TDBMSchemaAnalyzer;
19
use TheCodingMachine\TDBM\TDBMService;
20
use TheCodingMachine\TDBM\Test\Dao\UserDao;
21
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder;
22
use TheCodingMachine\TDBM\Utils\TDBMDaoGenerator;
23
use function dirname;
24
use function getenv;
25
use function glob;
26
use function is_dir;
27
use function is_file;
28
use function rmdir;
29
use function rtrim;
30
use function unlink;
31
32
/**
33
 * @BeforeClassMethods({"initDatabase"})
34
 */
35
class ManyToOneBench
36
{
37
    public static function initDatabase(): void
38
    {
39
        $dbConnection = ConnectionFactory::resetDatabase(
40
            getenv('DB_DRIVER') ?: null,
41
            getenv('DB_HOST') ?: null,
42
            getenv('DB_PORT') ?: null,
43
            getenv('DB_USERNAME') ?: null,
44
            getenv('DB_ADMIN_USERNAME') ?: null,
45
            getenv('DB_PASSWORD') ?: null,
46
            getenv('DB_NAME') ?: null
47
        );
48
49
        self::initSchema($dbConnection);
50
51
        self::generateDaosAndBeans($dbConnection);
52
    }
53
54
    private static function initSchema(Connection $connection): void
55
    {
56
        $fromSchema = $connection->getSchemaManager()->createSchema();
57
        $toSchema = clone $fromSchema;
58
59
        $db = new TdbmFluidSchema($toSchema, new \TheCodingMachine\FluidSchema\DefaultNamingStrategy($connection->getDatabasePlatform()));
60
61
        $db->table('countries')
62
            ->column('id')->integer()->primaryKey()
63
            ->column('label')->string(255)->unique();
64
65
        $db->table('users')
66
            ->column('id')->integer()->primaryKey()
67
            ->column('name')->string(255)
68
            ->column('country_id')->references('countries');
69
70
        $sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform());
71
72
        foreach ($sqlStmts as $sqlStmt) {
73
            $connection->exec($sqlStmt);
74
        }
75
76
        for ($i = 1; $i<200; $i++) {
77
            TDBMAbstractServiceTest::insert($connection, 'countries', [
78
                'id' => $i,
79
                'label' => 'Country '.$i,
80
            ]);
81
        }
82
83
        for ($i = 1; $i<1000; $i++) {
84
            TDBMAbstractServiceTest::insert($connection, 'users', [
85
                'id' => $i,
86
                'name' => 'User '.$i,
87
                'country_id' => ($i%199) +1,
88
            ]);
89
        }
90
    }
91
92
    private static function generateDaosAndBeans(Connection $connection): void
93
    {
94
        $schemaManager = $connection->getSchemaManager();
95
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
96
        $schemaLockFileDumper = new SchemaLockFileDumper($connection, new ArrayCache(), Configuration::getDefaultLockFilePath());
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

96
        $schemaLockFileDumper = new SchemaLockFileDumper($connection, /** @scrutinizer ignore-deprecated */ new ArrayCache(), Configuration::getDefaultLockFilePath());
Loading history...
97
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($connection, new ArrayCache(), $schemaAnalyzer, $schemaLockFileDumper);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

97
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($connection, /** @scrutinizer ignore-deprecated */ new ArrayCache(), $schemaAnalyzer, $schemaLockFileDumper);
Loading history...
98
        $tdbmDaoGenerator = new TDBMDaoGenerator(self::createConfiguration(), $tdbmSchemaAnalyzer);
99
        $rootPath = __DIR__ . '/../';
100
        self::recursiveDelete(__DIR__. '/../../src/Test/Dao/');
101
102
        $tdbmDaoGenerator->generateAllDaosAndBeans();
103
    }
104
105
    /**
106
     * Delete a file or recursively delete a directory.
107
     *
108
     * @param string $str Path to file or directory
109
     * @return bool
110
     */
111
    private static function recursiveDelete(string $str): bool
112
    {
113
        if (is_file($str)) {
114
            return @unlink($str);
115
        } elseif (is_dir($str)) {
116
            $scan = glob(rtrim($str, '/') . '/*');
117
            foreach ($scan as $index => $path) {
118
                self::recursiveDelete($path);
119
            }
120
121
            return @rmdir($str);
122
        }
123
        return false;
124
    }
125
126
    private static function getConnection(): Connection
127
    {
128
        return ConnectionFactory::createConnection(
129
            getenv('DB_DRIVER') ?: null,
130
            getenv('DB_HOST') ?: null,
131
            getenv('DB_PORT') ?: null,
132
            getenv('DB_USERNAME') ?: null,
133
            getenv('DB_PASSWORD') ?: null,
134
            getenv('DB_NAME') ?: null
135
        );
136
    }
137
138
    protected function getTdbmService(): TDBMService
139
    {
140
        return new TDBMService($this->getConfiguration());
141
    }
142
143
    private static $cache;
144
145
    protected static function getCache(): ArrayCache
146
    {
147
        if (self::$cache === null) {
148
            self::$cache = new ArrayCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

148
            self::$cache = /** @scrutinizer ignore-deprecated */ new ArrayCache();
Loading history...
149
        }
150
        return self::$cache;
151
    }
152
153
    private static function createConfiguration(): ConfigurationInterface
154
    {
155
        $configuration = new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', self::getConnection(), null, self::getCache(), null, null, []);
156
        $configuration->setPathFinder(new PathFinder(null, dirname(__DIR__, 5)));
157
        return $configuration;
158
    }
159
160
    /**
161
     * @var ConfigurationInterface
162
     */
163
    private $configuration;
164
165
    protected function getConfiguration() : ConfigurationInterface
166
    {
167
        if ($this->configuration === null) {
168
            return self::createConfiguration();
169
        }
170
        return $this->configuration;
171
    }
172
173
    /**
174
     * @Iterations(10)
175
     */
176
    public function benchManyToOne(): void
177
    {
178
        $tdbmService = $this->getTdbmService();
179
        $userDao = new UserDao($tdbmService);
180
        foreach ($userDao->findAll() as $user) {
181
            $label = $user->getCountry()->getLabel();
182
        }
183
    }
184
}
185