PerformanceTest.php 4.9 KB

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