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