-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PHPLIB-1183: Add benchmarks for codecs (#1146)
* Move sample data to fixtures directory * Add benchmarks to test empty codec overhead * Add benchmark to convert BSON structures to PHP via iteration
- Loading branch information
Showing
9 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters