tobybatch / kimai2

Docker containers for the kimai2 web application including docker-compose and kubernetes/helm deployment.
MIT License
183 stars 97 forks source link

enable opcache #444

Closed kevinpapst closed 1 year ago

kevinpapst commented 1 year ago

can someone please test these changes. i don't know if that is even the correct file to change... 😁

while being on it, i changed the default session timeout from 14 minutes to 1 week.

the setting opcache.validate_timestamps might cause troubles for files changed during runtime (like updated invoice templates). we should mention in the docs, that a docker restart (actually fpm) is advised if file changes do not become visible in the container

do not merge without testing, these changes were made blindly!!!

LiaraAlis commented 1 year ago

I'm not sure what I need to do to test these changes, but I assume that I have to do docker build and then running docker-compose up. If this is so, this changes aren't working. I looked at the Dockerfile before and I think the changes @tobybatch mentioned in #438 is the way to go. I will try this later.

kevinpapst commented 1 year ago

but opcache is not an extension afaik, it is built in php.

no clue, not a docker user 🤷but what about you ssh into the container, edit the file manually and apply my changes, restart fpm inside the container and check if performance improves.

we first need to confirm whether the settings do any good - and then we can ask @tobybatch to put it into the image properly.

tobybatch commented 1 year ago

@kevinpapst I'll take a look.

tobybatch commented 1 year ago

Looks like the opcache settings are not set at all. I'll fix this. I'll do it in the next couple of days.

✘  ~/usr/kimai/kimai2 [ kevinpapst-patch-1 | ✔  ] $ docker run -ti --rm --name=kimai --entrypoint bash docker.io/kimai/kimai2:fpm-1.30.1-prod
bash-5.1$ php -i | grep "opcache.enable\|opcache.memory_consumption\|opcache.interned_strings_buffer\|opcache.max_accelerated_files\|opcache.validate_timestamps\|session.gc_maxlifetime"
session.gc_maxlifetime => 604800 => 604800
LiaraAlis commented 1 year ago

@kevinpapst No, it's an extension. If you compile PHP manually you can compile it directly, but as far as I know in apt packages it is a separate package. I tried to change the Dockerfile and was able to confirm that this is working:

  1. Add the following code after this: https://github.com/tobybatch/kimai2/blob/993e98e628c7c7f8d76f091682655870908f1421/Dockerfile#L122

    # php extension opcache
    FROM ${BASE}-php-ext-base AS php-ext-opcache
    RUN docker-php-ext-install -j$(nproc) opcache
  2. Add the following code after this: https://github.com/tobybatch/kimai2/blob/993e98e628c7c7f8d76f091682655870908f1421/Dockerfile#L228

    COPY --from=php-ext-opcache /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini  /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini

image

As hoped, I could notice a strong increase in speed.

kevinpapst commented 1 year ago

Ok, double checked and you are right, opcache is an extension. It is even in the Kimai installation docs ... never trust your own memory ^^

Anyhow, great that we have everything together now. And great that this rather simple chance will make a huge difference.

LiaraAlis commented 1 year ago

Because I'm not able to change anything in this PR, I've created a new pr #446. I hope this is okay for you. 😊

kevinpapst commented 1 year ago

LOL, happy that my PR pushed you to try it yourself ;-) but I would like to understand the configuration of the opcode cache.

The default settings are suboptimal: too small for a symfony application with many files, caches and long uptimes.

Can you add a phpinfo to the kimai public folder and copy & paste the result of the opcode settings? Then this PR can be used to improve them after yours activated the opcode cache.

