MemoryUsageTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento;
  7. class MemoryUsageTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * Number of application reinitialization iterations to be conducted by tests
  11. */
  12. const APP_REINITIALIZATION_LOOPS = 20;
  13. /**
  14. * @var \Magento\TestFramework\Helper\Memory
  15. */
  16. protected $_helper;
  17. protected function setUp()
  18. {
  19. if (defined('HHVM_VERSION')) {
  20. $this->markTestSkipped("Test not relevant because no gc in HHVM.");
  21. }
  22. $this->_helper = new \Magento\TestFramework\Helper\Memory(
  23. new \Magento\Framework\Shell(new \Magento\Framework\Shell\CommandRenderer())
  24. );
  25. }
  26. /**
  27. * Test that application reinitialization produces no memory leaks
  28. */
  29. public function testAppReinitializationNoMemoryLeak()
  30. {
  31. $this->markTestSkipped('Test fails at Travis. Skipped until MAGETWO-47111');
  32. $this->_deallocateUnusedMemory();
  33. $actualMemoryUsage = $this->_helper->getRealMemoryUsage();
  34. for ($i = 0; $i < self::APP_REINITIALIZATION_LOOPS; $i++) {
  35. \Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize();
  36. $this->_deallocateUnusedMemory();
  37. }
  38. $actualMemoryUsage = $this->_helper->getRealMemoryUsage() - $actualMemoryUsage;
  39. $this->assertLessThanOrEqual(
  40. $this->_getAllowedMemoryUsage(),
  41. $actualMemoryUsage,
  42. sprintf(
  43. "Application reinitialization causes the memory leak of %u bytes per %u iterations.",
  44. $actualMemoryUsage,
  45. self::APP_REINITIALIZATION_LOOPS
  46. )
  47. );
  48. }
  49. /**
  50. * Force to deallocate no longer used memory
  51. */
  52. protected function _deallocateUnusedMemory()
  53. {
  54. gc_collect_cycles();
  55. }
  56. /**
  57. * Retrieve the allowed memory usage in bytes, depending on the environment
  58. *
  59. * @return int
  60. */
  61. protected function _getAllowedMemoryUsage()
  62. {
  63. // Memory usage limits should not be further increased, corresponding memory leaks have to be fixed instead!
  64. // @todo fix memory leak and decrease limit to 1 M (in scope of MAGETWO-47693 limit was temporary increased)
  65. return \Magento\TestFramework\Helper\Memory::convertToBytes('2M');
  66. }
  67. }