Created
January 23, 2022 12:06
-
-
Save sergix44/f8400812647931d7c6284cfe33fbca4d to your computer and use it in GitHub Desktop.
comparing json mapping libraries
This file contains hidden or 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
| <?php | |
| use EventSauce\ObjectHydrator\DefinitionDumper; | |
| use EventSauce\ObjectHydrator\DefinitionProvider; | |
| use EventSauce\ObjectHydrator\ObjectHydrator; | |
| use EventSauce\ObjectHydrator\ObjectHydratorDumper; | |
| use JsonMapper\Cache\ArrayCache; | |
| use JsonMapper\Handler\PropertyMapper; | |
| require 'vendor/autoload.php'; | |
| function bench($what = 'Bench', $echo = true) | |
| { | |
| global $DEBUG_TIME; | |
| if ($DEBUG_TIME === null) { | |
| $DEBUG_TIME = hrtime(true); | |
| } else { | |
| $t = (hrtime(true) - $DEBUG_TIME) / 1e+9; | |
| $DEBUG_TIME = null; | |
| if ($echo) { | |
| echo "{$what} took {$t} s\n"; | |
| } else { | |
| return $t; | |
| } | |
| } | |
| return null; | |
| } | |
| class Address | |
| { | |
| public ?string $street; | |
| public ?string $city; | |
| /** | |
| * @param string|null $street | |
| * @param string|null $city | |
| */ | |
| public function __construct(?string $street = null, ?string $city = null) | |
| { | |
| $this->street = $street; | |
| $this->city = $city; | |
| } | |
| } | |
| class Contact | |
| { | |
| /** | |
| * Full name | |
| * @var string|null | |
| */ | |
| public ?string $name; | |
| /** | |
| * @var Address|null | |
| */ | |
| public ?Address $address; | |
| /** | |
| * @param string|null $name | |
| * @param Address|null $address | |
| */ | |
| public function __construct(?string $name = null, ?Address $address = null) | |
| { | |
| $this->name = $name; | |
| $this->address = $address; | |
| } | |
| } | |
| $rawJson = '{"name":"Mario Rossi","address":{"street":"Via XX XX 1","city":"Milano"}}'; | |
| $json = json_decode($rawJson, false, 512, JSON_THROW_ON_ERROR); | |
| $jsonArr = json_decode($rawJson, true, 512, JSON_THROW_ON_ERROR); | |
| $runs = 100000; | |
| $mapper1 = new JsonMapper(); | |
| bench('cweiske/jsonmapper json mapper'); | |
| for ($i = 0; $i < $runs; $i++) { | |
| $o = new Contact(); | |
| $mapper1->map($json, $o); | |
| } | |
| bench('cweiske/jsonmapper json mapper'); | |
| // ------------------------------------------------------------------------------------------------------- | |
| $mapper2 = (new \JsonMapper\JsonMapperFactory())->bestFit(); | |
| bench('new json mapper "best fit"'); | |
| for ($i = 0; $i < $runs; $i++) { | |
| $o = new Contact(); | |
| $mapper2->mapObject($json, $o); | |
| } | |
| bench('jsonmapper/jsonmapper json mapper "best fit"'); | |
| // ------------------------------------------------------------------------------------------------------- | |
| $mapper2 = (new \JsonMapper\JsonMapperFactory())->default(); | |
| bench('new json mapper "default"'); | |
| for ($i = 0; $i < $runs; $i++) { | |
| $o = new Contact(); | |
| $mapper2->mapObject($json, $o); | |
| } | |
| bench('jsonmapper/jsonmapper json mapper "default"'); | |
| // ------------------------------------------------------------------------------------------------------- | |
| $c = new ArrayCache(); | |
| $mapper2 = (new \JsonMapper\JsonMapperFactory())->create( | |
| new PropertyMapper, | |
| new \JsonMapper\Middleware\NamespaceResolver($c), | |
| new \JsonMapper\Middleware\TypedProperties($c), | |
| ); | |
| bench('jsonmapper/jsonmapper json mapper "custom"'); | |
| for ($i = 0; $i < $runs; $i++) { | |
| $o = new Contact(); | |
| $mapper2->mapObject($json, $o); | |
| } | |
| bench('jsonmapper/jsonmapper json mapper "custom"'); | |
| // ------------------------------------------------------------------------------------------------------- | |
| $hydrator = new ObjectHydrator(); | |
| bench('eventsauce/object-hydrator json mapper'); | |
| for ($i = 0; $i < $runs; $i++) { | |
| $o = $hydrator->hydrateObject( | |
| Contact::class, | |
| $jsonArr, | |
| ); | |
| } | |
| bench('eventsauce/object-hydrator json mapper'); | |
| // ------------------------------------------------------------------------------------------------------- | |
| $dumpedClassNamed = "YYY\\XXX"; | |
| $dumper = new ObjectHydratorDumper(); | |
| $classesToDump = [Contact::class]; | |
| $code = $dumper->dump($classesToDump, $dumpedClassNamed); | |
| file_put_contents('XXX.php', $code); | |
| require 'XXX.php'; | |
| /** @var ObjectHydrator $hydrator */ | |
| $hydrator = new YYY\XXX(); | |
| bench('eventsauce/object-hydrator optimized json mapper'); | |
| for ($i = 0; $i < $runs; $i++) { | |
| $o = $hydrator->hydrateObject( | |
| Contact::class, | |
| $jsonArr, | |
| ); | |
| } | |
| bench('eventsauce/object-hydrator optimized json mapper'); | |
| // ------------------------------------------------------------------------------------------------------- | |
| $mapper3 = (new \CuyZ\Valinor\MapperBuilder()) | |
| ->mapper(); | |
| bench('cuyz/valinor json mapper'); | |
| for ($i = 0; $i < $runs; $i++) { | |
| $o = $mapper3->map(Contact::class, new \CuyZ\Valinor\Mapper\Source\JsonSource($rawJson)); | |
| } | |
| bench('cuyz/valinor json mapper'); |
This file contains hidden or 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
| { | |
| "require": { | |
| "json-mapper/json-mapper": "^2.9", | |
| "netresearch/jsonmapper": "^4.0", | |
| "eventsauce/object-hydrator": "^0.1.0", | |
| "cuyz/valinor": "^0.4.0" | |
| } | |
| } |
This file contains hidden or 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
| testMapper> php8 .\bench.php | |
| cweiske/jsonmapper json mapper took 0.8331107 s | |
| jsonmapper/jsonmapper json mapper "best fit" took 3.4992287 s | |
| jsonmapper/jsonmapper json mapper "default" took 1.7027062 s | |
| jsonmapper/jsonmapper json mapper "custom" took 2.079014 s | |
| eventsauce/object-hydrator json mapper took 1.4441185 s | |
| eventsauce/object-hydrator optimized json mapper took 0.1095334 s | |
| cuyz/valinor json mapper took 4.9791515 s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment