Skip to content

Commit

Permalink
Fixes Out-of-memory errors during Scrutinizer run (#443)
Browse files Browse the repository at this point in the history
* run gc_collect_cycles in testParseFile to avoid out-of-memory error

* Added gc_collect_cycles() to setUp

* Update ParserTest.php

* Update ParserTest.php

* simplified ParserTest::testParseFile

only test a given set of files, not all in samples/bugs

* check if scrutinizer fails only when executing testParseFile

* restored ParserTest::testParseFile; refined testRetainImageContentImpact

* restored old version of testParseFile; marked it and other tests as memory-heavy ...

... exclude all memory-heavy tests from scrutinizer run

* refined memory checks in testRetainImageContentImpact

* increased memory value in testRetainImageContentImpact: Reason is, that it otherwise fails in Github because
more memory is used as in local development environment. Its still not an error!
  • Loading branch information
k00ni authored Aug 3, 2021
1 parent 1048d7e commit 43e436f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build:
# to not enough memory.
# @https://github.com/smalot/pdfparser/issues/410
# @https://github.com/smalot/pdfparser/pull/412
command: make prepare-for-scrutinizer && make install-dev-tools && make run-phpunit ARGS="--coverage-clover coverage/clover.xml"
command: make prepare-for-scrutinizer && make install-dev-tools && make run-phpunit ARGS="--exclude-group memory-heavy --coverage-clover coverage/clover.xml"
coverage:
file: coverage/clover.xml
format: clover
Expand Down
34 changes: 29 additions & 5 deletions tests/Integration/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ protected function setUp(): void
$this->fixture = new Parser();
}

/**
* Notice: it may fail to run in Scrutinizer because of memory limitations.
*
* @group memory-heavy
*/
public function testParseFile()
{
$directory = $this->rootDir.'/samples/bugs';
Expand Down Expand Up @@ -305,6 +310,8 @@ public function testUsageOfConfigObject()
/**
* Tests the impact of the retainImageContent config setting on memory usage
*
* @group memory-heavy
*
* @see https://github.com/smalot/pdfparser/issues/104#issuecomment-883422508
*/
public function testRetainImageContentImpact()
Expand All @@ -314,29 +321,46 @@ public function testRetainImageContentImpact()
}

$filename = $this->rootDir.'/samples/bugs/Issue104a.pdf';
$iterations = 2;
$iterations = 1;

// check default (= true)
/*
* check default (= true)
*/
$this->fixture = new Parser([]);
$this->assertTrue($this->fixture->getConfig()->getRetainImageContent());
$document = null;

for ($i = 0; $i < $iterations; ++$i) {
$document = $this->fixture->parseFile($filename);
}

$this->assertTrue(memory_get_usage() > 200000000);
$usedMemory = memory_get_usage(true);
$this->assertTrue($usedMemory > 100000000, 'Memory is only '.$usedMemory);
$this->assertTrue(null != $document && 0 < \strlen($document->getText()));

// force garbage collection
$this->fixture = $document = null;
gc_collect_cycles();

// check false
/*
* check false
*/
$config = new Config();
$config->setRetainImageContent(false);
$this->fixture = new Parser([], $config);
$this->assertEquals($config, $this->fixture->getConfig());

$this->assertTrue(memory_get_usage() < 200000000);
for ($i = 0; $i < $iterations; ++$i) {
$document = $this->fixture->parseFile($filename);
}

$usedMemory = memory_get_usage(true);
/*
* note: the following memory value is set manually and may differ from system to system.
* it must be high enough to not produce a false negative though.
*/
$this->assertTrue($usedMemory < 106000000, 'Memory is '.$usedMemory);
$this->assertTrue(0 < \strlen($document->getText()));
}
}

Expand Down

0 comments on commit 43e436f

Please sign in to comment.