Skip to content
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

feat: additional opcache setting in check php.ini #9032

Open
wants to merge 14 commits into
base: 4.6
Choose a base branch
from
14 changes: 13 additions & 1 deletion system/Commands/Utilities/PhpIniCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CodeIgniter\Commands\Utilities;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Security\CheckPhpIni;

/**
Expand Down Expand Up @@ -56,6 +57,7 @@ final class PhpIniCheck extends BaseCommand
* @var array<string, string>
*/
protected $arguments = [
'opcache' => 'Check detail opcache values in production environment.',
];

/**
Expand All @@ -70,7 +72,17 @@ final class PhpIniCheck extends BaseCommand
*/
public function run(array $params)
{
CheckPhpIni::run();
if (isset($params[0]) && ! in_array($params[0], array_keys($this->arguments), true)) {
CLI::error('You must write correct arguments.');
CLI::write(' Usage: ' . $this->usage);
CLI::write('Example: config:check opcache');
ddevsr marked this conversation as resolved.
Show resolved Hide resolved

return EXIT_ERROR;
}

$argument = $params[0] ?? null;

CheckPhpIni::run(argument: $argument);

return EXIT_SUCCESS;
}
Expand Down
29 changes: 23 additions & 6 deletions system/Security/CheckPhpIni.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class CheckPhpIni
*
* @return string|void HTML string or void in CLI
*/
public static function run(bool $isCli = true)
public static function run(bool $isCli = true, ?string $argument = null)
{
$output = static::checkIni();
$output = static::checkIni($argument);

$thead = ['Directive', 'Global', 'Current', 'Recommended', 'Remark'];
$tbody = [];
Expand Down Expand Up @@ -115,8 +115,9 @@ private static function outputForWeb(array $output, array $thead, array $tbody):
* @internal Used for testing purposes only.
* @testTag
*/
public static function checkIni(): array
public static function checkIni(?string $argument = null): array
{
// Default items
$items = [
'error_reporting' => ['recommended' => '5111'],
'display_errors' => ['recommended' => '0'],
Expand All @@ -134,12 +135,28 @@ public static function checkIni(): array
'date.timezone' => ['recommended' => 'UTC'],
'mbstring.language' => ['recommended' => 'neutral'],
'opcache.enable' => ['recommended' => '1'],
'opcache.enable_cli' => [],
'opcache.jit' => [],
'opcache.jit_buffer_size' => [],
'opcache.enable_cli' => ['recommended' => '1'],
'opcache.jit' => ['recommended' => 'tracing'],
'opcache.jit_buffer_size' => ['recommended' => '256', 'remark' => 'Adjust with your free space of memory'],
'zend.assertions' => ['recommended' => '-1'],
];

if ($argument === 'opcache') {
$items = [
'opcache.enable' => ['recommended' => '1'],
'opcache.enable_cli' => ['recommended' => '1'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why opcache.enable_cli is 1?

In my understanding, it does not improve performance unless you set opcache.file_cache correctly.
https://stackoverflow.com/questions/77778860/proper-opcache-configuration-for-cli

'opcache.jit' => ['recommended' => 'tracing'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to recommend to enable jit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not safe if using third-party ext like xdebug.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on other and my benchmark, JIT have benefit significant. Moreover, in PHP8.4 a new, better JIT has been implemented

Ref :

'opcache.jit_buffer_size' => ['recommended' => '256', 'remark' => 'Adjust with your free space of memory'],
ddevsr marked this conversation as resolved.
Show resolved Hide resolved
'opcache.memory_consumption' => ['recommended' => '512', 'remark' => 'Adjust with your free space of memory'],
ddevsr marked this conversation as resolved.
Show resolved Hide resolved
'opcache.interned_strings_buffer' => ['recommended' => '64'],
ddevsr marked this conversation as resolved.
Show resolved Hide resolved
'opcache.max_accelerated_files' => ['recommended' => '40000', 'remark' => 'Find many files in your project (example: find your_project/ -iname *.php|wc -l)'],
ddevsr marked this conversation as resolved.
Show resolved Hide resolved
ddevsr marked this conversation as resolved.
Show resolved Hide resolved
'opcache.max_wasted_percentage' => ['recommended' => '15'],
'opcache.validate_timestamps' => ['recommended' => '0'],
'opcache.revalidate_freq' => ['recommended' => '0'],
ddevsr marked this conversation as resolved.
Show resolved Hide resolved
'opcache.save_comments' => ['recommended' => '0'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting opcache.save_comments to 0 is dangerous. Some packages may use doc comments.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already test smooth for my project. For recomendation production perspective, i think can disabled in production with remark notes

"require": {
        "php": "^8.1",
        "chillerlan/php-qrcode": "^5.0",
        "codeigniter4/framework": "^4.5.3",
        "firebase/php-jwt": "^6.10",
        "mpdf/mpdf": "^8.2"
},

];
}

$output = [];
$ini = ini_get_all();

Expand Down
74 changes: 74 additions & 0 deletions tests/system/Commands/Utilities/PhpIniCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands\Utilities;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\Group;

/**
* @internal
*/
#[Group('Others')]
final class PhpIniCheckTest extends CIUnitTestCase
{
use StreamFilterTrait;

protected function setUp(): void
{
$this->resetServices();
parent::setUp();
}

protected function tearDown(): void
{
$this->resetServices();
parent::tearDown();
}

protected function getBuffer(): string
{
return $this->getStreamFilterBuffer();
}

public function testCommandCheckNoArg(): void
{
command('phpini:check');

$result = $this->getBuffer();

$this->assertStringContainsString('Directive', $result);
$this->assertStringContainsString('Global', $result);
$this->assertStringContainsString('Current', $result);
$this->assertStringContainsString('Recommended', $result);
$this->assertStringContainsString('Remark', $result);
}

public function testCommandCheckOpcache(): void
{
command('phpini:check opcache');

$this->assertStringContainsString('opcache.save_comments', $this->getBuffer());
}

public function testCommandCheckNoExistsArg(): void
{
command('phpini:check noexists');

$this->assertStringContainsString(
'You must write correct arguments.',
$this->getBuffer()
);
}
}
13 changes: 13 additions & 0 deletions tests/system/Security/CheckPhpIniTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public function testCheckIni(): void
$this->assertSame($expected, $output['display_errors']);
}

public function testCheckIniOpcache(): void
{
$output = CheckPhpIni::checkIni('opcache');

$expected = [
'global' => '1',
'current' => '1',
'recommended' => '0',
'remark' => '',
];
$this->assertSame($expected, $output['opcache.save_comments']);
}

public function testRunCli(): void
{
// Set MockInputOutput to CLI.
Expand Down
Loading