Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPLIB-1183: Add benchmarks for codecs #1146

Merged
merged 3 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions benchmark/BSON/DocumentBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand Down
9 changes: 9 additions & 0 deletions benchmark/BSON/PackedArrayBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions benchmark/BaseBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
47 changes: 47 additions & 0 deletions benchmark/Fixtures/PassThruCodec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace MongoDB\Benchmark\Fixtures;

use MongoDB\BSON\Document;
use MongoDB\Codec\DecodeIfSupported;
use MongoDB\Codec\DocumentCodec;
use MongoDB\Codec\EncodeIfSupported;
use MongoDB\Exception\UnsupportedValueException;

final class PassThruCodec implements DocumentCodec
{
use DecodeIfSupported;
use EncodeIfSupported;

/** @param mixed $value */
public function canDecode($value): bool
{
return $value instanceof Document;
}

/** @param mixed $value */
public function canEncode($value): bool
{
return $value instanceof Document;
}

/** @param mixed $value */
public function decode($value): Document
{
if (! $value instanceof Document) {
throw UnsupportedValueException::invalidDecodableValue($value);
}

return $value;
}

/** @param mixed $value */
public function encode($value): Document
{
if (! $value instanceof Document) {
throw UnsupportedValueException::invalidEncodableValue($value);
}

return $value;
}
}
49 changes: 49 additions & 0 deletions benchmark/Fixtures/ToObjectCodec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace MongoDB\Benchmark\Fixtures;

use MongoDB\BSON\Document;
use MongoDB\Codec\DecodeIfSupported;
use MongoDB\Codec\DocumentCodec;
use MongoDB\Codec\EncodeIfSupported;
use MongoDB\Exception\UnsupportedValueException;

use function is_object;

final class ToObjectCodec implements DocumentCodec
{
use DecodeIfSupported;
use EncodeIfSupported;

/** @param mixed $value */
public function canDecode($value): bool
{
return $value instanceof Document;
}

/** @param mixed $value */
public function canEncode($value): bool
{
return is_object($value);
}

/** @param mixed $value */
public function decode($value): object
{
if (! $value instanceof Document) {
throw UnsupportedValueException::invalidDecodableValue($value);
}

return $value->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);
}
}
File renamed without changes.
File renamed without changes.
16 changes: 15 additions & 1 deletion benchmark/ReadLargeDocumentBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,7 +42,7 @@ public function provideParams(): Generator
{
yield 'Driver default typemap' => [
'codec' => null,
'typeMap' => [],
'typeMap' => null,
'accessor' => 'object',
];

Expand All @@ -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')]
Expand Down
14 changes: 14 additions & 0 deletions benchmark/ReadMultipleDocumentsBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')]
Expand Down