Skip to content

Commit 63da6c2

Browse files
committed
Add benchmarks to test empty codec overhead
1 parent b0a7521 commit 63da6c2

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed

benchmark/Fixtures/PassThruCodec.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace MongoDB\Benchmark\Fixtures;
4+
5+
use MongoDB\BSON\Document;
6+
use MongoDB\Codec\DecodeIfSupported;
7+
use MongoDB\Codec\DocumentCodec;
8+
use MongoDB\Codec\EncodeIfSupported;
9+
use MongoDB\Exception\UnsupportedValueException;
10+
11+
final class PassThruCodec implements DocumentCodec
12+
{
13+
use DecodeIfSupported;
14+
use EncodeIfSupported;
15+
16+
public function canDecode($value): bool
17+
{
18+
return $value instanceof Document;
19+
}
20+
21+
public function canEncode($value): bool
22+
{
23+
return $value instanceof Document;
24+
}
25+
26+
public function decode($value): object
27+
{
28+
if (! $value instanceof Document) {
29+
throw UnsupportedValueException::invalidDecodableValue($value);
30+
}
31+
32+
return $value;
33+
}
34+
35+
public function encode($value): Document
36+
{
37+
if (! $value instanceof Document) {
38+
throw UnsupportedValueException::invalidEncodableValue($value);
39+
}
40+
41+
return $value;
42+
}
43+
}

benchmark/Fixtures/ToObjectCodec.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace MongoDB\Benchmark\Fixtures;
4+
5+
use MongoDB\BSON\Document;
6+
use MongoDB\Codec\DecodeIfSupported;
7+
use MongoDB\Codec\DocumentCodec;
8+
use MongoDB\Codec\EncodeIfSupported;
9+
use MongoDB\Exception\UnsupportedValueException;
10+
use function is_object;
11+
12+
final class ToObjectCodec implements DocumentCodec
13+
{
14+
use DecodeIfSupported;
15+
use EncodeIfSupported;
16+
17+
public function canDecode($value): bool
18+
{
19+
return $value instanceof Document;
20+
}
21+
22+
public function canEncode($value): bool
23+
{
24+
return is_object($value);
25+
}
26+
27+
public function decode($value): object
28+
{
29+
if (! $value instanceof Document) {
30+
throw UnsupportedValueException::invalidDecodableValue($value);
31+
}
32+
33+
return $value->toPHP(['root' => 'stdClass', 'array' => 'array', 'document' => 'stdClass']);
34+
}
35+
36+
public function encode($value): Document
37+
{
38+
if (! is_object($value)) {
39+
throw UnsupportedValueException::invalidEncodableValue($value);
40+
}
41+
42+
return Document::fromPHP($value);
43+
}
44+
}

benchmark/ReadLargeDocumentBench.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
namespace MongoDB\Benchmark;
44

55
use Generator;
6+
use MongoDB\Benchmark\Fixtures\PassThruCodec;
7+
use MongoDB\Benchmark\Fixtures\ToObjectCodec;
68
use MongoDB\BSON\Document;
9+
use MongoDB\Codec\DecodeIfSupported;
10+
use MongoDB\Codec\DocumentCodec;
11+
use MongoDB\Codec\EncodeIfSupported;
712
use MongoDB\Exception\InvalidArgumentException;
13+
use MongoDB\Exception\UnsupportedValueException;
814
use MongoDB\Model\BSONArray;
915
use MongoDB\Model\BSONDocument;
1016
use PhpBench\Attributes\BeforeClassMethods;
@@ -40,7 +46,7 @@ public function provideParams(): Generator
4046
{
4147
yield 'Driver default typemap' => [
4248
'codec' => null,
43-
'typeMap' => [],
49+
'typeMap' => null,
4450
'accessor' => 'object',
4551
];
4652

@@ -65,6 +71,18 @@ public function provideParams(): Generator
6571
'typeMap' => ['root' => 'bson'],
6672
'accessor' => 'bson',
6773
];
74+
75+
yield 'Codec (pass thru)' => [
76+
'codec' => new PassThruCodec(),
77+
'typeMap' => null,
78+
'accessor' => 'bson',
79+
];
80+
81+
yield 'Codec (to array)' => [
82+
'codec' => new ToObjectCodec(),
83+
'typeMap' => null,
84+
'accessor' => 'object',
85+
];
6886
}
6987

7088
#[ParamProviders('provideParams')]

benchmark/ReadMultipleDocumentsBench.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace MongoDB\Benchmark;
44

55
use Generator;
6+
use MongoDB\Benchmark\Fixtures\PassThruCodec;
7+
use MongoDB\Benchmark\Fixtures\ToObjectCodec;
68
use MongoDB\BSON\Document;
79
use MongoDB\Exception\InvalidArgumentException;
810
use MongoDB\Model\BSONArray;
@@ -65,6 +67,18 @@ public function provideParams(): Generator
6567
'typeMap' => ['root' => 'bson'],
6668
'accessor' => 'bson',
6769
];
70+
71+
yield 'Codec (pass thru)' => [
72+
'codec' => new PassThruCodec(),
73+
'typeMap' => null,
74+
'accessor' => 'bson',
75+
];
76+
77+
yield 'Codec (to array)' => [
78+
'codec' => new ToObjectCodec(),
79+
'typeMap' => null,
80+
'accessor' => 'object',
81+
];
6882
}
6983

7084
#[ParamProviders('provideParams')]

0 commit comments

Comments
 (0)