You are here

class RuntimeMeasurement in Purge 8.3

Provides a execution time measurer for invalidation processing.

Hierarchy

Expanded class hierarchy of RuntimeMeasurement

File

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

Namespace

Drupal\purge\Plugin\Purge\Purger
View source
class RuntimeMeasurement extends Counter implements RuntimeMeasurementInterface {

  /**
   * The initial time measurement.
   *
   * @var null|float
   */
  protected $start = NULL;

  /**
   * {@inheritdoc}
   */
  public function getSafeTimeHintValue($value) {
    if ($value < 0.1) {
      return 0.1;
    }
    elseif ($value > 10.0) {
      return 10.0;
    }
    return $value;
  }

  /**
   * {@inheritdoc}
   */
  public function start() {
    if (!is_null($this->start)) {
      throw new \LogicException("Already started, call ->stop() first!");
    }
    $this->start = microtime(TRUE);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Counter::$callback protected property The callback that is called on writes when not NULL.
Counter::$permissionDecrement protected property Whether it is possible to call ::decrement() or not.
Counter::$permissionIncrement protected property Whether it is possible to call ::increment() or not.
Counter::$permissionSet protected property Whether it is possible to call ::set() or not.
Counter::$value protected property The value of the counter.
Counter::decrement public function Decrease the counter. Overrides CounterInterface::decrement
Counter::disableDecrement public function Disable the possibility to decrement the counter. Overrides CounterInterface::disableDecrement
Counter::disableIncrement public function Disable the possibility to increment the counter. Overrides CounterInterface::disableIncrement
Counter::disableSet public function Disable the possibility of setting counter. Overrides CounterInterface::disableSet
Counter::get public function Get the current value. Overrides CounterInterface::get
Counter::getInteger public function Get the current value as integer. Overrides CounterInterface::getInteger
Counter::increment public function Increase the counter. Overrides CounterInterface::increment
Counter::set public function Overwrite the counter value. Overrides CounterInterface::set
Counter::setDirectly protected function Overwrite the counter value (permission bypass).
Counter::setWriteCallback public function Set the callback that gets called when writes occur. Overrides CounterInterface::setWriteCallback
Counter::__construct public function Construct a counter object. Overrides CounterInterface::__construct 3
RuntimeMeasurement::$start protected property The initial time measurement.
RuntimeMeasurement::getSafeTimeHintValue public function Return a value safe for time hints, between 0.1 and 10.00. Overrides RuntimeMeasurementInterface::getSafeTimeHintValue
RuntimeMeasurement::start public function Start measuring execution time. Overrides RuntimeMeasurementInterface::start
RuntimeMeasurement::stop public function Stop measuring execution time and store if necessary. Overrides RuntimeMeasurementInterface::stop