CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

feat: additional `opcache` setting in check php.ini

Open ddevsr opened this issue 1 year ago • 9 comments

Description I think this additional needed to recommendation.

Checklist:

  • [x] Securely signed commits
  • [ ] Component(s) with PHPDoc blocks, only if necessary or adds value
  • [x] Unit testing, with >80% coverage
  • [ ] User guide updated
  • [x] Conforms to style guide

ddevsr avatar Jul 08 '24 07:07 ddevsr

I have concerns.

  1. Too many items in the output can be difficult to read. I want to display only the really important items.
  2. The current output is too wide to see.
  3. It is difficult to determine recommended values for OPCache settings because they are highly dependent on the environment.
+---------------------------------+---------+---------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
| Directive                       | Global  | Current | Recommended | Remark                                                                                                                                   |
+---------------------------------+---------+---------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
| error_reporting                 | 32767   | 32767   | 5111        |                                                                                                                                          |
| display_errors                  | 1       | 1       | 0           |                                                                                                                                          |
| display_startup_errors          | 1       | 1       | 0           |                                                                                                                                          |
| log_errors                      | 1       | 1       |             |                                                                                                                                          |
| error_log                       |         |         |             |                                                                                                                                          |
| default_charset                 | UTF-8   | UTF-8   | UTF-8       |                                                                                                                                          |
| memory_limit                    | 1G      | 1G      |             | > post_max_size                                                                                                                          |
| post_max_size                   | 8M      | 8M      |             | > upload_max_filesize                                                                                                                    |
| upload_max_filesize             | 2M      | 2M      |             | < post_max_size                                                                                                                          |
| request_order                   | GP      | GP      | GP          |                                                                                                                                          |
| variables_order                 | GPCS    | GPCS    | GPCS        |                                                                                                                                          |
| date.timezone                   | UTC     | UTC     | UTC         |                                                                                                                                          |
| mbstring.language               | neutral | neutral | neutral     |                                                                                                                                          |
| opcache.enable                  | 1       | 1       | 1           |                                                                                                                                          |
| opcache.enable_cli              | 0       | 0       |             |                                                                                                                                          |
| opcache.jit                     | tracing | tracing |             |                                                                                                                                          |
| opcache.jit_buffer_size         | 0       | 0       |             |                                                                                                                                          |
| opcache.memory_consumption      | 128     | 128     | 728         | Increasing the configured memory size (MB) will improve performance by caching those files (consideration based on free space of memory) |
| opcache.interned_strings_buffer | 8       | 8       | 64          |                                                                                                                                          |
| opcache.max_accelerated_files   | 10000   | 10000   | 56000       | Increasing the configured accelerated will many cache files into OPCache hash table                                                      |
| opcache.max_wasted_percentage   | 5       | 5       | 15          |                                                                                                                                          |
| opcache.validate_timestamps     | 1       | 1       | 0           |                                                                                                                                          |
| opcache.revalidate_freq         | 2       | 2       | 0           |                                                                                                                                          |
| opcache.save_comments           | 1       | 1       | 1           |                                                                                                                                          |
+---------------------------------+---------+---------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+

kenjis avatar Jul 09 '24 00:07 kenjis

Probably a simple mention of opcache On/Off is enough. Many people don't use the console when developing. Especially the newbies. I think special attention to php.ini is not required. More questions about apache/nginx

neznaika0 avatar Aug 11 '24 12:08 neznaika0

This command is mainly used to check the PHP settings of production environments.

The Recommended column shows the recommended values for production environment. They may differ in development environments. https://codeigniter4.github.io/CodeIgniter4/installation/running.html#checking-php-ini-settings

kenjis avatar Aug 11 '24 23:08 kenjis

@ddevsr If you want to show opcache detailed settings, how about adding an argument? E.g. php spark phpini:check opcache shows only opcache settings.

kenjis avatar Aug 12 '24 02:08 kenjis

:wave: Hi, @ddevsr!

We detected conflicts in your PR against the base branch :speak_no_evil:
You may want to sync :arrows_counterclockwise: your branch with upstream!

Ref: Syncing Your Branch

github-actions[bot] avatar Aug 22 '24 00:08 github-actions[bot]

@kenjis @michalsn

$ php spark phpini:check

CodeIgniter v4.5.4 Command Line Tool - Server Time: 2024-08-22 09:50:55 UTC+00:00

