PerformanceTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 Symfony\Component\Translation\MessageSelector;
  19. use Symfony\Component\Translation\IdentityTranslator;
  20. use Doctrine\Common\Annotations\AnnotationReader;
  21. use JMS\SerializerBundle\Exception\InvalidArgumentException;
  22. use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
  23. use JMS\SerializerBundle\JMSSerializerBundle;
  24. use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension;
  25. use Symfony\Component\DependencyInjection\ContainerBuilder;
  26. use JMS\SerializerBundle\Tests\Fixtures\Comment;
  27. use JMS\SerializerBundle\Tests\Fixtures\Author;
  28. use JMS\SerializerBundle\Tests\Fixtures\BlogPost;
  29. use JMS\SerializerBundle\Serializer\Exclusion\NoneExclusionStrategy;
  30. use JMS\SerializerBundle\Serializer\Exclusion\AllExclusionStrategy;
  31. use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyFactory;
  32. use JMS\SerializerBundle\Serializer\Naming\CamelCaseNamingStrategy;
  33. use JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy;
  34. use JMS\SerializerBundle\Serializer\Normalizer\ArrayCollectionNormalizer;
  35. use JMS\SerializerBundle\Serializer\Normalizer\NativePhpTypeNormalizer;
  36. use JMS\SerializerBundle\Serializer\Normalizer\PropertyBasedNormalizer;
  37. use JMS\SerializerBundle\Serializer\Serializer;
  38. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  39. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  40. /**
  41. * @group performance
  42. */
  43. class PerformanceTest extends \PHPUnit_Framework_TestCase
  44. {
  45. /**
  46. * @dataProvider getFormats
  47. */
  48. public function testSerializationPerformance($format)
  49. {
  50. $serializer = $this->getSerializer();
  51. $testData = $this->getTestCollection();
  52. $time = microtime(true);
  53. for ($i=0,$c=10; $i<$c; $i++) {
  54. $serializer->serialize($testData, $format);
  55. }
  56. $time = microtime(true) - $time;
  57. $this->printResults("serialization ($format)", $time, $c);
  58. }
  59. public function getFormats()
  60. {
  61. return array(array('json'), array('xml'));
  62. }
  63. private function getTestCollection()
  64. {
  65. $collection = array();
  66. for ($i=0; $i<50; $i++) {
  67. $collection[] = $this->getTestObject();
  68. }
  69. return $collection;
  70. }
  71. private function getTestObject()
  72. {
  73. $post = new BlogPost('FooooooooooooooooooooooBAR', new Author('Foo'), new \DateTime);
  74. for ($i=0; $i<10; $i++) {
  75. $post->addComment(new Comment(new Author('foo'), 'foobar'));
  76. }
  77. return $post;
  78. }
  79. private function getSerializer()
  80. {
  81. $container = new ContainerBuilder();
  82. $container->set('annotation_reader', new AnnotationReader());
  83. $container->set('translator', new IdentityTranslator(new MessageSelector()));
  84. $container->setParameter('kernel.debug', true);
  85. $container->setParameter('kernel.cache_dir', sys_get_temp_dir());
  86. $container->setParameter('kernel.bundles', array());
  87. $extension = new JMSSerializerExtension();
  88. $extension->load(array(array()), $container);
  89. $bundle = new JMSSerializerBundle();
  90. $bundle->build($container);
  91. $container->getCompilerPassConfig()->setOptimizationPasses(array(
  92. new ResolveDefinitionTemplatesPass(),
  93. ));
  94. $container->getCompilerPassConfig()->setRemovingPasses(array());
  95. $container->compile();
  96. return $container->get('serializer');
  97. }
  98. private function printResults($test, $time, $iterations)
  99. {
  100. if (0 == $iterations) {
  101. throw new InvalidArgumentException('$iterations cannot be zero.');
  102. }
  103. $title = $test." results:\n";
  104. $iterationsText = sprintf("Iterations: %d\n", $iterations);
  105. $totalTime = sprintf("Total Time: %.3f s\n", $time);
  106. $iterationTime = sprintf("Time per iteration: %.3f ms\n", $time/$iterations * 1000);
  107. $max = max(strlen($title), strlen($iterationTime)) - 1;
  108. echo "\n".str_repeat('-', $max)."\n";
  109. echo $title;
  110. echo str_repeat('=', $max)."\n";
  111. echo $iterationsText;
  112. echo $totalTime;
  113. echo $iterationTime;
  114. echo str_repeat('-', $max)."\n";
  115. }
  116. }