From 48ab2112237b845bcaa4faf4613f4e6f26275736 Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Thu, 15 Nov 2018 11:01:43 +0000 Subject: [PATCH] add a symfony benchmark with custom normalizers --- src/Command/BenchmarkCommand.php | 2 + src/SymfonyCustomNormalizerBenchmark.php | 149 +++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 src/SymfonyCustomNormalizerBenchmark.php diff --git a/src/Command/BenchmarkCommand.php b/src/Command/BenchmarkCommand.php index 9ecc4ee..36a6d9a 100644 --- a/src/Command/BenchmarkCommand.php +++ b/src/Command/BenchmarkCommand.php @@ -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; @@ -71,6 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { $benchmarks = [ new IvoryBenchmark(), + new SymfonyCustomNormalizerBenchmark(), new SymfonyObjectNormalizerBenchmark(), new SymfonyGetSetNormalizerBenchmark(), new JmsBenchmark(), diff --git a/src/SymfonyCustomNormalizerBenchmark.php b/src/SymfonyCustomNormalizerBenchmark.php new file mode 100644 index 0000000..9b4e333 --- /dev/null +++ b/src/SymfonyCustomNormalizerBenchmark.php @@ -0,0 +1,149 @@ +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() + ); + } +}