PerformanceTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Tests;
  18. use Doctrine\Common\Annotations\AnnotationReader;
  19. use JMS\SerializerBundle\Exception\InvalidArgumentException;
  20. use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
  21. use JMS\SerializerBundle\JMSSerializerBundle;
  22. use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension;
  23. use Symfony\Component\DependencyInjection\ContainerBuilder;
  24. use JMS\SerializerBundle\Tests\Fixtures\Comment;
  25. use JMS\SerializerBundle\Tests\Fixtures\Author;
  26. use JMS\SerializerBundle\Tests\Fixtures\BlogPost;
  27. use JMS\SerializerBundle\Serializer\Exclusion\NoneExclusionStrategy;
  28. use JMS\SerializerBundle\Serializer\Exclusion\AllExclusionStrategy;
  29. use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyFactory;
  30. use JMS\SerializerBundle\Serializer\Naming\CamelCaseNamingStrategy;
  31. use JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy;
  32. use JMS\SerializerBundle\Serializer\Normalizer\ArrayCollectionNormalizer;
  33. use JMS\SerializerBundle\Serializer\Normalizer\NativePhpTypeNormalizer;
  34. use JMS\SerializerBundle\Serializer\Normalizer\PropertyBasedNormalizer;
  35. use JMS\SerializerBundle\Serializer\Serializer;
  36. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  37. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  38. /**
  39. * @group performance
  40. */
  41. class PerformanceTest extends \PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * @dataProvider getFormats
  45. */
  46. public function testSerializationPerformance($format)
  47. {
  48. $serializer = $this->getSerializer();
  49. $testData = $this->getTestCollection();
  50. $time = microtime(true);
  51. for ($i=0,$c=10; $i<$c; $i++) {
  52. $serializer->serialize($testData, $format);
  53. }
  54. $time = microtime(true) - $time;
  55. $this->printResults("serialization ($format)", $time, $c);
  56. }
  57. public function getFormats()
  58. {
  59. return array(array('json'), array('xml'));
  60. }
  61. private function getTestCollection()
  62. {
  63. $collection = array();
  64. for ($i=0; $i<50; $i++) {
  65. $collection[] = $this->getTestObject();
  66. }
  67. return $collection;
  68. }
  69. private function getTestObject()
  70. {
  71. $post = new BlogPost('FooooooooooooooooooooooBAR', new Author('Foo'), new \DateTime);
  72. for ($i=0; $i<10; $i++) {
  73. $post->addComment(new Comment(new Author('foo'), 'foobar'));
  74. }
  75. return $post;
  76. }
  77. private function getSerializer()
  78. {
  79. $container = new ContainerBuilder();
  80. $container->set('annotation_reader', new AnnotationReader());
  81. $container->setParameter('kernel.debug', true);
  82. $container->setParameter('kernel.cache_dir', sys_get_temp_dir());
  83. $extension = new JMSSerializerExtension();
  84. $extension->load(array(array()), $container);
  85. $bundle = new JMSSerializerBundle();
  86. $bundle->build($container);
  87. $container->getCompilerPassConfig()->setOptimizationPasses(array(
  88. new ResolveDefinitionTemplatesPass(),
  89. ));
  90. $container->getCompilerPassConfig()->setRemovingPasses(array());
  91. $container->compile();
  92. return $container->get('serializer');
  93. }
  94. private function printResults($test, $time, $iterations)
  95. {
  96. if (0 == $iterations) {
  97. throw new InvalidArgumentException('$iterations cannot be zero.');
  98. }
  99. $title = $test." results:\n";
  100. $iterationsText = sprintf("Iterations: %d\n", $iterations);
  101. $totalTime = sprintf("Total Time: %.3f s\n", $time);
  102. $iterationTime = sprintf("Time per iteration: %.3f ms\n", $time/$iterations * 1000);
  103. $max = max(strlen($title), strlen($iterationTime)) - 1;
  104. echo "\n".str_repeat('-', $max)."\n";
  105. echo $title;
  106. echo str_repeat('=', $max)."\n";
  107. echo $iterationsText;
  108. echo $totalTime;
  109. echo $iterationTime;
  110. echo str_repeat('-', $max)."\n";
  111. }
  112. }