Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a011842

Browse files
committed
[HttpKernel] fixed memory collector
1 parent def2ccb commit a011842

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/memory.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{% set text %}
1111
<div class="sf-toolbar-info-piece">
1212
<b>Memory usage</b>
13-
<span>{{ '%.1f'|format(collector.memory / 1024 / 1024) }} / {{ collector.memoryLimit == -1 ? '&infin;' : collector.memoryLimit }} MB</span>
13+
<span>{{ '%.1f'|format(collector.memory / 1024 / 1024) }} / {{ collector.memoryLimit == -1 ? '&infin;' : '%.1f'|format(collector.memoryLimit / 1024 / 1024)|escape }} MB</span>
1414
</div>
1515
{% endset %}
1616
{% include '@WebProfiler/Profiler/toolbar_item.html.twig' with { 'link': false } %}

src/Symfony/Component/HttpKernel/DataCollector/MemoryDataCollector.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class MemoryDataCollector extends DataCollector
2424
public function __construct()
2525
{
2626
$this->data = array(
27-
'memory' => 0,
28-
'memory_limit' => rtrim(ini_get('memory_limit'), 'M')
27+
'memory' => 0,
28+
'memory_limit' => $this->convertToBytes(ini_get('memory_limit')),
2929
);
3030
}
3131

@@ -72,4 +72,14 @@ public function getName()
7272
{
7373
return 'memory';
7474
}
75+
76+
private function convertToBytes($memoryLimit)
77+
{
78+
if (preg_match('#^(\d+)([bkmgt])#i', $memoryLimit, $match)) {
79+
$shift = array('b' => 0, 'k' => 10, 'm' => 20, 'g' => 30, 't' => 40);
80+
$memoryLimit = ($match[1] * (1 << $shift[strtolower($match[2])]));
81+
}
82+
83+
return (int) $memoryLimit;
84+
}
7585
}

src/Symfony/Component/HttpKernel/Tests/DataCollector/MemoryDataCollectorTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,30 @@ protected function setUp()
2626

2727
public function testCollect()
2828
{
29-
$c = new MemoryDataCollector();
29+
$collector = new MemoryDataCollector();
30+
$collector->collect(new Request(), new Response());
3031

31-
$c->collect(new Request(), new Response());
32+
$this->assertInternalType('integer', $collector->getMemory());
33+
$this->assertInternalType('integer', $collector->getMemoryLimit());
34+
$this->assertSame('memory', $collector->getName());
35+
}
3236

33-
$this->assertInternalType('integer',$c->getMemory());
34-
$this->assertSame('memory',$c->getName());
37+
/** @dataProvider getBytesConversionTestData */
38+
public function testBytesConversion($limit, $bytes)
39+
{
40+
$collector = new MemoryDataCollector();
41+
$method = new \ReflectionMethod($collector, 'convertToBytes');
42+
$method->setAccessible(true);
43+
$this->assertEquals($bytes, $method->invoke($collector, $limit));
3544
}
3645

46+
public function getBytesConversionTestData()
47+
{
48+
return array(
49+
array('-1', -1),
50+
array('2k', 2048),
51+
array('2K', 2048),
52+
array('1g', 1024 * 1024 * 1024),
53+
);
54+
}
3755
}

0 commit comments

Comments
 (0)