Skip to content

Commit

Permalink
Config: add trackTime toggle for whether or not to track listener t…
Browse files Browse the repository at this point in the history
…imes

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.
  • Loading branch information
jrfnl committed Nov 11, 2023
1 parent 8bb2eb1 commit 35570ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class Config
'stdin' => null,
'stdinContent' => null,
'stdinPath' => null,
'trackTime' => null,
'unknown' => null,
];

Expand Down Expand Up @@ -264,6 +265,13 @@ public function __set($name, $value)

$value = $cleaned;
break;

// Only track time when explicitly needed.
case 'verbosity':
if ($value > 2) {
$this->settings['trackTime'] = true;
}
break;
default :
// No validation required.
break;
Expand Down Expand Up @@ -517,6 +525,7 @@ public function restoreDefaults()
$this->stdin = false;
$this->stdinContent = null;
$this->stdinPath = null;
$this->trackTime = false;
$this->unknown = [];

$standard = self::getConfigData('default_standard');
Expand Down
13 changes: 9 additions & 4 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ public function __construct($path, Ruleset $ruleset, Config $config)
$this->configCache['errorSeverity'] = $this->config->errorSeverity;
$this->configCache['warningSeverity'] = $this->config->warningSeverity;
$this->configCache['recordErrors'] = $this->config->recordErrors;
$this->configCache['trackTime'] = $this->config->trackTime;
$this->configCache['ignorePatterns'] = $this->ruleset->ignorePatterns;
$this->configCache['includePatterns'] = $this->ruleset->includePatterns;

Expand Down Expand Up @@ -497,8 +498,11 @@ public function process()

$this->activeListener = $class;

if (PHP_CODESNIFFER_VERBOSITY > 2) {
if ($this->configCache['trackTime'] === true) {
$startTime = microtime(true);
}

if (PHP_CODESNIFFER_VERBOSITY > 2) {
echo "\t\t\tProcessing ".$this->activeListener.'... ';
}

Expand All @@ -507,14 +511,16 @@ public function process()
$listenerIgnoreTo[$this->activeListener] = $ignoreTo;
}

if (PHP_CODESNIFFER_VERBOSITY > 2) {
if ($this->configCache['trackTime'] === true) {
$timeTaken = (microtime(true) - $startTime);
if (isset($this->listenerTimes[$this->activeListener]) === false) {
$this->listenerTimes[$this->activeListener] = 0;
}

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

if (PHP_CODESNIFFER_VERBOSITY > 2) {
$timeTaken = round(($timeTaken), 4);
echo "DONE in $timeTaken seconds".PHP_EOL;
}
Expand All @@ -541,8 +547,7 @@ public function process()
echo "\t*** END TOKEN PROCESSING ***".PHP_EOL;
echo "\t*** START SNIFF PROCESSING REPORT ***".PHP_EOL;

asort($this->listenerTimes, SORT_NUMERIC);
$this->listenerTimes = array_reverse($this->listenerTimes, true);
arsort($this->listenerTimes, SORT_NUMERIC);
foreach ($this->listenerTimes as $listener => $timeTaken) {
echo "\t$listener: ".round(($timeTaken), 4).' secs'.PHP_EOL;
}
Expand Down

0 comments on commit 35570ad

Please sign in to comment.