2010-08-28 14:23:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-08-28 14:23:21 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-08-28 14:23:21 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-08-28 14:23:21 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\HttpKernel\DataCollector;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
2010-08-28 14:23:21 +01:00
|
|
|
/**
|
|
|
|
* MemoryDataCollector.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-08-28 14:23:21 +01:00
|
|
|
*/
|
|
|
|
class MemoryDataCollector extends DataCollector
|
|
|
|
{
|
2012-11-10 16:53:14 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2013-02-10 09:40:27 +00:00
|
|
|
$this->data = array(
|
2013-03-15 16:55:21 +00:00
|
|
|
'memory' => 0,
|
2013-03-23 06:20:57 +00:00
|
|
|
'memory_limit' => $this->convertToBytes(strtolower(ini_get('memory_limit'))),
|
2013-02-10 09:40:27 +00:00
|
|
|
);
|
2012-11-10 16:53:14 +00:00
|
|
|
}
|
|
|
|
|
2010-08-28 14:23:21 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null)
|
|
|
|
{
|
2012-11-10 16:53:14 +00:00
|
|
|
$this->updateMemoryUsage();
|
2010-08-28 14:23:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the memory.
|
|
|
|
*
|
|
|
|
* @return integer The memory
|
|
|
|
*/
|
|
|
|
public function getMemory()
|
|
|
|
{
|
|
|
|
return $this->data['memory'];
|
|
|
|
}
|
|
|
|
|
2013-02-10 09:40:27 +00:00
|
|
|
/**
|
|
|
|
* Gets the PHP memory limit.
|
|
|
|
*
|
|
|
|
* @return integer The memory limit
|
|
|
|
*/
|
|
|
|
public function getMemoryLimit()
|
|
|
|
{
|
|
|
|
return $this->data['memory_limit'];
|
|
|
|
}
|
|
|
|
|
2012-11-10 16:53:14 +00:00
|
|
|
/**
|
|
|
|
* Updates the memory usage data.
|
|
|
|
*/
|
|
|
|
public function updateMemoryUsage()
|
|
|
|
{
|
|
|
|
$this->data['memory'] = memory_get_peak_usage(true);
|
|
|
|
}
|
|
|
|
|
2010-08-28 14:23:21 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'memory';
|
|
|
|
}
|
2013-03-15 16:55:21 +00:00
|
|
|
|
|
|
|
private function convertToBytes($memoryLimit)
|
|
|
|
{
|
2013-03-23 06:20:57 +00:00
|
|
|
if ('-1' === $memoryLimit) {
|
|
|
|
return -1;
|
2013-03-15 16:55:21 +00:00
|
|
|
}
|
|
|
|
|
2013-03-23 06:20:57 +00:00
|
|
|
if (preg_match('#^\+?(0x?)?([^kmg]*)([kmg]?)#', $memoryLimit, $match)) {
|
|
|
|
$shifts = array('' => 0, 'k' => 10, 'm' => 20, 'g' => 30);
|
|
|
|
$bases = array('' => 10, '0' => 8, '0x' => 16);
|
|
|
|
|
|
|
|
return (intval($match[2], $bases[$match[1]]) * (1 << $shifts[$match[3]]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-03-15 16:55:21 +00:00
|
|
|
}
|
2010-08-28 14:23:21 +01:00
|
|
|
}
|