diff --git a/benchmark/BSON/DocumentBench.php b/benchmark/BSON/DocumentBench.php index 111ddb310..db0ca0495 100644 --- a/benchmark/BSON/DocumentBench.php +++ b/benchmark/BSON/DocumentBench.php @@ -5,6 +5,7 @@ use MongoDB\Benchmark\BaseBench; use MongoDB\BSON\Document; use PhpBench\Attributes\BeforeMethods; +use stdClass; use function file_get_contents; use function iterator_to_array; @@ -49,6 +50,15 @@ public function benchToPHPObject(): void self::$document->toPHP(); } + public function benchToPHPObjectViaIteration(): void + { + $object = new stdClass(); + + foreach (self::$document as $key => $value) { + $object->$key = $value; + } + } + public function benchToPHPArray(): void { self::$document->toPHP(['root' => 'array']); diff --git a/benchmark/BSON/PackedArrayBench.php b/benchmark/BSON/PackedArrayBench.php index 4bb349d75..59cea5f37 100644 --- a/benchmark/BSON/PackedArrayBench.php +++ b/benchmark/BSON/PackedArrayBench.php @@ -55,6 +55,15 @@ public function benchToPHPArray(): void self::$array->toPHP(); } + public function benchToPHPArrayViaIteration(): void + { + $array = []; + + foreach (self::$array as $key => $value) { + $array[$key] = $value; + } + } + public function benchIteration(): void { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach diff --git a/benchmark/BaseBench.php b/benchmark/BaseBench.php index 824d57879..5a7b2b669 100644 --- a/benchmark/BaseBench.php +++ b/benchmark/BaseBench.php @@ -9,8 +9,8 @@ abstract class BaseBench { - protected const LARGE_FILE_PATH = __DIR__ . '/data/large_doc.json'; - protected const TWEET_FILE_PATH = __DIR__ . '/data/tweet.json'; + protected const LARGE_FILE_PATH = __DIR__ . '/Fixtures/data/large_doc.json'; + protected const TWEET_FILE_PATH = __DIR__ . '/Fixtures/data/tweet.json'; private static ?Collection $collection; diff --git a/benchmark/Fixtures/PassThruCodec.php b/benchmark/Fixtures/PassThruCodec.php new file mode 100644 index 000000000..7a8846cc6 --- /dev/null +++ b/benchmark/Fixtures/PassThruCodec.php @@ -0,0 +1,47 @@ +toPHP(['root' => 'stdClass', 'array' => 'array', 'document' => 'stdClass']); + } + + /** @param mixed $value */ + public function encode($value): Document + { + if (! is_object($value)) { + throw UnsupportedValueException::invalidEncodableValue($value); + } + + return Document::fromPHP($value); + } +} diff --git a/benchmark/data/large_doc.json b/benchmark/Fixtures/data/large_doc.json similarity index 100% rename from benchmark/data/large_doc.json rename to benchmark/Fixtures/data/large_doc.json diff --git a/benchmark/data/tweet.json b/benchmark/Fixtures/data/tweet.json similarity index 100% rename from benchmark/data/tweet.json rename to benchmark/Fixtures/data/tweet.json diff --git a/benchmark/ReadLargeDocumentBench.php b/benchmark/ReadLargeDocumentBench.php index fd25380a2..471a9ec8f 100644 --- a/benchmark/ReadLargeDocumentBench.php +++ b/benchmark/ReadLargeDocumentBench.php @@ -3,6 +3,8 @@ namespace MongoDB\Benchmark; use Generator; +use MongoDB\Benchmark\Fixtures\PassThruCodec; +use MongoDB\Benchmark\Fixtures\ToObjectCodec; use MongoDB\BSON\Document; use MongoDB\Exception\InvalidArgumentException; use MongoDB\Model\BSONArray; @@ -40,7 +42,7 @@ public function provideParams(): Generator { yield 'Driver default typemap' => [ 'codec' => null, - 'typeMap' => [], + 'typeMap' => null, 'accessor' => 'object', ]; @@ -65,6 +67,18 @@ public function provideParams(): Generator 'typeMap' => ['root' => 'bson'], 'accessor' => 'bson', ]; + + yield 'Codec (pass thru)' => [ + 'codec' => new PassThruCodec(), + 'typeMap' => null, + 'accessor' => 'bson', + ]; + + yield 'Codec (to object)' => [ + 'codec' => new ToObjectCodec(), + 'typeMap' => null, + 'accessor' => 'object', + ]; } #[ParamProviders('provideParams')] diff --git a/benchmark/ReadMultipleDocumentsBench.php b/benchmark/ReadMultipleDocumentsBench.php index 18c97bf02..e52d4ea91 100644 --- a/benchmark/ReadMultipleDocumentsBench.php +++ b/benchmark/ReadMultipleDocumentsBench.php @@ -3,6 +3,8 @@ namespace MongoDB\Benchmark; use Generator; +use MongoDB\Benchmark\Fixtures\PassThruCodec; +use MongoDB\Benchmark\Fixtures\ToObjectCodec; use MongoDB\BSON\Document; use MongoDB\Exception\InvalidArgumentException; use MongoDB\Model\BSONArray; @@ -65,6 +67,18 @@ public function provideParams(): Generator 'typeMap' => ['root' => 'bson'], 'accessor' => 'bson', ]; + + yield 'Codec (pass thru)' => [ + 'codec' => new PassThruCodec(), + 'typeMap' => null, + 'accessor' => 'bson', + ]; + + yield 'Codec (to object)' => [ + 'codec' => new ToObjectCodec(), + 'typeMap' => null, + 'accessor' => 'object', + ]; } #[ParamProviders('provideParams')]