diff -u b/config/install/monitoring.sensor_config.monitoring_perfomance_settings.yml b/config/install/monitoring.sensor_config.monitoring_perfomance_settings.yml --- b/config/install/monitoring.sensor_config.monitoring_perfomance_settings.yml +++ b/config/install/monitoring.sensor_config.monitoring_perfomance_settings.yml @@ -3,7 +3,7 @@ description: 'Monitors performance settings' category: 'System' plugin_id: monitoring_performance_settings -value_type: number +value_type: 'no_value' value_label: 'Performance' status: TRUE caching_time: 86400 diff -u b/src/Plugin/monitoring/SensorPlugin/PerformanceSettingsSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/PerformanceSettingsSensorPlugin.php --- b/src/Plugin/monitoring/SensorPlugin/PerformanceSettingsSensorPlugin.php +++ b/src/Plugin/monitoring/SensorPlugin/PerformanceSettingsSensorPlugin.php @@ -4,6 +4,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableMetadata; +use Drupal\monitoring\Result\SensorResult; use Drupal\monitoring\Result\SensorResultInterface; use Drupal\monitoring\SensorPlugin\ExtendedInfoSensorPluginInterface; use Drupal\monitoring\SensorPlugin\SensorPluginBase; @@ -25,7 +26,23 @@ * {@inheritdoc} */ public function runSensor(SensorResultInterface $sensor_result) { + $sensor_result->setStatus(SensorResultInterface::STATUS_OK); + $performance_settings = $this->performanceSettings(); + $value = 0; + foreach ($performance_settings as $performance_setting) { + if (!$performance_setting['status']) { + $sensor_result->addStatusMessage($this->t('@label : @value', ['@label' => $performance_setting['label'], '@value' => $performance_setting['value']])); + $value++; + } + } + + if ($value !== 0) { + $sensor_result->setStatus(SensorResult::STATUS_WARNING); + } + if ($sensor_result->getStatus() == SensorResultInterface::STATUS_OK) { + $sensor_result->addStatusMessage('Optimal configuration'); + } } /** @@ -82,2 +99,63 @@ + /** + * Helper function for performance settings. + * + * @return array + * The performance settings. + */ + protected function performanceSettings() { + $performance_settings = []; + $performance_config = \Drupal::config('system.performance'); + + // Check CSS aggregation. + $css_enabled = $performance_config->get('css.preprocess'); + $performance_settings[] = [ + 'label' => 'CSS Aggregation', + 'value' => $css_enabled ? 'Enabled' : 'Disabled', + 'status' => $css_enabled, + ]; + + // Check JS aggregation. + $js_enabled = $performance_config->get('js.preprocess'); + $performance_settings[] = [ + 'label' => 'JS Aggregation', + 'value' => $js_enabled ? 'Enabled' : 'Disabled', + 'status' => $js_enabled, + ]; + + // Check page caching. + $moduleHandler = \Drupal::service('module_handler'); + $page_caching_enabled = $moduleHandler->moduleExists('page_cache'); + $performance_settings[] = [ + 'label' => 'Page caching', + 'value' => $page_caching_enabled ? 'Enabled' : 'Disabled', + 'status' => $page_caching_enabled, + ]; + + // Check if there is a non zero max age. + $cacheable_metadata = new CacheableMetadata(); + $non_zero_max_age = $cacheable_metadata->getCacheMaxAge() === 0 ?: 1; + $performance_settings[] = [ + 'label' => 'Non-zero max age', + 'value' => $non_zero_max_age ? 'Yes' : 'No', + 'status' => $non_zero_max_age === FALSE, + ]; + + // Check if there are any NullBackend cache bins. + $bins = Cache::getBins(); + $null_cache_bins = FALSE; + foreach ($bins as $bin) { + if ($bin instanceof NullBackend) { + $null_cache_bins = TRUE; + } + } + $performance_settings[] =[ + 'label' => 'Null backend cache bins', + 'value' => $null_cache_bins ? 'Yes' : 'No', + 'status' => $null_cache_bins === FALSE, + ]; + + return $performance_settings; + } + } only in patch2: unchanged: --- /dev/null +++ b/tests/src/Kernel/MonitoringPerformanceSettingsPluginTest.php @@ -0,0 +1,45 @@ +getEditable('system.performance') + ->set('css.preprocess', FALSE) + ->set('js.preprocess', TRUE) + ->save(); + + // Enable page caching. + $this->enableModules(['page_cache']); + + // Create a NullBackend cache bin. + $cache_backend = new NullBackend('cache'); + + $sensor_manager = monitoring_sensor_manager(); + $sensor_config = $sensor_manager->getSensorConfigByName('performance_settings'); + $sensor_config->set('status', TRUE); + $sensor_config->save(); + + // Run sensor and assert message and verbose output. + $sensor_result = $this->runSensor('performance_settings'); + $this->assertTrue($sensor_result->isWarning()); + $this->assertEquals('CSS Aggregation : Disabled, Non-zero max age : Yes', $sensor_result->getMessage()); + } +}