Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MemoryUsageTest.php
Go to the documentation of this file.
1 <?php
6 namespace Magento;
7 
8 class MemoryUsageTest extends \PHPUnit\Framework\TestCase
9 {
13  const APP_REINITIALIZATION_LOOPS = 20;
14 
18  protected $_helper;
19 
20  protected function setUp()
21  {
22  if (defined('HHVM_VERSION')) {
23  $this->markTestSkipped("Test not relevant because no gc in HHVM.");
24  }
25  $this->_helper = new \Magento\TestFramework\Helper\Memory(
26  new \Magento\Framework\Shell(new \Magento\Framework\Shell\CommandRenderer())
27  );
28  }
29 
34  {
35  $this->markTestSkipped('Test fails at Travis. Skipped until MAGETWO-47111');
36 
37  $this->_deallocateUnusedMemory();
38  $actualMemoryUsage = $this->_helper->getRealMemoryUsage();
39  for ($i = 0; $i < self::APP_REINITIALIZATION_LOOPS; $i++) {
41  $this->_deallocateUnusedMemory();
42  }
43  $actualMemoryUsage = $this->_helper->getRealMemoryUsage() - $actualMemoryUsage;
44  $this->assertLessThanOrEqual(
45  $this->_getAllowedMemoryUsage(),
46  $actualMemoryUsage,
47  sprintf(
48  "Application reinitialization causes the memory leak of %u bytes per %u iterations.",
49  $actualMemoryUsage,
50  self::APP_REINITIALIZATION_LOOPS
51  )
52  );
53  }
54 
58  protected function _deallocateUnusedMemory()
59  {
60  gc_collect_cycles();
61  }
62 
68  protected function _getAllowedMemoryUsage()
69  {
70  // Memory usage limits should not be further increased, corresponding memory leaks have to be fixed instead!
71  // @todo fix memory leak and decrease limit to 1 M (in scope of MAGETWO-47693 limit was temporary increased)
72  return \Magento\TestFramework\Helper\Memory::convertToBytes('2M');
73  }
74 }
$i
Definition: gallery.phtml:31