Skip to content

Speedup #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 62 additions & 33 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ class CodeCoverage
*/
private $unintentionallyCoveredSubclassesWhitelist = [];

/**
* Determine if the data has been initialized or not
*
* @var bool
*/
private $isInitialized = false;

/**
* Determine whether we need to check for dead and unused code on each test
*
* @var bool
*/
private $shouldCheckForDeadAndUnused = true;

/**
* Constructor.
*
Expand Down Expand Up @@ -154,6 +168,7 @@ public function getReport()
*/
public function clear()
{
$this->isInitialized = false;
$this->currentId = null;
$this->data = [];
$this->tests = [];
Expand Down Expand Up @@ -237,9 +252,13 @@ public function start($id, $clear = false)
$this->clear();
}

if ($this->isInitialized === false) {
$this->initializeData();
}

$this->currentId = $id;

$this->driver->start();
$this->driver->start($this->shouldCheckForDeadAndUnused);
}

/**
Expand Down Expand Up @@ -687,13 +706,7 @@ private function addUncoveredFilesFromWhitelist()
continue;
}

if ($this->processUncoveredFilesFromWhitelist) {
$this->processUncoveredFileFromWhitelist(
$uncoveredFile,
$data,
$uncoveredFiles
);
} else {
if (!$this->processUncoveredFilesFromWhitelist) {
$data[$uncoveredFile] = [];

$lines = count(file($uncoveredFile));
Expand All @@ -707,31 +720,6 @@ private function addUncoveredFilesFromWhitelist()
$this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST');
}

/**
* @param string $uncoveredFile
* @param array $data
* @param array $uncoveredFiles
*/
private function processUncoveredFileFromWhitelist($uncoveredFile, array &$data, array $uncoveredFiles)
{
$this->driver->start();
include_once $uncoveredFile;
$coverage = $this->driver->stop();

foreach ($coverage as $file => $fileCoverage) {
if (!isset($data[$file]) &&
in_array($file, $uncoveredFiles)) {
foreach (array_keys($fileCoverage) as $key) {
if ($fileCoverage[$key] == Driver::LINE_EXECUTED) {
$fileCoverage[$key] = Driver::LINE_NOT_EXECUTED;
}
}

$data[$file] = $fileCoverage;
}
}
}

/**
* Returns the lines of a source file that should be ignored.
*
Expand Down Expand Up @@ -1074,4 +1062,45 @@ private function processUnintentionallyCoveredUnits(array $unintentionallyCovere

return array_values($unintentionallyCoveredUnits);
}

/**
* If we are processing uncovered files from whitelist,
* we can initialize the data before we start to speed up the tests
*/
protected function initializeData()
{
$this->isInitialized = true;

if ($this->processUncoveredFilesFromWhitelist) {

$this->shouldCheckForDeadAndUnused = false;

$this->driver->start(true);

foreach ($this->filter->getWhitelist() as $file) {
if ($this->filter->isFile($file)) {
include_once($file);
}
}

$data = [];
$coverage = $this->driver->stop();

foreach ($coverage as $file => $fileCoverage) {
if ($this->filter->isFiltered($file)) {
continue;
}

foreach (array_keys($fileCoverage) as $key) {
if ($fileCoverage[$key] == Driver::LINE_EXECUTED) {
$fileCoverage[$key] = Driver::LINE_NOT_EXECUTED;
}
}

$data[$file] = $fileCoverage;
}

$this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST');
}
}
}
2 changes: 1 addition & 1 deletion src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface Driver
/**
* Start collection of code coverage information.
*/
public function start();
public function start($determineUnusedAndDead = true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot to update the phpdoc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I've added these now


/**
* Stop collection of code coverage information.
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/HHVM.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class HHVM extends Xdebug
/**
* Start collection of code coverage information.
*/
public function start()
public function start($determineUnusedAndDead = true)
{
xdebug_start_code_coverage();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/PHPDBG.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct()
/**
* Start collection of code coverage information.
*/
public function start()
public function start($determineUnusedAndDead = true)
{
phpdbg_start_oplog();
}
Expand Down
29 changes: 22 additions & 7 deletions src/Driver/Xdebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
*/
class Xdebug implements Driver
{
/**
* Cache the number of lines for each file
*
* @var array
*/
private $cacheNumLines = [];

/**
* Constructor.
*/
Expand All @@ -39,9 +46,13 @@ public function __construct()
/**
* Start collection of code coverage information.
*/
public function start()
public function start($determineUnusedAndDead = true)
{
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
if ($determineUnusedAndDead) {
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
} else {
xdebug_start_code_coverage();
}
}

/**
Expand Down Expand Up @@ -88,13 +99,17 @@ private function cleanup(array $data)
*/
private function getNumberOfLinesInFile($file)
{
$buffer = file_get_contents($file);
$lines = substr_count($buffer, "\n");
if (!isset($this->cacheNumLines[$file])) {
$buffer = file_get_contents($file);
$lines = substr_count($buffer, "\n");

if (substr($buffer, -1) !== "\n") {
$lines++;
}

if (substr($buffer, -1) !== "\n") {
$lines++;
$this->cacheNumLines[$file] = $lines;
}

return $lines;
return $this->cacheNumLines[$file];
}
}