LiaraAlis commented 1 year ago
www-data@814fec82650f:/opt/kimai/public$ php -i | grep opcache                                                                                                                                              
/usr/local/etc/php/conf.d/docker-php-ext-opcache.ini,                                                                                                                                                       
opcache.blacklist_filename => no value => no value                                                                                                                                                          
opcache.consistency_checks => 0 => 0                                                                                                                                                                        
opcache.dups_fix => Off => Off                                                                                                                                                                              
opcache.enable => On => On                                                                                                                                                                                  
opcache.enable_cli => Off => Off                                                                                                                                                                            
opcache.enable_file_override => Off => Off                                                                                                                                                                  
opcache.error_log => no value => no value                                                                                                                                                                   
opcache.file_cache => no value => no value                                                                                                                                                                  
opcache.file_cache_consistency_checks => On => On                                                                                                                                                           
opcache.file_cache_only => Off => Off                                                                                                                                                                       
opcache.file_update_protection => 2 => 2                                                                                                                                                                    
opcache.force_restart_timeout => 180 => 180                                                                                                                                                                   
opcache.huge_code_pages => Off => Off                                                                                                                                                                       
opcache.interned_strings_buffer => 8 => 8                                                                                                                                                                   
opcache.jit => tracing => tracing                                                                                                                                                                           
opcache.jit_bisect_limit => 0 => 0                                                                                                                                                                          
opcache.jit_blacklist_root_trace => 16 => 16                                                                                                                                                                
opcache.jit_blacklist_side_trace => 8 => 8                                                                                                                                                                  
opcache.jit_buffer_size => 0 => 0                                                                                                                                                                           
opcache.jit_debug => 0 => 0                                                                                                                                                                                 
opcache.jit_hot_func => 127 => 127                                                                                                                                                                          
opcache.jit_hot_loop => 64 => 64                                                                                                                                                                            
opcache.jit_hot_return => 8 => 8                                                                                                                                                                            
opcache.jit_hot_side_exit => 8 => 8                                                                                                                                                                         
opcache.jit_max_exit_counters => 8192 => 8192                                                                                                                                                               
opcache.jit_max_loop_unrolls => 8 => 8                                                                                                                                                                      
opcache.jit_max_polymorphic_calls => 2 => 2                                                                                                                                                                 
opcache.jit_max_recursive_calls => 2 => 2                                                                                                                                                                   
opcache.jit_max_recursive_returns => 2 => 2                                                                                                                                                                 
opcache.jit_max_root_traces => 1024 => 1024                                                                                                                                                                 
opcache.jit_max_side_traces => 128 => 128                                                                                                                                                                   
opcache.jit_prof_threshold => 0.005 => 0.005                                                                                                                                                                
opcache.lockfile_path => /tmp => /tmp                                                                                                                                                                       
opcache.log_verbosity_level => 1 => 1                                                                                                                                                                       
opcache.max_accelerated_files => 10000 => 10000                                                                                                                                                             
opcache.max_file_size => 0 => 0                                                                                                                                                                             
opcache.max_wasted_percentage => 5 => 5                                                                                                                                                                     
opcache.memory_consumption => 128 => 128                                                                                                                                                                    
opcache.opt_debug_level => 0 => 0                                                                                                                                                                           
opcache.optimization_level => 0x7FFEBFFF => 0x7FFEBFFF                                                                                                                                                      
opcache.preferred_memory_model => no value => no value                                                                                                                                                      
opcache.preload => no value => no value                                                                                                                                                                     
opcache.preload_user => no value => no value                                                                                                                                                                
opcache.protect_memory => Off => Off                                                                                                                                                                        
opcache.record_warnings => Off => Off                                                                                                                                                                       
opcache.restrict_api => no value => no value                                                                                                                                                                
opcache.revalidate_freq => 2 => 2                                                                                                                                                                           
opcache.revalidate_path => Off => Off                                                                                                                                                                       
opcache.save_comments => On => On                                                                                                                                                                           
opcache.use_cwd => On => On                                                                                                                                                                                 
opcache.validate_permission => Off => Off                                                                                                                                                                   
opcache.validate_root => Off => Off                                                                                                                                                                         
opcache.validate_timestamps => On => On

I think this should be the same settings as in web.

kevinpapst commented 1 year ago

I don't understand that docker container.

  1. Enabling opcache on the CLI is not the best idea, I vote against that.
  2. Are you sure that web is using the same php.ini?
  3. Can you double check the config for web => creating a info.php file that you open via browser / through fpm.
LiaraAlis commented 1 year ago
  1. What makes you think that OPcache is enabled on the CLI? opcache.enable_cli is Off
  2. Not sure, but it looks like.
  3. Of course. See table below.

Zend OPcache

