class Profiler in XHProf 8
Provides profiler service.
Hierarchy
- class \Drupal\xhprof\Profiler implements ProfilerInterface
Expanded class hierarchy of Profiler
1 string reference to 'Profiler'
1 service uses Profiler
File
- src/
Profiler.php, line 18
Namespace
Drupal\xhprofView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Profiler:: |
private | property | ||
Profiler:: |
private | property | ||
Profiler:: |
private | property | ||
Profiler:: |
private | property | ||
Profiler:: |
private | property | ||
Profiler:: |
private | property | ||
Profiler:: |
public | function |
Returns whether a profiling can be enabled for the current request. Overrides ProfilerInterface:: |
|
Profiler:: |
public | function |
Creates a new unique run id. Overrides ProfilerInterface:: |
|
Profiler:: |
public | function |
Conditionally enable XHProf profiling. Overrides ProfilerInterface:: |
|
Profiler:: |
public | function |
Returns a list of available PHP extensions for profiling. Overrides ProfilerInterface:: |
|
Profiler:: |
private | function | Returns the namespace for this site. | |
Profiler:: |
public | function |
Loads a specific run. Overrides ProfilerInterface:: |
|
Profiler:: |
public | function |
Returns the run id associated with the current request. Overrides ProfilerInterface:: |
|
Profiler:: |
public | function |
Returns the current selected storage. Overrides ProfilerInterface:: |
|
Profiler:: |
public | function |
Check whether profiler is enabled. Overrides ProfilerInterface:: |
|
Profiler:: |
public | function |
Is any profiler extension loaded. Overrides ProfilerInterface:: |
|
Profiler:: |
public | function |
Generates a link to the report page for a specific run ID. Overrides ProfilerInterface:: |
|
Profiler:: |
public | function |
Shutdown and disable XHProf profiling. Overrides ProfilerInterface:: |
|
Profiler:: |
public | function |