Skip to content

Commit 91ace14

Browse files
author
Rob Caiger
committed
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 c84f05b commit 91ace14

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
@@ -86,6 +86,20 @@ class PHP_CodeCoverage
8686
*/
8787
private $tests = [];
8888

89+
/**
90+
* Determine if the data has been initialized or not
91+
*
92+
* @var bool
93+
*/
94+
private $isInitialized = false;
95+
96+
/**
97+
* Determine whether we need to check for dead and unused code on each test
98+
*
99+
* @var bool
100+
*/
101+
private $shouldCheckForDeadAndUnused = true;
102+
89103
/**
90104
* Constructor.
91105
*
@@ -126,6 +140,7 @@ public function getReport()
126140
*/
127141
public function clear()
128142
{
143+
$this->isInitialized = false;
129144
$this->currentId = null;
130145
$this->data = [];
131146
$this->tests = [];
@@ -211,9 +226,13 @@ public function start($id, $clear = false)
211226
$this->clear();
212227
}
213228

229+
if ($this->isInitialized === false) {
230+
$this->initializeData();
231+
}
232+
214233
$this->currentId = $id;
215234

216-
$this->driver->start();
235+
$this->driver->start($this->shouldCheckForDeadAndUnused);
217236
}
218237

219238
/**
@@ -592,13 +611,7 @@ private function addUncoveredFilesFromWhitelist()
592611
continue;
593612
}
594613

595-
if ($this->processUncoveredFilesFromWhitelist) {
596-
$this->processUncoveredFileFromWhitelist(
597-
$uncoveredFile,
598-
$data,
599-
$uncoveredFiles
600-
);
601-
} else {
614+
if (!$this->processUncoveredFilesFromWhitelist) {
602615
$data[$uncoveredFile] = [];
603616

604617
$lines = count(file($uncoveredFile));
@@ -612,31 +625,6 @@ private function addUncoveredFilesFromWhitelist()
612625
$this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST');
613626
}
614627

615-
/**
616-
* @param string $uncoveredFile
617-
* @param array $data
618-
* @param array $uncoveredFiles
619-
*/
620-
private function processUncoveredFileFromWhitelist($uncoveredFile, array &$data, array $uncoveredFiles)
621-
{
622-
$this->driver->start();
623-
include_once $uncoveredFile;
624-
$coverage = $this->driver->stop();
625-
626-
foreach ($coverage as $file => $fileCoverage) {
627-
if (!isset($data[$file]) &&
628-
in_array($file, $uncoveredFiles)) {
629-
foreach (array_keys($fileCoverage) as $key) {
630-
if ($fileCoverage[$key] == PHP_CodeCoverage_Driver::LINE_EXECUTED) {
631-
$fileCoverage[$key] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED;
632-
}
633-
}
634-
635-
$data[$file] = $fileCoverage;
636-
}
637-
}
638-
}
639-
640628
/**
641629
* Returns the lines of a source file that should be ignored.
642630
*
@@ -909,4 +897,45 @@ private function selectDriver()
909897
return new PHP_CodeCoverage_Driver_Xdebug;
910898
}
911899
}
900+
901+
/**
902+
* If we are processing uncovered files from whitelist,
903+
* we can initialize the data before we start to speed up the tests
904+
*/
905+
protected function initializeData()
906+
{
907+
$this->isInitialized = true;
908+
909+
if ($this->processUncoveredFilesFromWhitelist) {
910+
911+
$this->shouldCheckForDeadAndUnused = false;
912+
913+
$this->driver->start(true);
914+
915+
foreach ($this->filter->getWhitelist() as $file) {
916+
if ($this->filter->isFile($file)) {
917+
include_once($file);
918+
}
919+
}
920+
921+
$data = [];
922+
$coverage = $this->driver->stop();
923+
924+
foreach ($coverage as $file => $fileCoverage) {
925+
if ($this->filter->isFiltered($file)) {
926+
continue;
927+
}
928+
929+
foreach (array_keys($fileCoverage) as $key) {
930+
if ($fileCoverage[$key] == PHP_CodeCoverage_Driver::LINE_EXECUTED) {
931+
$fileCoverage[$key] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED;
932+
}
933+
}
934+
935+
$data[$file] = $fileCoverage;
936+
}
937+
938+
$this->append($coverage, 'UNCOVERED_FILES_FROM_WHITELIST');
939+
}
940+
}
912941
}

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)