Skip to content

[HttpKernel] fixed memory collector #7395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% set text %}
<div class="sf-toolbar-info-piece">
<b>Memory usage</b>
<span>{{ '%.1f'|format(collector.memory / 1024 / 1024) }} MB</span>
<span>{{ '%.1f'|format(collector.memory / 1024 / 1024) }} / {{ collector.memoryLimit == -1 ? '&infin;' : '%.1f'|format(collector.memoryLimit / 1024 / 1024)|escape }} MB</span>
</div>
{% endset %}
{% include '@WebProfiler/Profiler/toolbar_item.html.twig' with { 'link': false } %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class MemoryDataCollector extends DataCollector
{
public function __construct()
{
$this->data = array('memory' => 0);
$this->data = array(
'memory' => 0,
'memory_limit' => $this->convertToBytes(ini_get('memory_limit')),
);
}

/**
Expand All @@ -44,6 +47,16 @@ public function getMemory()
return $this->data['memory'];
}

/**
* Gets the PHP memory limit.
*
* @return integer The memory limit
*/
public function getMemoryLimit()
{
return $this->data['memory_limit'];
}

/**
* Updates the memory usage data.
*/
Expand All @@ -59,4 +72,14 @@ public function getName()
{
return 'memory';
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,30 @@ protected function setUp()

public function testCollect()
{
$c = new MemoryDataCollector();
$collector = new MemoryDataCollector();
$collector->collect(new Request(), new Response());

$c->collect(new Request(), new Response());
$this->assertInternalType('integer', $collector->getMemory());
$this->assertInternalType('integer', $collector->getMemoryLimit());
$this->assertSame('memory', $collector->getName());
}

$this->assertInternalType('integer',$c->getMemory());
$this->assertSame('memory',$c->getName());
/** @dataProvider getBytesConversionTestData */
public function testBytesConversion($limit, $bytes)
{
$collector = new MemoryDataCollector();
$method = new \ReflectionMethod($collector, 'convertToBytes');
$method->setAccessible(true);
$this->assertEquals($bytes, $method->invoke($collector, $limit));
}

public function getBytesConversionTestData()
{
return array(
array('-1', -1),
array('2k', 2048),
array('2K', 2048),
array('1g', 1024 * 1024 * 1024),
);
}
}