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,
|
|
|
|
'memory_limit' => $this->convertToBytes(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)
|
|
|
|
{
|
|
|
|
if (preg_match('#^(\d+)([bkmgt])#i', $memoryLimit, $match)) {
|
|
|
|
$shift = array('b' => 0, 'k' => 10, 'm' => 20, 'g' => 30, 't' => 40);
|
|
|
|
$memoryLimit = ($match[1] * (1 << $shift[strtolower($match[2])]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) $memoryLimit;
|
|
|
|
}
|
2010-08-28 14:23:21 +01:00
|
|
|
}
|