Opcode Caching | Up and Running -- | -- Optimization | Enabled SHM Cache | Enabled File Cache | Disabled JIT | Disabled Startup | OK Shared memory model | mmap Cache hits | 1919995 Cache misses | 2979 Used memory | 48407616 Free memory | 85810112 Wasted memory | 0 Interned Strings Used memory | 4662640 Interned Strings Free memory | 1628352 Cached scripts | 2977 Cached keys | 4087 Max keys | 16229 OOM restarts | 0 Hash keys restarts | 0 Manual restarts | 0 Directive | Local Value | Master Value -- | -- | -- opcache.blacklist_filename | no value | no value opcache.consistency_checks | 0 | 0 opcache.dups_fix | Off | Off opcache.enable | On | On opcache.enable_cli | Off | Off opcache.enable_file_override | Off | Off opcache.error_log | no value | no value opcache.file_cache | no value | no value opcache.file_cache_consistency_checks | On | On opcache.file_cache_only | Off | Off opcache.file_update_protection | 2 | 2 opcache.force_restart_timeout | 180 | 180 opcache.huge_code_pages | Off | Off opcache.interned_strings_buffer | 8 | 8 opcache.jit | tracing | tracing opcache.jit_bisect_limit | 0 | 0 opcache.jit_blacklist_root_trace | 16 | 16 opcache.jit_blacklist_side_trace | 8 | 8 opcache.jit_buffer_size | 0 | 0 opcache.jit_debug | 0 | 0 opcache.jit_hot_func | 127 | 127 opcache.jit_hot_loop | 64 | 64 opcache.jit_hot_return | 8 | 8 opcache.jit_hot_side_exit | 8 | 8 opcache.jit_max_exit_counters | 8192 | 8192 opcache.jit_max_loop_unrolls | 8 | 8 opcache.jit_max_polymorphic_calls | 2 | 2 opcache.jit_max_recursive_calls | 2 | 2 opcache.jit_max_recursive_returns | 2 | 2 opcache.jit_max_root_traces | 1024 | 1024 opcache.jit_max_side_traces | 128 | 128 opcache.jit_prof_threshold | 0.005 | 0.005 opcache.lockfile_path | /tmp | /tmp opcache.log_verbosity_level | 1 | 1 opcache.max_accelerated_files | 10000 | 10000 opcache.max_file_size | 0 | 0 opcache.max_wasted_percentage | 5 | 5 opcache.memory_consumption | 128 | 128 opcache.opt_debug_level | 0 | 0 opcache.optimization_level | 0x7FFEBFFF | 0x7FFEBFFF opcache.preferred_memory_model | no value | no value opcache.preload | no value | no value opcache.preload_user | no value | no value opcache.protect_memory | Off | Off opcache.record_warnings | Off | Off opcache.restrict_api | no value | no value opcache.revalidate_freq | 2 | 2 opcache.revalidate_path | Off | Off opcache.save_comments | On | On opcache.use_cwd | On | On opcache.validate_permission | Off | Off opcache.validate_root | Off | Off opcache.validate_timestamps | On | On
kevinpapst commented 1 year ago

Thank you very much 👍

I just saw opcache.enable => On. Missed the second setting. Off is better on cli, so we are good for that one.

LiaraAlis commented 1 year ago

@kevinpapst Should I add your changes in this PR to my Dockerfile and try your changes?

kevinpapst commented 1 year ago

You could test if they work, yes please. I guess you will not see much difference for a single user though.

But at least the validate_timestamps makes a huge IO difference, especially with many users.

LiaraAlis commented 1 year ago

I tried it out but it seems that your changes doesn't have any effect. It seems that the replace doesn't work. {{php.ini}} values stays on default. But I also noticed that this line also have no effect: https://github.com/tobybatch/kimai2/blob/993e98e628c7c7f8d76f091682655870908f1421/Dockerfile#L297

www-data@eeb903d9b49d:~/html$ cat /usr/local/etc/php/php.ini | grep expose_php                                                                                                                              
expose_php = On

I used build apache-1.30.2-prod. @tobybatch Maybe this is the wrong place for these changes?

tobybatch commented 1 year ago

There was core bug with my sed operations in the Dockerfile and startup.sh.

@kevinpapst I've lifted your code into a new PR #452 along with the bug fix.