mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-08 14:16:42 +02:00
Add method in Utils to convert memory values from php_ini into bytes, and use lower amount of chunk size based on memory limit
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Test\TestCase;
|
||||
use Monolog\Logger;
|
||||
|
||||
@@ -220,4 +221,90 @@ class StreamHandlerTest extends TestCase
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function provideMemoryValues()
|
||||
{
|
||||
return [
|
||||
['1M', true],
|
||||
['10M', true],
|
||||
['1024M', true],
|
||||
['1G', true],
|
||||
['2000M', true],
|
||||
['2050M', true],
|
||||
['2048M', true],
|
||||
['3G', false],
|
||||
['2560M', false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideMemoryValues
|
||||
* @return void
|
||||
*/
|
||||
public function testPreventOOMError($phpMemory, $chunkSizeDecreased)
|
||||
{
|
||||
$memoryLimit = ini_set('memory_limit', $phpMemory);
|
||||
|
||||
if ($memoryLimit === false) {
|
||||
/*
|
||||
* We could not set a memory limit that would trigger the error.
|
||||
* There is no need to continue with this test.
|
||||
*/
|
||||
|
||||
$this->assertTrue(true);
|
||||
return;
|
||||
}
|
||||
|
||||
$stream = tmpfile();
|
||||
|
||||
if ($stream === false) {
|
||||
/*
|
||||
* We could create a temp file to be use as a stream.
|
||||
* There is no need to continue with this test.
|
||||
*/
|
||||
$this->assertTrue(true);
|
||||
return;
|
||||
}
|
||||
|
||||
$exceptionRaised = false;
|
||||
|
||||
try {
|
||||
$handler = new StreamHandler($stream);
|
||||
stream_get_contents($stream, 1024);
|
||||
} catch (\RuntimeException $exception) {
|
||||
/*
|
||||
* At this point, stream_set_chunk_size() failed in the constructor.
|
||||
* Probably because not enough memory.
|
||||
* The rest of th test depends on the success pf stream_set_chunk_size(), that is why
|
||||
* if exception is raised (which is true at this point), the rest of assertions will not be executed.
|
||||
*/
|
||||
$exceptionRaised = true;
|
||||
}
|
||||
|
||||
ini_set('memory_limit', $memoryLimit);
|
||||
$this->assertEquals($memoryLimit, ini_get('memory_limit'));
|
||||
|
||||
if (!$exceptionRaised) {
|
||||
$this->assertEquals($chunkSizeDecreased, $handler->getStreamChunkSize() < StreamHandler::MAX_CHUNK_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimpleOOMPrevention()
|
||||
{
|
||||
$previousValue = ini_set('memory_limit', '2048M');
|
||||
|
||||
if ($previousValue === false) {
|
||||
$this->assertTrue(true);
|
||||
return;
|
||||
}
|
||||
|
||||
$stream = tmpfile();
|
||||
new StreamHandler($stream);
|
||||
stream_get_contents($stream);
|
||||
ini_set('memory_limit', $previousValue);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
@@ -141,4 +141,47 @@ class UtilsTest extends \PHPUnit_Framework_TestCase
|
||||
[-1, 'UNDEFINED_ERROR'],
|
||||
];
|
||||
}
|
||||
|
||||
public function provideMemoryIniValuesToConvertToBytes()
|
||||
{
|
||||
return [
|
||||
['1', 1],
|
||||
['2', 2],
|
||||
['2.5', 2],
|
||||
['2.9', 2],
|
||||
['1B', 1],
|
||||
['1X', 1],
|
||||
['1K', 1024],
|
||||
['1G', 1073741824],
|
||||
['', false],
|
||||
[null, false],
|
||||
['A', false],
|
||||
['AA', false],
|
||||
['B', false],
|
||||
['BB', false],
|
||||
['G', false],
|
||||
['GG', false],
|
||||
['-1', false],
|
||||
['-123', false],
|
||||
['-1A', false],
|
||||
['-1B', false],
|
||||
['-123G', false],
|
||||
['-B', false],
|
||||
['-A', false],
|
||||
['-', false],
|
||||
[true, false],
|
||||
[false, false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideMemoryIniValuesToConvertToBytes
|
||||
* @param mixed $input
|
||||
* @param int|false $expected
|
||||
*/
|
||||
public function testMemoryIniValueToBytes($input, $expected)
|
||||
{
|
||||
$result = Utils::memoryIniValueToBytes($input);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user