forked from doctrine/mongodb-odm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseBench.php
89 lines (73 loc) · 2.98 KB
/
BaseBench.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
namespace Doctrine\ODM\MongoDB\Benchmark;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use MongoDB\Client;
use MongoDB\Model\DatabaseInfo;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use function array_map;
use function getenv;
use function in_array;
use function iterator_to_array;
/**
* @BeforeMethods({"initDocumentManager", "clearDatabase"})
*/
abstract class BaseBench
{
public const DATABASE_NAME = 'doctrine_odm_performance';
private const DEFAULT_MONGODB_SERVER = 'mongodb://localhost:27017';
/** @var DocumentManager */
protected static $documentManager;
protected function getDocumentManager(): DocumentManager
{
return self::$documentManager;
}
public function initDocumentManager(): void
{
$config = new Configuration();
$config->setProxyDir(__DIR__ . '/../../tests/Proxies');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir(__DIR__ . '/../../tests/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setPersistentCollectionDir(__DIR__ . '/../../tests/PersistentCollections');
$config->setPersistentCollectionNamespace('PersistentCollections');
$config->setDefaultDB(self::DATABASE_NAME);
$config->setMetadataDriverImpl(self::createMetadataDriverImpl());
$config->setMetadataCache(new ArrayAdapter());
$client = new Client(
getenv('DOCTRINE_MONGODB_SERVER') ?: self::DEFAULT_MONGODB_SERVER,
[],
['typeMap' => ['root' => 'array', 'document' => 'array']]
);
self::$documentManager = DocumentManager::create($client, $config);
}
public function clearDatabase(): void
{
// Check if the database exists. Calling listCollections on a non-existing
// database in a sharded setup will cause an invalid command cursor to be
// returned
$client = self::$documentManager->getClient();
$databases = iterator_to_array($client->listDatabases());
$databaseNames = array_map(static function (DatabaseInfo $database) {
return $database->getName();
}, $databases);
if (! in_array(self::DATABASE_NAME, $databaseNames)) {
return;
}
$collections = $client->selectDatabase(self::DATABASE_NAME)->listCollections();
foreach ($collections as $collection) {
// See https://jira.mongodb.org/browse/SERVER-16541
if ($collection->getName() === 'system.indexes') {
continue;
}
$client->selectCollection(self::DATABASE_NAME, $collection->getName())->drop();
}
}
protected static function createMetadataDriverImpl(): AnnotationDriver
{
return AnnotationDriver::create(__DIR__ . '/../tests/Documents');
}
}