Skip to content

Add hypernode:check-performance-config, add proper error codes to hypernode:performance #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions n98-magerun2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ autoloaders:
commands:
customCommands:
- Hypernode\Magento\Command\Hypernode\Performance\PerformanceCommand
- Hypernode\Magento\Command\Hypernode\Config\CheckPerformanceConfigCommand

passwordCracker:
rulesDirs:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Byte Hypernode Magerun
*
* @package hypernode-Magerun
* @author Byte
* @copyright Copyright (c) 2016 Byte
* @license http://opensource.org/licenses/osl-3.0.php Open Software License 3.0 (OSL-3.0)
*/
namespace Hypernode\Magento\Command\Hypernode\Config;

use Hypernode\Magento\Command\AbstractHypernodeCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;

/**
* Class GenerateMapsCommand
* @package Hypernode\Magento\Command\Hypernode\Config
*/
class CheckPerformanceConfigCommand extends AbstractHypernodeCommand
{
protected function configure()
{
$this
->setName('hypernode:check-performance-config')
->setDescription('Creates a little report if the magento configuration is optimal based on Redis, Varnish, and more');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->detectMagento($output);
if (!$this->initMagento()) {
return 1;
}

$output->writeln('<info>Checking Magento Performance Configuration...</info>');

// Fetch configuration values
$config = $this->getMagentoConfig();

$checks = [
'cache.frontend.page_cache' => [
'current' => $config['cache']['cache.frontend.page_cache'] ?? 'Unknown',
'optimal' => 'Magento\Framework\Cache\Backend\Redis',
],
'session.save' => [
'current' => $config['cache']['session.save'] ?? 'Unknown',
'optimal' => 'redis',
],
'cache.frontend.default.backend' => [
'current' => $config['cache']['cache.frontend.default.backend'] ?? 'Unknown',
'optimal' => 'Magento\Framework\Cache\Backend\Redis',
],
'http_cache_hosts' => [
'current' => $config['cache']['http_cache_hosts'] ?? 'Unknown',
'optimal' => 'varnish:6081',
],
'deploy:mode' => [
'current' => $config['deploy_mode'] ?? 'Unknown',
'optimal' => 'production',
],
];

// Create a table to display results
$table = new Table($output);
$table->setHeaders(['Configuration', 'Current Value', 'Optimal Value']);

foreach ($checks as $key => $check) {
$current = $check['current'];
$optimal = $check['optimal'];
$color = $current === $optimal ? 'green' : 'red';

$table->addRow([
$key,
"<fg=$color>$current</>",
$optimal,
]);
}

$table->render();

$output->writeln('<info>Performance configuration check completed.</info>');
return 0;
}

private function getMagentoConfig(): array
{
// Load the env.php file
$envFilePath = BP . '/app/etc/env.php';
if (!file_exists($envFilePath)) {
throw new \RuntimeException('env.php file not found.');
}

$envConfig = include $envFilePath;

// Fetch deployment mode
$deployMode = \Magento\Framework\App\State::MODE_DEFAULT;
if (isset($envConfig['MAGE_MODE'])) {
$deployMode = $envConfig['MAGE_MODE'];
}

// Fetch cache configurations
$cacheConfig = [
'cache.frontend.page_cache' => $envConfig['cache']['frontend']['page_cache']['backend'] ?? 'Unknown',
'session.save' => $envConfig['session']['save'] ?? 'Unknown',
'cache.frontend.default.backend' => $envConfig['cache']['frontend']['default']['backend'] ?? 'Unknown',
'http_cache_hosts' => isset($envConfig['http_cache_hosts'][0])
? ($envConfig['http_cache_hosts'][0]['host'] . ':' . $envConfig['http_cache_hosts'][0]['port'])
: 'Unknown',];

return [
'cache' => $cacheConfig,
'deploy_mode' => $deployMode,
];
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function configure()
*
* @return bool|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
// setting the options - hypernode demands it
$this->_options = $input->getOptions();
Expand Down Expand Up @@ -120,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
'<error>Use argument --sitemap to specify sitemap when using Magento 2.</error>'
);

return;
return 1;
}

if ($this->_options['silent']) {
Expand Down Expand Up @@ -170,6 +170,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}
}

return 0;
}

/**
Expand Down