|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Benchmark\DriverBench; |
| 4 | + |
| 5 | +use Generator; |
| 6 | +use MongoDB\Benchmark\Fixtures\Data; |
| 7 | +use MongoDB\Benchmark\Utils; |
| 8 | +use MongoDB\BSON\Document; |
| 9 | +use PhpBench\Attributes\BeforeMethods; |
| 10 | +use PhpBench\Attributes\ParamProviders; |
| 11 | + |
| 12 | +use function array_fill; |
| 13 | +use function file_get_contents; |
| 14 | + |
| 15 | +/** |
| 16 | + * For accurate results, run benchmarks on a standalone server. |
| 17 | + * |
| 18 | + * @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#multi-doc-benchmarks |
| 19 | + */ |
| 20 | +#[BeforeMethods('prepareDatabase')] |
| 21 | +class MultiDocBench |
| 22 | +{ |
| 23 | + public function prepareDatabase(): void |
| 24 | + { |
| 25 | + Utils::getCollection()->drop(); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @see https://github.com/mongodb/specifications/blob/master/source/benchmarking/benchmarking.rst#find-many-and-empty-the-cursor |
| 30 | + * @param array{options: array} $params |
| 31 | + */ |
| 32 | + #[BeforeMethods('setupFindMany')] |
| 33 | + #[ParamProviders('provideFindManyParams')] |
| 34 | + public function benchFindMany(array $params): void |
| 35 | + { |
| 36 | + $collection = Utils::getCollection(); |
| 37 | + |
| 38 | + // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach |
| 39 | + // phpcs:ignore Generic.ControlStructures.InlineControlStructure.NotAllowed |
| 40 | + foreach ($collection->find([], $params['options']) as $document); |
| 41 | + } |
| 42 | + |
| 43 | + public function setupFindMany(): void |
| 44 | + { |
| 45 | + $tweet = Data::readJsonFile(Data::TWEET_FILE_PATH); |
| 46 | + $documents = array_fill(0, 9_999, $tweet); |
| 47 | + Utils::getCollection()->insertMany($documents); |
| 48 | + } |
| 49 | + |
| 50 | + public static function provideFindManyParams(): Generator |
| 51 | + { |
| 52 | + yield 'Driver default typemap' => [ |
| 53 | + 'options' => [], |
| 54 | + ]; |
| 55 | + |
| 56 | + yield 'Raw BSON' => [ |
| 57 | + 'options' => ['typeMap' => ['root' => 'bson']], |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#small-doc-bulk-insert |
| 63 | + * @see https://github.com/mongodb/specifications/blob/ddfc8b583d49aaf8c4c19fa01255afb66b36b92e/source/benchmarking/benchmarking.rst#large-doc-bulk-insert |
| 64 | + * @param array{documents: array} $params |
| 65 | + */ |
| 66 | + #[BeforeMethods('setupBulkInsert')] |
| 67 | + #[ParamProviders('provideBulkInsertParams')] |
| 68 | + public function benchBulkInsert(array $params): void |
| 69 | + { |
| 70 | + $collection = Utils::getCollection(); |
| 71 | + |
| 72 | + // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach |
| 73 | + // phpcs:ignore Generic.ControlStructures.InlineControlStructure.NotAllowed |
| 74 | + $collection->insertMany($params['documents']); |
| 75 | + } |
| 76 | + |
| 77 | + public function setupBulkInsert(): void |
| 78 | + { |
| 79 | + $collectionName = Utils::getCollection()->getCollectionName(); |
| 80 | + Utils::getClient() |
| 81 | + ->selectDatabase(Utils::getDatabase()) |
| 82 | + ->createCollection($collectionName); |
| 83 | + } |
| 84 | + |
| 85 | + public static function provideBulkInsertParams(): Generator |
| 86 | + { |
| 87 | + yield 'Small doc' => [ |
| 88 | + 'documents' => array_fill(0, 9_999, Data::readJsonFile(Data::SMALL_FILE_PATH)), |
| 89 | + ]; |
| 90 | + |
| 91 | + yield 'Small BSON doc' => [ |
| 92 | + 'documents' => array_fill(0, 9_999, Document::fromJSON(file_get_contents(Data::SMALL_FILE_PATH))), |
| 93 | + ]; |
| 94 | + |
| 95 | + yield 'Large doc' => [ |
| 96 | + 'documents' => array_fill(0, 9, Data::readJsonFile(Data::LARGE_FILE_PATH)), |
| 97 | + ]; |
| 98 | + |
| 99 | + yield 'Large BSON doc' => [ |
| 100 | + 'documents' => array_fill(0, 9, Document::fromJSON(file_get_contents(Data::LARGE_FILE_PATH))), |
| 101 | + ]; |
| 102 | + } |
| 103 | +} |
0 commit comments