PerformanceTest.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\DiExtraBundle\Tests;
  18. use Symfony\Bundle\TwigBundle\TwigBundle;
  19. use Symfony\Bundle\DoctrineBundle\DoctrineBundle;
  20. use JMS\DiExtraBundle\JMSDiExtraBundle;
  21. use JMS\SecurityExtraBundle\JMSSecurityExtraBundle;
  22. use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
  23. use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
  24. use Symfony\Bundle\AsseticBundle\AsseticBundle;
  25. use Symfony\Bundle\MonologBundle\MonologBundle;
  26. use Symfony\Bundle\SecurityBundle\SecurityBundle;
  27. use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
  28. use JMS\DiExtraBundle\Finder\ServiceFinder;
  29. /**
  30. * @group performance
  31. */
  32. class PerformanceTest extends \PHPUnit_Framework_TestCase
  33. {
  34. /**
  35. * @dataProvider getFinderMethods
  36. */
  37. public function testServiceFinder($method)
  38. {
  39. $finder = new ServiceFinder();
  40. $ref = new \ReflectionMethod($finder, $method);
  41. $ref->setAccessible(true);
  42. $bundles = array(
  43. new FrameworkBundle(),
  44. new SecurityBundle(),
  45. new MonologBundle(),
  46. new AsseticBundle(),
  47. new DoctrineBundle(),
  48. new TwigBundle(),
  49. new SensioFrameworkExtraBundle(),
  50. new JMSSecurityExtraBundle(),
  51. );
  52. $bundles = array_map(function($v) {
  53. return $v->getPath();
  54. }, $bundles);
  55. $bundles[] = __DIR__.'/../';
  56. $time = microtime(true);
  57. for ($i=0,$c=5; $i<$c; $i++) {
  58. $ref->invoke($finder, $bundles);
  59. }
  60. $time = microtime(true) - $time;
  61. $this->printResults('service finder ('.$method.')', $time, $c);
  62. }
  63. public function getFinderMethods()
  64. {
  65. return array(
  66. array('findUsingGrep'),
  67. array('findUsingFinder'),
  68. );
  69. }
  70. private function printResults($test, $time, $iterations)
  71. {
  72. if (0 == $iterations) {
  73. throw new InvalidArgumentException('$iterations cannot be zero.');
  74. }
  75. $title = $test." results:\n";
  76. $iterationsText = sprintf("Iterations: %d\n", $iterations);
  77. $totalTime = sprintf("Total Time: %.3f s\n", $time);
  78. $iterationTime = sprintf("Time per iteration: %.3f ms\n", $time/$iterations * 1000);
  79. $max = max(strlen($title), strlen($iterationTime)) - 1;
  80. echo "\n".str_repeat('-', $max)."\n";
  81. echo $title;
  82. echo str_repeat('=', $max)."\n";
  83. echo $iterationsText;
  84. echo $totalTime;
  85. echo $iterationTime;
  86. echo str_repeat('-', $max)."\n";
  87. }
  88. }