+-------------------------+---------------------------------------+---------------------------------------+-------------+---------------------------------------+      
| Directive               | Global                                | Current                               | Recommended | Remark                                |      
+-------------------------+---------------------------------------+---------------------------------------+-------------+---------------------------------------+      
| error_reporting         | 32767                                 | 32767                                 | 5111        |                                       |      
| display_errors          | 1                                     | 1                                     | 0           |                                       |      
| display_startup_errors  | 1                                     | 1                                     | 0           |                                       |      
| log_errors              | 1                                     | 1                                     |             |                                       |      
| error_log               | D:/Project/laragon/tmp/php_errors.log | D:/Project/laragon/tmp/php_errors.log |             |                                       |      
| default_charset         | UTF-8                                 | UTF-8                                 | UTF-8       |                                       |      
| max_execution_time      | 0                                     | 0                                     |             | The default is 30.                    |      
| memory_limit            | 1024M                                 | 1024M                                 |             | > post_max_size                       |      
| post_max_size           | 2G                                    | 2G                                    |             | > upload_max_filesize                 |      
| upload_max_filesize     | 2G                                    | 2G                                    |             | < post_max_size                       |      
| max_input_vars          | 1000                                  | 1000                                  |             | The default is 1000.                  |      
| request_order           | GP                                    | GP                                    | GP          |                                       |      
| variables_order         | GPCS                                  | GPCS                                  | GPCS        |                                       |      
| date.timezone           | Asia/Jakarta                          | Asia/Jakarta                          | UTC         |                                       |      
| mbstring.language       | neutral                               | neutral                               | neutral     |                                       |      
| opcache.enable          | 0                                     | 0                                     | 1           |                                       |      
| opcache.enable_cli      | 1                                     | 1                                     | 1           |                                       |      
| opcache.jit             | tracing                               | tracing                               | tracing     |                                       |      
| opcache.jit_buffer_size | 128M                                  | 128M                                  | 256         | Adjust with your free space of memory |      
| zend.assertions         | 1                                     | 1                                     | -1          |                                       |      
+-------------------------+---------------------------------------+---------------------------------------+-------------+---------------------------------------+
$ php spark phpini:check opcache

CodeIgniter v4.5.4 Command Line Tool - Server Time: 2024-08-22 09:52:13 UTC+00:00

+---------------------------------+---------+---------+-------------+----------------------------------------------------------------------------------+
| Directive                       | Global  | Current | Recommended | Remark                                                                           |
+---------------------------------+---------+---------+-------------+----------------------------------------------------------------------------------+
| opcache.enable                  | 0       | 0       | 1           |                                                                                  |
| opcache.enable_cli              | 1       | 1       | 1           |                                                                                  |
| opcache.jit                     | tracing | tracing | tracing     |                                                                                  |
| opcache.jit_buffer_size         | 128M    | 128M    | 256         | Adjust with your free space of memory                                            |
| opcache.memory_consumption      | 728     | 728     | 512         | Adjust with your free space of memory                                            |
| opcache.interned_strings_buffer | 64      | 64      | 64          |                                                                                  |
| opcache.max_accelerated_files   | 58000   | 58000   | 40000       | Find many files in your project (example: find your_project/ -iname *.php|wc -l) |
| opcache.max_wasted_percentage   | 15      | 15      | 15          |                                                                                  |
| opcache.validate_timestamps     | 0       | 0       | 0           |                                                                                  |
| opcache.revalidate_freq         | 0       | 0       | 0           |                                                                                  |
| opcache.save_comments           | 0       | 0       | 0           |                                                                                  |
+---------------------------------+---------+---------+-------------+----------------------------------------------------------------------------------+

ddevsr avatar Aug 22 '24 09:08 ddevsr

This looks good, however, I wonder if we should keep all 4 opcache keys in the default table. IMO only the first 2 will be relevant now.

michalsn avatar Aug 22 '24 10:08 michalsn

Recommendations by ChatGPT CodeIgniter 4 Assistant


When deploying CodeIgniter 4 (CI4) in a production environment, configuring PHP’s OPcache properly is essential to ensure optimal performance. Below are the recommended values for the specified OPcache settings that should be good for most CI4 users. However, certain values might need adjustment based on the specific needs of your application and server resources.

