-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Memory.php
160 lines (146 loc) · 5.19 KB
/
Memory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Helper for determining system memory usage
*
* Uses OS tools to provide accurate information about factual memory consumption.
* The PHP standard functions may return incorrect information because the process itself may have leaks.
*
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @copyright Copyright (c) 2013 X.commerce, Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Magento_Test_Helper_Memory
{
/**
* Prefixes to specify unit of measure for memory amount
*
* Warning: it is important to maintain the exact order of letters in this literal,
* as it is used to convert string with units to bytes
*/
const MEMORY_UNITS = 'BKMGTPE';
/**
* @var Magento_Shell
*/
private $_shell;
/**
* Inject dependencies
*
* @param Magento_Shell $shell
*/
public function __construct(Magento_Shell $shell)
{
$this->_shell = $shell;
}
/**
* Retrieve the effective memory usage of the current process
*
* memory_get_usage() cannot be used because of the bug
* @link https://bugs.php.net/bug.php?id=62467
*
* @return int Memory usage in bytes
*/
public function getRealMemoryUsage()
{
$pid = getmypid();
if (self::isWindowsOs()) {
$result = $this->getWinProcessMemoryUsage($pid);
} else {
$result = $this->getUnixProcessMemoryUsage($pid);
}
return $result;
}
/**
* Retrieve the current process' memory usage using Unix command line interface
*
* @link http://linux.die.net/man/1/top
* @param int $pid
* @return int Memory usage in bytes
*/
public function getUnixProcessMemoryUsage($pid)
{
$output = $this->_shell->execute('top -p %s -n 1 -b | grep PID -A 1', array($pid));
$output = preg_split('/\n+/', $output, -1, PREG_SPLIT_NO_EMPTY);
$keys = preg_split('/\s+/', $output[0], -1, PREG_SPLIT_NO_EMPTY);
$values = preg_split('/\s+/', $output[1], -1, PREG_SPLIT_NO_EMPTY);
$stats = array_combine($keys, $values);
$result = $stats['RES']; // resident set size, the non-swapped physical memory
if (is_numeric($result)) {
$result .= 'k'; // kilobytes by default
}
return self::convertToBytes($result);
}
/**
* Retrieve the current process' memory usage using Windows command line interface
*
* @link http://technet.microsoft.com/en-us/library/bb491010.aspx
* @param int $pid
* @return int Memory usage in bytes
*/
public function getWinProcessMemoryUsage($pid)
{
$output = $this->_shell->execute('tasklist /fi %s /fo CSV', array("PID eq $pid"));
/** @link http://www.php.net/manual/en/wrappers.data.php */
$csvStream = 'data://text/plain;base64,' . base64_encode($output);
$csvHandle = fopen($csvStream, 'r');
$keys = fgetcsv($csvHandle);
$values = fgetcsv($csvHandle);
fclose($csvHandle);
$stats = array_combine($keys, $values);
$result = $stats['Mem Usage'];
return self::convertToBytes($result);
}
/**
* Whether the operating system belongs to the Windows family
*
* @link http://php.net/manual/en/function.php-uname.php
* @return bool
*/
public static function isWindowsOs()
{
return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
}
/**
* Convert a number optionally followed by the unit symbol (B, K, M, G, etc.) to bytes
*
* @param string $number String representation of a number
* @return int
* @throws InvalidArgumentException
* @throws OutOfBoundsException
*/
public static function convertToBytes($number)
{
$number = str_replace(array(',', ' '), '', $number);
$number = strtoupper($number);
if (!preg_match('/^(\d+(?:\.\d+)?)([' . self::MEMORY_UNITS . ']?)$/', $number, $matches)) {
throw new InvalidArgumentException("Number format '$number' is not recognized.");
}
$result = (float)$matches[1];
$unitSymbol = $matches[2];
$pow = $unitSymbol ? strpos(self::MEMORY_UNITS, $unitSymbol) : 0;
$is32Bit = PHP_INT_SIZE == 4;
if ($is32Bit && $pow >= 4) {
throw new OutOfBoundsException("A 32-bit system is unable to process such a number.");
}
if ($unitSymbol) {
$result *= pow(1024, $pow);
}
return (int)$result;
}
}