Skip to content

Commit 3a5ca4d

Browse files
committed
Config: add trackTime toggle for whether or not to track listener times
As recording the time taken by each sniff has a performance impact in and of itself on a CS run, only record the time taken by each sniff when needed. [*] Originally, this was already done conditionally based on the `PHP_CODESNIFFER_VERBOSITY > 2` condition. However, adding the Performance report would add a second criteria. This commit adds a new (internal) Config setting `trackTime`, which will be set to `true` when `PHP_CODESNIFFER_VERBOSITY > 2`. This commit paves the way for adding the second criteria in the next commit. --- [*] I've done some unscientific benchmarks for this by running PHPCS multiple times, with and without tracking the listener times, over a 300+ file codebase. Without tracking listener times, the run time was always around 39 seconds with 56Mb memory use. With tracking listener times, the run time was always around 54 seconds with 64Mb memory use. This, to me, shows a significant enough difference and sufficient reason to put this toggle in place to only track time when needed.
1 parent 02338c2 commit 3a5ca4d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class Config
143143
'stdin' => null,
144144
'stdinContent' => null,
145145
'stdinPath' => null,
146+
'trackTime' => null,
146147
'unknown' => null,
147148
];
148149

@@ -277,6 +278,13 @@ public function __set($name, $value)
277278

278279
$value = $cleaned;
279280
break;
281+
282+
// Only track time when explicitly needed.
283+
case 'verbosity':
284+
if ($value > 2) {
285+
$this->settings['trackTime'] = true;
286+
}
287+
break;
280288
default :
281289
// No validation required.
282290
break;
@@ -530,6 +538,7 @@ public function restoreDefaults()
530538
$this->stdin = false;
531539
$this->stdinContent = null;
532540
$this->stdinPath = null;
541+
$this->trackTime = false;
533542
$this->unknown = [];
534543

535544
$standard = self::getConfigData('default_standard');

src/Files/File.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ public function __construct($path, Ruleset $ruleset, Config $config)
254254
$this->configCache['errorSeverity'] = $this->config->errorSeverity;
255255
$this->configCache['warningSeverity'] = $this->config->warningSeverity;
256256
$this->configCache['recordErrors'] = $this->config->recordErrors;
257+
$this->configCache['trackTime'] = $this->config->trackTime;
257258
$this->configCache['ignorePatterns'] = $this->ruleset->ignorePatterns;
258259
$this->configCache['includePatterns'] = $this->ruleset->includePatterns;
259260

@@ -506,8 +507,11 @@ public function process()
506507

507508
$this->activeListener = $class;
508509

509-
if (PHP_CODESNIFFER_VERBOSITY > 2) {
510+
if ($this->configCache['trackTime'] === true) {
510511
$startTime = microtime(true);
512+
}
513+
514+
if (PHP_CODESNIFFER_VERBOSITY > 2) {
511515
echo "\t\t\tProcessing ".$this->activeListener.'... ';
512516
}
513517

@@ -516,14 +520,16 @@ public function process()
516520
$listenerIgnoreTo[$this->activeListener] = $ignoreTo;
517521
}
518522

519-
if (PHP_CODESNIFFER_VERBOSITY > 2) {
523+
if ($this->configCache['trackTime'] === true) {
520524
$timeTaken = (microtime(true) - $startTime);
521525
if (isset($this->listenerTimes[$this->activeListener]) === false) {
522526
$this->listenerTimes[$this->activeListener] = 0;
523527
}
524528

525529
$this->listenerTimes[$this->activeListener] += $timeTaken;
530+
}
526531

532+
if (PHP_CODESNIFFER_VERBOSITY > 2) {
527533
$timeTaken = round(($timeTaken), 4);
528534
echo "DONE in $timeTaken seconds".PHP_EOL;
529535
}
@@ -557,8 +563,7 @@ public function process()
557563
echo "\t*** END TOKEN PROCESSING ***".PHP_EOL;
558564
echo "\t*** START SNIFF PROCESSING REPORT ***".PHP_EOL;
559565

560-
asort($this->listenerTimes, SORT_NUMERIC);
561-
$this->listenerTimes = array_reverse($this->listenerTimes, true);
566+
arsort($this->listenerTimes, SORT_NUMERIC);
562567
foreach ($this->listenerTimes as $listener => $timeTaken) {
563568
echo "\t$listener: ".round(($timeTaken), 4).' secs'.PHP_EOL;
564569
}

0 commit comments

Comments
 (0)