2009-06-17 13:36:49 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Core\Tests;
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Core\Environment;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2009-06-17 13:36:49 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class MemoryLimitTest extends SapphireTest
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
protected $origMemLimitMax;
|
|
|
|
protected $origTimeLimitMax;
|
|
|
|
protected $origMemLimit;
|
|
|
|
protected $origTimeLimit;
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// see http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit
|
|
|
|
if (in_array('suhosin', get_loaded_extensions())) {
|
|
|
|
$this->markTestSkipped("This test cannot be run with suhosin installed");
|
|
|
|
} else {
|
|
|
|
$this->origMemLimit = ini_get('memory_limit');
|
|
|
|
$this->origTimeLimit = ini_get('max_execution_time');
|
|
|
|
$this->origMemLimitMax = Environment::getMemoryLimitMax();
|
|
|
|
$this->origTimeLimitMax = Environment::getTimeLimitMax();
|
|
|
|
Environment::setMemoryLimitMax(null);
|
|
|
|
Environment::setTimeLimitMax(null);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function tearDown(): void
|
2017-06-22 12:50:45 +02:00
|
|
|
{
|
|
|
|
if (!in_array('suhosin', get_loaded_extensions())) {
|
|
|
|
ini_set('memory_limit', $this->origMemLimit);
|
|
|
|
set_time_limit($this->origTimeLimit);
|
|
|
|
Environment::setMemoryLimitMax($this->origMemLimitMax);
|
|
|
|
Environment::setTimeLimitMax($this->origTimeLimitMax);
|
|
|
|
}
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIncreaseMemoryLimitTo()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
// ini_set('memory_limit', '64M');
|
|
|
|
// current memory usage in travis is 197M, can't ini_set this down to 64M
|
2021-11-16 00:55:00 +01:00
|
|
|
// for recipe-kitchen-sink, it's 284M
|
2021-10-27 04:39:47 +02:00
|
|
|
// Using a higher memory limit instead
|
2021-11-16 00:55:00 +01:00
|
|
|
ini_set('memory_limit', '330M');
|
|
|
|
Environment::setMemoryLimitMax('512M');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// It can go up
|
2021-11-16 00:55:00 +01:00
|
|
|
Environment::increaseMemoryLimitTo('340M');
|
|
|
|
$this->assertEquals('340M', ini_get('memory_limit'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// But not down
|
2021-11-16 00:55:00 +01:00
|
|
|
Environment::increaseMemoryLimitTo('320M');
|
|
|
|
$this->assertEquals('340M', ini_get('memory_limit'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Test the different kinds of syntaxes
|
2021-11-16 00:55:00 +01:00
|
|
|
Environment::increaseMemoryLimitTo(1024*1024*350);
|
|
|
|
$this->assertEquals('350M', ini_get('memory_limit'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
Environment::increaseMemoryLimitTo('109600K');
|
2021-11-16 00:55:00 +01:00
|
|
|
$this->assertEquals('350M', ini_get('memory_limit'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Attempting to increase past max size only sets to max
|
|
|
|
Environment::increaseMemoryLimitTo('1G');
|
2021-11-16 00:55:00 +01:00
|
|
|
$this->assertEquals('512M', ini_get('memory_limit'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// No argument means unlimited (but only if originally allowed)
|
|
|
|
if (is_numeric($this->origMemLimitMax) && $this->origMemLimitMax < 0) {
|
2019-07-12 06:30:25 +02:00
|
|
|
Environment::setMemoryLimitMax(-1);
|
2017-06-22 12:50:45 +02:00
|
|
|
Environment::increaseMemoryLimitTo();
|
|
|
|
$this->assertEquals(-1, ini_get('memory_limit'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIncreaseTimeLimitTo()
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
// Can't change time limit
|
|
|
|
if (!set_time_limit(6000)) {
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->markTestSkipped("Cannot change time limit");
|
2017-02-22 04:15:08 +01:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// It can go up
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertTrue(Environment::increaseTimeLimitTo(7000));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(7000, ini_get('max_execution_time'));
|
|
|
|
|
|
|
|
// But not down
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertTrue(Environment::increaseTimeLimitTo(5000));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(7000, ini_get('max_execution_time'));
|
|
|
|
|
|
|
|
// 0/nothing means infinity
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertTrue(Environment::increaseTimeLimitTo());
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(0, ini_get('max_execution_time'));
|
|
|
|
|
|
|
|
// Can't go down from there
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertTrue(Environment::increaseTimeLimitTo(10000));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(0, ini_get('max_execution_time'));
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|