PerformanceTest.php 4.1 KB

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