You are here

public function RuntimeMeasurement::stop in Purge 8.3

Stop measuring execution time and store if necessary.

To gather safe time hint measurements, the following rules apply:

  • All invalidations MUST have ::SUCCEEDED, if any of them failed the measurement will not be saved as it is likely unrepresentative data.
  • Measurements slower than previous records take priority. This means that a single slow (yet successful) performance will relentlessly adjust the measurement upwards, better safe...
  • Every faster measurement than previously stored records leads to 10% reduction of the last recorded measurement. This means structural low performance will be rewarded by more capacity, but slow and carefully!

Parameters

\Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface[] $invalidations: Non-associative array of processed invalidation objects.

Throws

\LogicException Thrown when the $invalidations parameter is empty.

\LogicException Thrown when any invalidation isn't a InvalidationInterface instance.

\LogicException Thrown when ::start() hasn't been called yet.

Overrides RuntimeMeasurementInterface::stop

File

src/Plugin/Purge/Purger/RuntimeMeasurement.php, line 46

Class

RuntimeMeasurement
Provides a execution time measurer for invalidation processing.

Namespace

Drupal\purge\Plugin\Purge\Purger

Code

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!");
  }

  // Check if any of the invalidations failed, if so, stop.
  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;
    }
  }

  // Calculate the spent execution time per invalidation by dividing it
  // through the number of invalidations processed. We're also adding 15% of
  // time for theoretic overhead and ensure that the final value remains
  // within the boundaries of ::getTimeHint().
  if (($spent = microtime(TRUE) - $this->start) === 0.0) {
    $this->start = NULL;
    return;
  }
  $spent = $this
    ->getSafeTimeHintValue($spent / count($invalidations) * 1.15);

  // Immediately write fresh or slower measurements.
  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);
    }
  }

  // Reset the start value so that new measurements can happen.
  $this->start = NULL;
}