4 * This file is part of the Behat Testwork.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Testwork\Counter;
14 * Counts amount of system memory being used.
16 * @author Konstantin Kudryashov <ever.zet@gmail.com>
23 private $units = array('B', 'Kb', 'Mb', 'Gb', 'Tb');
26 * Returns current memory usage.
30 public function getMemoryUsage()
32 return memory_get_usage();
36 * Presents memory usage in human-readable form.
40 public function __toString()
42 return $this->humanize($this->getMemoryUsage());
46 * Humanizes usage information.
48 * @param integer $bytes
52 private function humanize($bytes)
54 $e = intval(floor(log($bytes) / log(1024)));
56 if (!isset($this->units[$e])) {
57 return 'Can not calculate memory usage';
60 return sprintf('%.2f%s', ($bytes / pow(1024, floor($e))), $this->units[$e]);