RuntimeMeasurement.php in Purge 8.3
File
src/Plugin/Purge/Purger/RuntimeMeasurement.php
View source
<?php
namespace Drupal\purge\Plugin\Purge\Purger;
use Drupal\purge\Counter\Counter;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface;
class RuntimeMeasurement extends Counter implements RuntimeMeasurementInterface {
protected $start = NULL;
public function getSafeTimeHintValue($value) {
if ($value < 0.1) {
return 0.1;
}
elseif ($value > 10.0) {
return 10.0;
}
return $value;
}
public function start() {
if (!is_null($this->start)) {
throw new \LogicException("Already started, call ->stop() first!");
}
$this->start = microtime(TRUE);
}
public function stop(array $invalidations) {
if (empty($invalidations)) {
throw new \LogicException('The $invalidations parameter is empty.');
}
if (is_null($this->start)) {
throw new \LogicException("Not yet started, call ->start first!");
}
foreach ($invalidations as $invalidation) {
if (!$invalidation instanceof InvalidationInterface) {
throw new \LogicException('One of the $invalidations is not a InvalidationInterface derivative!');
}
if ($invalidation
->getState() !== InvalidationInterface::SUCCEEDED) {
$this->start = NULL;
return;
}
}
if (($spent = microtime(TRUE) - $this->start) === 0.0) {
$this->start = NULL;
return;
}
$spent = $this
->getSafeTimeHintValue($spent / count($invalidations) * 1.15);
if ($this->value === 0.0 || $spent > $this->value) {
$this
->set($spent);
}
elseif ($spent < $this->value) {
$slow_adjustment = $this
->getSafeTimeHintValue($this->value * 0.9);
if ($slow_adjustment >= $spent) {
$this
->set($slow_adjustment);
}
}
$this->start = NULL;
}
}