Skip to content
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

Speed improvements when using xdebug #384

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
92 changes: 40 additions & 52 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class PHP_CodeCoverage

/**
* @var bool
*
* @deprecated
*/
private $processUncoveredFilesFromWhitelist = false;

Expand Down Expand Up @@ -86,6 +88,13 @@ class PHP_CodeCoverage
*/
private $tests = array();

/**
* Store all uncovered files
*
* @var array
*/
private $uncoveredFiles = array();

/**
* Constructor.
*
Expand All @@ -105,6 +114,8 @@ public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCove

$this->driver = $driver;
$this->filter = $filter;

$this->initData();
}

/**
Expand Down Expand Up @@ -219,7 +230,7 @@ public function start($id, $clear = false)

$this->currentId = $id;

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

/**
Expand Down Expand Up @@ -250,6 +261,12 @@ public function stop($append = true, $linesToBeCovered = array(), array $linesTo
$data = $this->driver->stop();
$this->append($data, null, $append, $linesToBeCovered, $linesToBeUsed);

foreach (array_keys($data) as $file) {
if (isset($this->uncoveredFiles[$file])) {
unset($this->uncoveredFiles[$file]);
}
}

$this->currentId = null;

return $data;
Expand Down Expand Up @@ -576,7 +593,7 @@ private function applyIgnoredLinesFilter(array &$data)
private function initializeFilesThatAreSeenTheFirstTime(array $data)
{
foreach ($data as $file => $lines) {
if ($this->filter->isFile($file) && !isset($this->data[$file])) {
if ($this->filter->isFile($file) && !isset($this->data[$file]) && !$this->filter()->isFiltered($file)) {
$this->data[$file] = array();

foreach ($lines as $k => $v) {
Expand All @@ -591,62 +608,15 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
*/
private function addUncoveredFilesFromWhitelist()
{
$data = array();
$uncoveredFiles = array_diff(
$this->filter->getWhitelist(),
array_keys($this->data)
);
$data = [];

foreach ($uncoveredFiles as $uncoveredFile) {
if (!file_exists($uncoveredFile)) {
continue;
}

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

$lines = count(file($uncoveredFile));

for ($i = 1; $i <= $lines; $i++) {
$data[$uncoveredFile][$i] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED;
}
}
foreach (array_keys($this->uncoveredFiles) as $file) {
$data[$file] = $this->data[$file];
}

$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] == PHP_CodeCoverage_Driver::LINE_EXECUTED) {
$fileCoverage[$key] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED;
}
}

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

/**
* Returns the lines of a source file that should be ignored.
*
Expand Down Expand Up @@ -917,4 +887,22 @@ private function selectDriver()
return new PHP_CodeCoverage_Driver_Xdebug;
}
}

/**
* Initialise the data with the executable/dead lines from each file in the whitelist
*/
private function initData()
{
foreach ($this->filter()->getWhitelist() as $file) {
$this->driver->start();
include_once($file);
$data = $this->driver->stop();

$this->initializeFilesThatAreSeenTheFirstTime($data);
}

foreach (array_keys($this->data) as $file) {
$this->uncoveredFiles[$file] = 1;
}
}
}
2 changes: 1 addition & 1 deletion src/CodeCoverage/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface PHP_CodeCoverage_Driver
/**
* Start collection of code coverage information.
*/
public function start();
public function start($determineUnusedAndDead = true);

/**
* Stop collection of code coverage information.
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCoverage/Driver/HHVM.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PHP_CodeCoverage_Driver_HHVM extends PHP_CodeCoverage_Driver_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/CodeCoverage/Driver/PHPDBG.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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/CodeCoverage/Driver/Xdebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
*/
class PHP_CodeCoverage_Driver_Xdebug implements PHP_CodeCoverage_Driver
{
/**
* Cache the number of lines for each file
*
* @var array
*/
private $cacheNumLines = [];

/**
* Constructor.
*/
Expand All @@ -36,9 +43,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 @@ -85,13 +96,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];
}
}