Recommended PHP ini settings for OPcache in a Production Environment

  • opcache.enable: 1 (or On)

    • Recommendation: Always enable OPcache in production to improve PHP performance by caching compiled script bytecode.
  • opcache.enable_cli: 0 (or Off)

    • Recommendation: Generally, you don't need OPcache for CLI scripts. However, if your application runs CLI scripts frequently, you might consider enabling it (1 or On).
  • opcache.jit: off

    • Recommendation: Just-In-Time (JIT) compilation can provide performance improvements for certain workloads, but it's generally safe to keep it off unless you've profiled your application and observed tangible benefits from JIT.
  • opcache.jit_buffer_size: 0

    • Recommendation: Set to 0 to disable JIT unless you've determined that enabling JIT is beneficial. If enabling JIT, you might set this value between 32M to 128M depending on available memory and workload characteristics.
  • opcache.memory_consumption: 128M to 256M

    • Recommendation: This defines how much memory OPcache can use. For most applications, 128M should be sufficient, but for larger applications with many scripts, 256M might be better.
  • opcache.interned_strings_buffer: 8M to 16M

    • Recommendation: This is used for storing interned strings. 8M is a good starting point, and 16M can be used for larger applications.
  • opcache.max_accelerated_files: 10000 to 20000

    • Recommendation: This defines the maximum number of files that OPcache can cache. A setting of 10000 should work for most applications, but for large codebases, 20000 might be necessary.
  • opcache.max_wasted_percentage: 5%

    • Recommendation: When the percentage of wasted memory (due to cache fragmentation) exceeds this value, OPcache will restart. A 5% threshold is generally recommended.
  • opcache.validate_timestamps: 0 (or Off)

    • Recommendation: Disable in production. This improves performance by not checking the script modification time on every request. If your deployment process doesn’t guarantee that updated files are reloaded (e.g., through a cache invalidation strategy), you might need to set this to 1 and use opcache.revalidate_freq.
  • opcache.revalidate_freq: 0 to 60

    • Recommendation: If opcache.validate_timestamps is 1, this setting controls how often (in seconds) OPcache will check for script changes. Setting this to 0 means it checks on every request, which is not recommended for production. Instead, use a value like 60 to reduce the performance hit.
  • opcache.save_comments: 1 (or On)

    • Recommendation: Keep comments enabled as they are often required by various frameworks and tools. Disabling this can cause issues, especially with Doctrine annotations and other comment-based parsing tools.

Additional Considerations:

  • Memory Resources: Ensure your server has enough memory to accommodate these settings. Adjust values like opcache.memory_consumption and opcache.jit_buffer_size based on your server’s available RAM.
  • Application Size: Larger applications might require higher values for settings like opcache.max_accelerated_files and opcache.memory_consumption.
  • Testing: Always profile your application with these settings to ensure they are optimal for your specific environment. What works well for one CI4 application might need tweaks for another.

kenjis avatar Aug 23 '24 00:08 kenjis

Recommendations by Claude 3.5 Sonnet


As a PHP expert, I can provide recommended values for the PHP ini settings you've listed, specifically tailored for a CodeIgniter 4 production environment. These recommendations should be suitable for most CI4 users, but keep in mind that optimal values can vary depending on your specific application and server resources.

opcache.enable = 1
opcache.enable_cli = 0
opcache.jit = 1255
opcache.jit_buffer_size = 100M
opcache.memory_consumption = 256
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 10000
opcache.max_wasted_percentage = 10
opcache.validate_timestamps = 0
opcache.revalidate_freq = 0
opcache.save_comments = 1

Let me explain each setting:

  1. opcache.enable = 1: This enables OPcache for web requests, which is crucial for performance in production.

  2. opcache.enable_cli = 0: OPcache is typically not needed for CLI operations, so it's usually disabled.

  3. opcache.jit = 1255: This enables JIT compilation with a balanced setting. The value 1255 represents tracing JIT for all JIT-able functions.

  4. opcache.jit_buffer_size = 100M: This allocates 100MB for JIT-compiled code. Adjust based on available memory, but 100MB is a good starting point.

  5. opcache.memory_consumption = 256: Allocates 256MB for OPcache. Adjust based on your application size and available memory.

  6. opcache.interned_strings_buffer = 16: Allocates 16MB for interned strings. This is usually sufficient for most applications.

  7. opcache.max_accelerated_files = 10000: Allows caching of up to 10,000 files. Adjust based on the number of PHP files in your application.

  8. opcache.max_wasted_percentage = 10: Restarts the OPcache when 10% of memory is wasted. This helps manage memory efficiently.

  9. opcache.validate_timestamps = 0: Disables file timestamp checking. This improves performance but requires a server restart or OPcache reset when files are updated.

  10. opcache.revalidate_freq = 0: Related to validate_timestamps. When set to 0 and validate_timestamps is off, it never checks file timestamps.

  11. opcache.save_comments = 1: Preserves comments in the cached files. This is important for CodeIgniter 4, as it uses doc comments for annotations.

These values provide a good starting point for a CodeIgniter 4 production environment. However, you may need to adjust them based on your specific application requirements, server resources, and performance needs. Regular monitoring and tuning can help you find the optimal settings for your particular use case.

kenjis avatar Aug 23 '24 00:08 kenjis

I guess there is no consensus on recommended values?

michalsn avatar Dec 21 '24 18:12 michalsn