Skip to content

Commit b28af18

Browse files
Rob Caigersebastianbergmann
Rob Caiger
authored andcommitted
Speed improvements when using xdebug
- Init data by running xdebug with 'unused' and 'dead code' flags on all whitelist files - Run xdebug without flags for each test method - Cache num lines check for each file, to prevent superflous checks - (SB) Fix CS/WS issues
1 parent 85a7b89 commit b28af18

File tree

5 files changed

+87
-43
lines changed

5 files changed

+87
-43
lines changed

src/CodeCoverage.php

+62-33
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ class PHP_CodeCoverage
8181
*/
8282
private $tests = [];
8383

84+
/**
85+
* Determine if the data has been initialized or not
86+
*
87+
* @var bool
88+
*/
89+
private $isInitialized = false;
90+
91+
/**
92+
* Determine whether we need to check for dead and unused code on each test
93+
*
94+
* @var bool
95+
*/
96+
private $shouldCheckForDeadAndUnused = true;
97+
8498
/**
8599
* Constructor.
86100
*
@@ -121,6 +135,7 @@ public function getReport()
121135
*/
122136
public function clear()
123137
{
138+
$this->isInitialized = false;
124139
$this->currentId = null;
125140
$this->data = [];
126141
$this->tests = [];
@@ -206,9 +221,13 @@ public function start($id, $clear = false)
206221
$this->clear();
207222
}
208223

224+
if ($this->isInitialized === false) {
225+
$this->initializeData();
226+
}
227+
209228
$this->currentId = $id;
210229

211-
$this->driver->start();
230+
$this->driver->start($this->shouldCheckForDeadAndUnused);
212231
}
213232

214233
/**
@@ -580,13 +599,7 @@ private function addUncoveredFilesFromWhitelist()
580599
continue;
581600
}
582601

583-
if ($this->processUncoveredFilesFromWhitelist) {
584-
$this->processUncoveredFileFromWhitelist(
585-
$uncoveredFile,
586-
$data,
587-
$uncoveredFiles
588-
);
589-
} else {
602+
if (!$this->processUncoveredFilesFromWhitelist) {
590603
$data[$uncoveredFile] = [];
591604

592605
$lines = count(file($uncoveredFile));
@@ -600,31 +613,6 @@ private function addUncoveredFilesFromWhitelist()
600613
$this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST');
601614
}
602615

603-
/**
604-
* @param string $uncoveredFile
605-
* @param array $data
606-
* @param array $uncoveredFiles
607-
*/
608-
private function processUncoveredFileFromWhitelist($uncoveredFile, array &$data, array $uncoveredFiles)
609-
{
610-
$this->driver->start();
611-
include_once $uncoveredFile;
612-
$coverage = $this->driver->stop();
613-
614-
foreach ($coverage as $file => $fileCoverage) {
615-
if (!isset($data[$file]) &&
616-
in_array($file, $uncoveredFiles)) {
617-
foreach (array_keys($fileCoverage) as $key) {
618-
if ($fileCoverage[$key] == PHP_CodeCoverage_Driver::LINE_EXECUTED) {
619-
$fileCoverage[$key] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED;
620-
}
621-
}
622-
623-
$data[$file] = $fileCoverage;
624-
}
625-
}
626-
}
627-
628616
/**
629617
* Returns the lines of a source file that should be ignored.
630618
*
@@ -897,4 +885,45 @@ private function selectDriver()
897885
return new PHP_CodeCoverage_Driver_Xdebug;
898886
}
899887
}
888+
889+
/**
890+
* If we are processing uncovered files from whitelist,
891+
* we can initialize the data before we start to speed up the tests
892+
*/
893+
protected function initializeData()
894+
{
895+
$this->isInitialized = true;
896+
897+
if ($this->processUncoveredFilesFromWhitelist) {
898+
899+
$this->shouldCheckForDeadAndUnused = false;
900+
901+
$this->driver->start(true);
902+
903+
foreach ($this->filter->getWhitelist() as $file) {
904+
if ($this->filter->isFile($file)) {
905+
include_once($file);
906+
}
907+
}
908+
909+
$data = [];
910+
$coverage = $this->driver->stop();
911+
912+
foreach ($coverage as $file => $fileCoverage) {
913+
if ($this->filter->isFiltered($file)) {
914+
continue;
915+
}
916+
917+
foreach (array_keys($fileCoverage) as $key) {
918+
if ($fileCoverage[$key] == PHP_CodeCoverage_Driver::LINE_EXECUTED) {
919+
$fileCoverage[$key] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED;
920+
}
921+
}
922+
923+
$data[$file] = $fileCoverage;
924+
}
925+
926+
$this->append($coverage, 'UNCOVERED_FILES_FROM_WHITELIST');
927+
}
928+
}
900929
}

src/CodeCoverage/Driver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ interface PHP_CodeCoverage_Driver
3636
/**
3737
* Start collection of code coverage information.
3838
*/
39-
public function start();
39+
public function start($determineUnusedAndDead = true);
4040

4141
/**
4242
* Stop collection of code coverage information.

src/CodeCoverage/Driver/HHVM.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class PHP_CodeCoverage_Driver_HHVM extends PHP_CodeCoverage_Driver_Xdebug
1919
/**
2020
* Start collection of code coverage information.
2121
*/
22-
public function start()
22+
public function start($determineUnusedAndDead = true)
2323
{
2424
xdebug_start_code_coverage();
2525
}

src/CodeCoverage/Driver/PHPDBG.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct()
3737
/**
3838
* Start collection of code coverage information.
3939
*/
40-
public function start()
40+
public function start($determineUnusedAndDead = true)
4141
{
4242
phpdbg_start_oplog();
4343
}

src/CodeCoverage/Driver/Xdebug.php

+22-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
*/
1717
class PHP_CodeCoverage_Driver_Xdebug implements PHP_CodeCoverage_Driver
1818
{
19+
/**
20+
* Cache the number of lines for each file
21+
*
22+
* @var array
23+
*/
24+
private $cacheNumLines = [];
25+
1926
/**
2027
* Constructor.
2128
*/
@@ -36,9 +43,13 @@ public function __construct()
3643
/**
3744
* Start collection of code coverage information.
3845
*/
39-
public function start()
46+
public function start($determineUnusedAndDead = true)
4047
{
41-
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
48+
if ($determineUnusedAndDead) {
49+
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
50+
} else {
51+
xdebug_start_code_coverage();
52+
}
4253
}
4354

4455
/**
@@ -85,13 +96,17 @@ private function cleanup(array $data)
8596
*/
8697
private function getNumberOfLinesInFile($file)
8798
{
88-
$buffer = file_get_contents($file);
89-
$lines = substr_count($buffer, "\n");
99+
if (!isset($this->cacheNumLines[$file])) {
100+
$buffer = file_get_contents($file);
101+
$lines = substr_count($buffer, "\n");
102+
103+
if (substr($buffer, -1) !== "\n") {
104+
$lines++;
105+
}
90106

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

95-
return $lines;
110+
return $this->cacheNumLines[$file];
96111
}
97112
}

0 commit comments

Comments
 (0)