Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions
MemoryLimit Class Reference

Public Member Functions

 __construct ($memCap, $leakCap, \Magento\TestFramework\Helper\Memory $helper)
 
 printStats ()
 
 validateUsage ()
 

Static Public Member Functions

static printHeader ()
 

Detailed Description

Definition at line 10 of file MemoryLimit.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $memCap,
  $leakCap,
\Magento\TestFramework\Helper\Memory  $helper 
)

Initialize with the values

Parameters
string$memCap
string$leakCap
\Magento\TestFramework\Helper\Memory$helper
Exceptions

Definition at line 35 of file MemoryLimit.php.

36  {
37  $this->_memCap = $memCap ? $helper->convertToBytes($memCap) : 0;
38  $this->_leakCap = $leakCap ? $helper->convertToBytes($leakCap) : 0;
39  $this->_helper = $helper;
40  }
$helper
Definition: iframe.phtml:13

Member Function Documentation

◆ printHeader()

static printHeader ( )
static

Get a header printout

Returns
string

Definition at line 47 of file MemoryLimit.php.

48  {
49  return PHP_EOL . '=== Memory Usage System Stats ===' . PHP_EOL;
50  }

◆ printStats()

printStats ( )

Get statistics printout

Returns
string

Definition at line 57 of file MemoryLimit.php.

58  {
59  list($usage, $leak) = $this->_getUsage();
60  $result = [];
61 
62  $msg = sprintf(
63  "Memory usage (OS):\t%s (%.2F%% of %s reported by PHP",
64  $this->_toMb($usage),
65  100 * $usage / ($usage - $leak),
66  $this->_toMb($usage - $leak)
67  );
68  $percentMsg = '%.2F%% of configured %s limit';
69  if ($this->_memCap) {
70  $msg .= ', ' . sprintf($percentMsg, 100 * $usage / $this->_memCap, $this->_toMb($this->_memCap));
71  }
72  $result[] = "{$msg})";
73 
74  $msg = sprintf("Estimated memory leak:\t%s (%.2F%% of used memory", $this->_toMb($leak), 100 * $leak / $usage);
75  if ($this->_leakCap) {
76  $msg .= ', ' . sprintf($percentMsg, 100 * $leak / $this->_leakCap, $this->_toMb($this->_leakCap));
77  }
78  $result[] = "{$msg})";
79 
80  return implode(PHP_EOL, $result) . PHP_EOL;
81  }

◆ validateUsage()

validateUsage ( )

Raise error if memory usage breaks configured thresholds

Returns
null
Exceptions

Definition at line 100 of file MemoryLimit.php.

101  {
102  if (!$this->_memCap && !$this->_leakCap) {
103  return null;
104  }
105  list($usage, $leak) = $this->_getUsage();
106  if ($this->_memCap && $usage >= $this->_memCap) {
107  throw new \LogicException(
108  "Memory limit of {$this->_toMb($this->_memCap)} ({$this->_memCap} bytes) has been reached."
109  );
110  }
111  if ($this->_leakCap && $leak >= $this->_leakCap) {
112  throw new \LogicException(
113  "Estimated memory leak limit of {$this->_toMb(
114  $this->_leakCap
115  )}" . " ({$this->_leakCap} bytes) has been reached."
116  );
117  }
118  }

The documentation for this class was generated from the following file: