Skip to content

Commit bf25943

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 0506681 commit bf25943

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
@@ -136,6 +136,7 @@ class Config
136136
'stdin' => null,
137137
'stdinContent' => null,
138138
'stdinPath' => null,
139+
'trackTime' => null,
139140
'unknown' => null,
140141
];
141142

@@ -250,6 +251,13 @@ public function __set($name, $value)
250251

251252
$value = $cleaned;
252253
break;
254+
255+
// Only track time when explicitly needed.
256+
case 'verbosity':
257+
if ($value > 2) {
258+
$this->settings['trackTime'] = true;
259+
}
260+
break;
253261
default :
254262
// No validation required.
255263
break;
@@ -503,6 +511,7 @@ public function restoreDefaults()
503511
$this->stdin = false;
504512
$this->stdinContent = null;
505513
$this->stdinPath = null;
514+
$this->trackTime = false;
506515
$this->unknown = [];
507516

508517
$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

@@ -491,8 +492,11 @@ public function process()
491492

492493
$this->activeListener = $class;
493494

494-
if (PHP_CODESNIFFER_VERBOSITY > 2) {
495+
if ($this->configCache['trackTime'] === true) {
495496
$startTime = microtime(true);
497+
}
498+
499+
if (PHP_CODESNIFFER_VERBOSITY > 2) {
496500
echo "\t\t\tProcessing ".$this->activeListener.'... ';
497501
}
498502

@@ -501,14 +505,16 @@ public function process()
501505
$listenerIgnoreTo[$this->activeListener] = $ignoreTo;
502506
}
503507

504-
if (PHP_CODESNIFFER_VERBOSITY > 2) {
508+
if ($this->configCache['trackTime'] === true) {
505509
$timeTaken = (microtime(true) - $startTime);
506510
if (isset($this->listenerTimes[$this->activeListener]) === false) {
507511
$this->listenerTimes[$this->activeListener] = 0;
508512
}
509513

510514
$this->listenerTimes[$this->activeListener] += $timeTaken;
515+
}
511516

517+
if (PHP_CODESNIFFER_VERBOSITY > 2) {
512518
$timeTaken = round(($timeTaken), 4);
513519
echo "DONE in $timeTaken seconds".PHP_EOL;
514520
}
@@ -535,8 +541,7 @@ public function process()
535541
echo "\t*** END TOKEN PROCESSING ***".PHP_EOL;
536542
echo "\t*** START SNIFF PROCESSING REPORT ***".PHP_EOL;
537543

538-
asort($this->listenerTimes, SORT_NUMERIC);
539-
$this->listenerTimes = array_reverse($this->listenerTimes, true);
544+
arsort($this->listenerTimes, SORT_NUMERIC);
540545
foreach ($this->listenerTimes as $listener => $timeTaken) {
541546
echo "\t$listener: ".round(($timeTaken), 4).' secs'.PHP_EOL;
542547
}

0 commit comments

Comments
 (0)