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

add a symfony benchmark with custom normalizers #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/Command/BenchmarkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Ivory\Tests\Serializer\Benchmark\Runner\BenchmarkRunner;
use Ivory\Tests\Serializer\Benchmark\SerializardClosureBenchmark;
use Ivory\Tests\Serializer\Benchmark\SerializardReflectionBenchmark;
use Ivory\Tests\Serializer\Benchmark\SymfonyCustomNormalizerBenchmark;
use Ivory\Tests\Serializer\Benchmark\SymfonyGetSetNormalizerBenchmark;
use Ivory\Tests\Serializer\Benchmark\SymfonyObjectNormalizerBenchmark;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -71,6 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
} else {
$benchmarks = [
new IvoryBenchmark(),
new SymfonyCustomNormalizerBenchmark(),
new SymfonyObjectNormalizerBenchmark(),
new SymfonyGetSetNormalizerBenchmark(),
new JmsBenchmark(),
Expand Down
149 changes: 149 additions & 0 deletions src/SymfonyCustomNormalizerBenchmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace Ivory\Tests\Serializer\Benchmark;

use Ivory\Tests\Serializer\Benchmark\Model\Category;
use Ivory\Tests\Serializer\Benchmark\Model\Comment;
use Ivory\Tests\Serializer\Benchmark\Model\Forum;
use Ivory\Tests\Serializer\Benchmark\Model\Thread;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;

class SymfonyCustomNormalizerBenchmark extends AbstractBenchmark
{
/**
* @var Serializer
*/
private $serializer;

/**
* {@inheritdoc}
*/
public function setUp()
{
$this->serializer = new Serializer([
new DateTimeNormalizer(),
new class implements NormalizerInterface, CacheableSupportsMethodInterface
{
public function hasCacheableSupportsMethod(): bool
{
return true;
}

public function normalize($object, $format = null, array $context = array())
{
assert($object instanceof Forum);

return [
'id' => $object->getId(),
'name' => $object->getName(),
'threads' => $object->getThreads(),
'category' => $object->getCategory(),
'createdAt' => $object->getCreatedAt(),
'updatedAt' => $object->getUpdatedAt(),
];
}

public function supportsNormalization($data, $format = null)
{
return $data instanceof Forum;
}
},
new class implements NormalizerInterface, CacheableSupportsMethodInterface
{
public function hasCacheableSupportsMethod(): bool
{
return true;
}

public function normalize($object, $format = null, array $context = array())
{
assert($object instanceof Thread);

return [
'id' => $object->getId(),
'popularity' => $object->getPopularity(),
'title' => $object->getTitle(),
'comments' => $object->getComments(),
'description' => $object->getDescription(),
'createdAt' => $object->getCreatedAt(),
'updatedAt' => $object->getUpdatedAt(),
];
}

public function supportsNormalization($data, $format = null)
{
return $data instanceof Thread;
}
},
new class implements NormalizerInterface, CacheableSupportsMethodInterface
{
public function hasCacheableSupportsMethod(): bool
{
return true;
}

public function normalize($object, $format = null, array $context = array())
{
assert($object instanceof Comment);

return [
'id' => $object->getId(),
'content' => $object->getContent(),
'author' => $object->getAuthor(),
'createdAt' => $object->getCreatedAt(),
'updatedAt' => $object->getUpdatedAt(),
];
}

public function supportsNormalization($data, $format = null)
{
return $data instanceof Comment;
}
},
new class implements NormalizerInterface, CacheableSupportsMethodInterface
{
public function hasCacheableSupportsMethod(): bool
{
return true;
}

public function normalize($object, $format = null, array $context = array())
{
assert($object instanceof Category);

return [
'id' => $object->getId(),
'parent' => $object->getParent(),
'children' => $object->getChildren(),
'createdAt' => $object->getCreatedAt(),
'updatedAt' => $object->getUpdatedAt(),
];
}

public function supportsNormalization($data, $format = null)
{
return $data instanceof Category;
}
},
], [
new JsonEncoder(), new XmlEncoder(), new YamlEncoder()
]);
}

/**
* {@inheritdoc}
*/
public function execute($horizontalComplexity = 1, $verticalComplexity = 1)
{
return $this->serializer->serialize(
$this->getData($horizontalComplexity, $verticalComplexity),
$this->getFormat()
);
}
}