You are here

class Profiler in XHProf 8

Provides profiler service.

Hierarchy

Expanded class hierarchy of Profiler

1 string reference to 'Profiler'
xhprof.services.yml in ./xhprof.services.yml
xhprof.services.yml
1 service uses Profiler
xhprof.profiler in ./xhprof.services.yml
Drupal\xhprof\Profiler

File

src/Profiler.php, line 18

Namespace

Drupal\xhprof
View source
class Profiler implements ProfilerInterface {

  /**
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  private $configFactory;

  /**
   * @var \Drupal\xhprof\XHProfLib\Storage\StorageInterface
   */
  private $storage;

  /**
   * @var \Symfony\Component\HttpFoundation\RequestMatcherInterface
   */
  private $requestMatcher;

  /**
   * @var string
   */
  private $runId;

  /**
   * @var bool
   */
  private $enabled = FALSE;

  /**
   * @var \Drupal\xhprof\Extension\ExtensionInterface
   */
  private $activeExtension;

  /**
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   * @param \Drupal\xhprof\XHProfLib\Storage\StorageInterface $storage
   * @param \Symfony\Component\HttpFoundation\RequestMatcherInterface $requestMatcher
   */
  public function __construct(ConfigFactoryInterface $configFactory, StorageInterface $storage, RequestMatcherInterface $requestMatcher) {
    $this->configFactory = $configFactory;
    $this->storage = $storage;
    $this->requestMatcher = $requestMatcher;
    $extension = $this->configFactory
      ->get('xhprof.config')
      ->get('extension');
    if ($extension == 'xhprof') {
      $this->activeExtension = new XHProfExtension();
    }
    elseif ($extension == 'uprofiler') {
      $this->activeExtension = new UprofilerExtension();
    }
    elseif ($extension == 'tideways') {
      $this->activeExtension = new TidewaysExtension();
    }
    elseif ($extension == 'tideways_xhprof') {
      $this->activeExtension = new TidewaysXHProfExtension();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function enable() {
    $config = $this->configFactory
      ->get('xhprof.config');
    $flags = $config
      ->get('flags');
    $excludeIndirectFunctions = $config
      ->get('exclude_indirect_functions');
    $modifier = 0;
    $extensionOptions = $this->activeExtension
      ->getOptions();
    foreach ($flags as $key => $value) {
      if ($value !== '0') {
        $extensionFlag = $extensionOptions[$key];
        $modifier += @constant($extensionFlag);
      }
    }
    $options = [];
    if ($excludeIndirectFunctions) {
      $options = [
        'ignored_functions' => [
          'call_user_func',
          'call_user_func_array',
        ],
      ];
    }
    $this->activeExtension
      ->enable($modifier, $options);
    $this->enabled = TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function shutdown($runId) {
    $xhprof_data = $this->activeExtension
      ->disable();
    $this->enabled = FALSE;
    return $this->storage
      ->saveRun($xhprof_data, $this
      ->getNamespace(), $runId);
  }

  /**
   * {@inheritdoc}
   */
  public function isEnabled() {
    return $this->enabled;
  }

  /**
   * {@inheritdoc}
   */
  public function canEnable(Request $request) {
    $config = $this->configFactory
      ->get('xhprof.config');
    if ($this
      ->isLoaded() && $config
      ->get('enabled') && $this->requestMatcher
      ->matches($request)) {
      $interval = $config
        ->get('interval');
      if ($interval && mt_rand(1, $interval) % $interval != 0) {
        return FALSE;
      }
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function isLoaded() {
    return count($this
      ->getExtensions()) >= 1;
  }

  /**
   * {@inheritdoc}
   */
  public function getExtensions() {
    $extensions = [];
    if (XHProfExtension::isLoaded()) {
      $extensions['xhprof'] = 'XHProf';
    }
    if (UprofilerExtension::isLoaded()) {
      $extensions['uprofiler'] = 'UProfiler';
    }
    if (TidewaysExtension::isLoaded()) {
      $extensions['tideways'] = 'Tideways';
    }
    if (TidewaysXHProfExtension::isLoaded()) {
      $extensions['tideways_xhprof'] = 'Tideways xhprof';
    }
    return $extensions;
  }

  /**
   * {@inheritdoc}
   */
  public function link($run_id) {
    $link = Link::createFromRoute(t('XHProf output'), 'xhprof.run', [
      'run' => $run_id,
    ], [
      'absolute' => TRUE,
    ]);
    return $link
      ->toString();
  }

  /**
   * {@inheritdoc}
   */
  public function getStorage() {
    return $this->storage;
  }

  /**
   * {@inheritdoc}
   */
  public function getRunId() {
    return $this->runId;
  }

  /**
   * {@inheritdoc}
   */
  public function createRunId() {
    if (!$this->runId) {
      $this->runId = uniqid();
    }
    return $this->runId;
  }

  /**
   * {@inheritdoc}
   */
  public function getRun($run_id) {
    return $this
      ->getStorage()
      ->getRun($run_id, $this
      ->getNamespace());
  }

  /**
   * Returns the namespace for this site.
   *
   * Currently is set to the site name value.
   *
   * @return string
   *   The string generated from site name.
   */
  private function getNamespace() {
    $result = $this->configFactory
      ->get('system.site')
      ->get('name');
    return str_replace([
      '.',
      '/',
      '\\',
    ], '-', $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Profiler::$activeExtension private property
Profiler::$configFactory private property
Profiler::$enabled private property
Profiler::$requestMatcher private property
Profiler::$runId private property
Profiler::$storage private property
Profiler::canEnable public function Returns whether a profiling can be enabled for the current request. Overrides ProfilerInterface::canEnable
Profiler::createRunId public function Creates a new unique run id. Overrides ProfilerInterface::createRunId
Profiler::enable public function Conditionally enable XHProf profiling. Overrides ProfilerInterface::enable
Profiler::getExtensions public function Returns a list of available PHP extensions for profiling. Overrides ProfilerInterface::getExtensions
Profiler::getNamespace private function Returns the namespace for this site.
Profiler::getRun public function Loads a specific run. Overrides ProfilerInterface::getRun
Profiler::getRunId public function Returns the run id associated with the current request. Overrides ProfilerInterface::getRunId
Profiler::getStorage public function Returns the current selected storage. Overrides ProfilerInterface::getStorage
Profiler::isEnabled public function Check whether profiler is enabled. Overrides ProfilerInterface::isEnabled
Profiler::isLoaded public function Is any profiler extension loaded. Overrides ProfilerInterface::isLoaded
Profiler::link public function Generates a link to the report page for a specific run ID. Overrides ProfilerInterface::link
Profiler::shutdown public function Shutdown and disable XHProf profiling. Overrides ProfilerInterface::shutdown
Profiler::__construct public function