1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-08 16:17:37 +00:00
composer/tests/Composer/Test/DependencyResolver/RuleSetTest.php
rubenrua 4e1887a721 Improve memory usage resolving dependencies
It is known that composer update takes a lot of memory: #5915, #5902,

I am playing with a profiler (@blackfireio) to make a demo in my local
PHP meetup (@phpvigo) and I found out a way to use less memory. These
are my first tests:

* Private project using PHP 5.6:
  * Memory: from 1.31GB to 1.07GB
  * Wall Time: from 2min 8s to 1min 33s

* symfony-demo using PHP 7.1 in my old mac book:
  * Memory: from 667MB to 523MB
  * Wall Time: from  5min 29s to 5min 28s

Not use an array inside conflict rules is this improvement main idea:

```php
<?php
//Memory 38MB
gc_collect_cycles();
gc_disable();

class Rule
{
    public $literals;

    public function __construct(array $literals)
    {
        $this->literals = $literals;
    }
}

$rules = array();

$i = 0;
while ($i<80000){ //
    $i++;

    $array = array(-$i, $i);
    $rule = new Rule($array);
    $rules[] = $rule;
}
```

```php
<?php
//Memory 11.1MB
gc_collect_cycles();
gc_disable();

class Rule2Literals
{
    public $literal1;
    public $literal2;

    public function __construct($literal1, $literal2)
    {
        $this->literal1 = $literal1;
        $this->literal2 = $literal2;
    }
}

$rules = array();

$i = 0;
while ($i<80000){ //
    $i++;

    $rule = new ConflictRule(-$i, $i);
    $rules[] = $rule;
}
```

More info https://github.com/composer/composer/pull/6168
2017-02-20 18:52:17 +00:00

167 lines
5 KiB
PHP

<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\DependencyResolver;
use Composer\DependencyResolver\GenericRule;
use Composer\DependencyResolver\Rule;
use Composer\DependencyResolver\RuleSet;
use Composer\DependencyResolver\Pool;
use Composer\Repository\ArrayRepository;
use Composer\TestCase;
class RuleSetTest extends TestCase
{
protected $pool;
public function setUp()
{
$this->pool = new Pool;
}
public function testAdd()
{
$rules = array(
RuleSet::TYPE_PACKAGE => array(),
RuleSet::TYPE_JOB => array(
new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null),
new GenericRule(array(2), Rule::RULE_JOB_INSTALL, null),
),
RuleSet::TYPE_LEARNED => array(
new GenericRule(array(), Rule::RULE_INTERNAL_ALLOW_UPDATE, null),
),
);
$ruleSet = new RuleSet;
$ruleSet->add($rules[RuleSet::TYPE_JOB][0], RuleSet::TYPE_JOB);
$ruleSet->add($rules[RuleSet::TYPE_LEARNED][0], RuleSet::TYPE_LEARNED);
$ruleSet->add($rules[RuleSet::TYPE_JOB][1], RuleSet::TYPE_JOB);
$this->assertEquals($rules, $ruleSet->getRules());
}
public function testAddIgnoresDuplicates()
{
$rules = array(
RuleSet::TYPE_JOB => array(
new GenericRule(array(), Rule::RULE_JOB_INSTALL, null),
new GenericRule(array(), Rule::RULE_JOB_INSTALL, null),
new GenericRule(array(), Rule::RULE_JOB_INSTALL, null),
)
);
$ruleSet = new RuleSet;
$ruleSet->add($rules[RuleSet::TYPE_JOB][0], RuleSet::TYPE_JOB);
$ruleSet->add($rules[RuleSet::TYPE_JOB][1], RuleSet::TYPE_JOB);
$ruleSet->add($rules[RuleSet::TYPE_JOB][2], RuleSet::TYPE_JOB);
$this->assertCount(1, $ruleSet->getIteratorFor(array(RuleSet::TYPE_JOB)));
}
/**
* @expectedException \OutOfBoundsException
*/
public function testAddWhenTypeIsNotRecognized()
{
$ruleSet = new RuleSet;
$ruleSet->add(new GenericRule(array(), Rule::RULE_JOB_INSTALL, null), 7);
}
public function testCount()
{
$ruleSet = new RuleSet;
$ruleSet->add(new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null), RuleSet::TYPE_JOB);
$ruleSet->add(new GenericRule(array(2), Rule::RULE_JOB_INSTALL, null), RuleSet::TYPE_JOB);
$this->assertEquals(2, $ruleSet->count());
}
public function testRuleById()
{
$ruleSet = new RuleSet;
$rule = new GenericRule(array(), Rule::RULE_JOB_INSTALL, null);
$ruleSet->add($rule, RuleSet::TYPE_JOB);
$this->assertSame($rule, $ruleSet->ruleById[0]);
}
public function testGetIterator()
{
$ruleSet = new RuleSet;
$rule1 = new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null);
$rule2 = new GenericRule(array(2), Rule::RULE_JOB_INSTALL, null);
$ruleSet->add($rule1, RuleSet::TYPE_JOB);
$ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
$iterator = $ruleSet->getIterator();
$this->assertSame($rule1, $iterator->current());
$iterator->next();
$this->assertSame($rule2, $iterator->current());
}
public function testGetIteratorFor()
{
$ruleSet = new RuleSet;
$rule1 = new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null);
$rule2 = new GenericRule(array(2), Rule::RULE_JOB_INSTALL, null);
$ruleSet->add($rule1, RuleSet::TYPE_JOB);
$ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
$iterator = $ruleSet->getIteratorFor(RuleSet::TYPE_LEARNED);
$this->assertSame($rule2, $iterator->current());
}
public function testGetIteratorWithout()
{
$ruleSet = new RuleSet;
$rule1 = new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null);
$rule2 = new GenericRule(array(2), Rule::RULE_JOB_INSTALL, null);
$ruleSet->add($rule1, RuleSet::TYPE_JOB);
$ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
$iterator = $ruleSet->getIteratorWithout(RuleSet::TYPE_JOB);
$this->assertSame($rule2, $iterator->current());
}
public function testPrettyString()
{
$repo = new ArrayRepository;
$repo->addPackage($p = $this->getPackage('foo', '2.1'));
$this->pool->addRepository($repo);
$ruleSet = new RuleSet;
$literal = $p->getId();
$rule = new GenericRule(array($literal), Rule::RULE_JOB_INSTALL, null);
$ruleSet->add($rule, RuleSet::TYPE_JOB);
$this->assertContains('JOB : Install command rule (install foo 2.1)', $ruleSet->getPrettyString($this->pool));
}
private function getRuleMock()
{
return $this->getMockBuilder('Composer\DependencyResolver\Rule')
->disableOriginalConstructor()
->getMock();
}
}