Skip to content

Commit

Permalink
merged branch jfsimon/issue-7037 (PR #7395)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Commits
-------

a011842 [HttpKernel] fixed memory collector
def2ccb Add PHP memory_limit to WDT

Discussion
----------

[HttpKernel] fixed memory collector

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #7037

---------------------------------------------------------------------------

by vicb at 2013-03-15T20:21:55Z

OT, that's great to see many contributions for you @jfsimon, thanks !

---------------------------------------------------------------------------

by jfsimon at 2013-03-18T09:35:54Z

Thanks @vicb, this is so lovely :)
  • Loading branch information
fabpot committed Mar 18, 2013
2 parents d832dbf + a011842 commit 84ff273
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
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
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;
}
}
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),
);
}
}

0 comments on commit 84ff273

Please sign in to comment.