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

Feature Request: re-enable php opcache for dev-environments? #1442

Closed
3 tasks done
schuhwerk opened this issue Oct 20, 2022 · 4 comments · Fixed by #1447
Closed
3 tasks done

Feature Request: re-enable php opcache for dev-environments? #1442

schuhwerk opened this issue Oct 20, 2022 · 4 comments · Fixed by #1447

Comments

@schuhwerk
Copy link
Contributor

Terms

Summary

Re-enabling opcache for dev decreases load time (in my case) by ~30%.

Motivation

I'm developing on Windows and my VM is slow (0.3s on production vs. 2.5s on dev for the same page).
I tried re-enabling opcache and obviously ran into the problem discussed here.

So I added/changed the following settings (group_vars/development/php.yml):

php_opcache_enable: 1
php_opcache_validate_timestamps: 1
php_opcache_revalidate_freq: 1

This brings down loading times for the same page (mentioned above) to 1.7s.

I don't have any problems with file-changes. In my case php_opcache_revalidate_freq can even stay at 60. This would even bring the dev-configuration closer to the one on the production environment.

I'm not sure, how it is for other environments, so we might be able to collect different experiences for other OSes/Configurations here.

Additional Context

I used several runs of Performance Insights (within Chrome's dev-tools) for the benchmarks.

@swalkinshaw
Copy link
Member

🤔 interesting idea! Basically the theory is that having opcaching enabled is worthwhile even when re-validating every 1 second.

I'm also curious about this setting: opcache.enable_file_override bool

When enabled, the opcode cache will be checked for whether a file has already been cached when file_exists(), is_file() and is_readable() are called. This may increase performance in applications that check the existence and readability of PHP scripts, but risks returning stale data if opcache.validate_timestamps is disabled.

Because we'd always have validate_timestamps enabled, it seems safe. Want to test that out as well to see if it makes a difference?

@schuhwerk
Copy link
Contributor Author

I wrote a small script ...

#!/bin/bash
# benchmark.sh
URL="$1" # Pass as firs parameter or just add an url here...
COUNT=10
TOTAL=0
for i in $(seq 1 $COUNT); do
	T=$( curl -o /dev/null -s -w '%{time_total}' "$URL" )
	echo -e "$i/$COUNT:\t$T"
	TOTAL=$( awk "BEGIN{ print $TOTAL + $T }" )
done
echo -e "---\nTotal:\t$( awk "BEGIN{ print $TOTAL/$COUNT }" )"

... and did some benchmarks in the following manner:

sudo vim /etc/php/8.0/fpm/php.ini       # change the value
sudo service php8.0-fpm restart         # then restart php.ini
bash benchmark.sh "https://example.com" # then run the script

tldr.:

  • The first value is always bigger, because the cache has to be initialized.
  • enable_file_override doesn't significantly change things for me.

I use this ini-setting (below) as the default setting, and only show changed values (in the following benchmarks).

opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 512
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 1
opcache.fast_shutdown = 1
opcache.validate_timestamps = 1
opcache.enable_file_override = 1
1/10:   2.192602
2/10:   1.417862
3/10:   1.329829
4/10:   1.352817
5/10:   1.350337
6/10:   1.214131
7/10:   1.529780
8/10:   1.332163
9/10:   1.487570
10/10:  1.309430
---
Total:  1.45166

Another run with the same settings:

1/10:   2.212923
2/10:   1.270493
3/10:   1.220274
4/10:   1.247268
5/10:   1.277368
6/10:   1.330988
7/10:   1.247622
8/10:   1.161211
9/10:   1.512393
10/10:  1.280042
---
Total:  1.37605

opcache.validate_timestamps = 0
1/10:   2.037866
2/10:   1.356562
3/10:   1.318341
4/10:   1.239954
5/10:   1.335589
6/10:   1.267463
7/10:   1.441167
8/10:   1.315683
9/10:   1.320554
10/10:  1.295416
---
Total:  1.39286

opcache.enable = 0
1/10:   1.990889
2/10:   2.220091
3/10:   1.991241
4/10:   2.000759
5/10:   2.037611
6/10:   1.922285
7/10:   2.066977
8/10:   1.986670
9/10:   2.048466
10/10:  2.009548
---
Total:  2.02746

opcache.enable_file_override = 0
1/10:   2.365654
2/10:   1.445723
3/10:   1.356475
4/10:   1.493420
5/10:   1.291487
6/10:   1.497339
7/10:   1.313832
8/10:   1.460188
9/10:   1.255856
10/10:  1.324482
---
Total:  1.48045

opcache.revalidate_freq = 60
1/10:   2.197011
2/10:   1.305936
3/10:   1.235971
4/10:   1.205744
5/10:   1.371293
6/10:   1.160303
7/10:   1.276416
8/10:   1.170768
9/10:   1.216995
10/10:  1.226870
---
Total:  1.33673

opcache.revalidate_freq = 60
opcache.validate_timestamps = 0
1/10:   2.101342
2/10:   1.249363
3/10:   1.258679
4/10:   1.228603
5/10:   1.195424
6/10:   1.224175
7/10:   1.217903
8/10:   1.270338
9/10:   1.298442
10/10:  1.199673
---
Total:  1.32439

@swalkinshaw
Copy link
Member

I ran your script and saw similar numbers (but much lower) using a default install and default theme:

Without opcache:

1/10:	0.428497
2/10:	0.332736
3/10:	0.328061
4/10:	0.326045
5/10:	0.323269
6/10:	0.333123
7/10:	0.323723
8/10:	0.341296
9/10:	0.338455
10/10:	0.326333
---
Total:	0.340153

With opcache enabled:

1/10:	0.579579
2/10:	0.157527
3/10:	0.156097
4/10:	0.152035
5/10:	0.165091
6/10:	0.150111
7/10:	0.164437
8/10:	0.170205
9/10:	0.150592
10/10:	0.151921
---
Total:	0.199759

This was true even with revalidate_freq = 0. If that's true, then it would be the safest option in development since files are checked every request eliminating any time lag of cached files.

swalkinshaw added a commit that referenced this issue Nov 10, 2022
OPcache was previously disabled in development to ensure no stale cached
PHP. However, there are still performance benefits to having OPcache
enabled with a `revalidate_freq` of `0` which will result in OPcache
checking for updates on every request (instead of the default of `60` in
production).

This also enables the `enable_file_override` setting which can provide
better performance as long as `validate_timestamps` is enabled (and it
is by default).
@swalkinshaw
Copy link
Member

#1447 is up but we'll do some more testing.

swalkinshaw added a commit that referenced this issue Nov 10, 2022
OPcache was previously disabled in development to ensure no stale cached
PHP. However, there are still performance benefits to having OPcache
enabled with a `revalidate_freq` of `0` which will result in OPcache
checking for updates on every request (instead of the default of `60` in
production).

This also enables the `enable_file_override` setting which can provide
better performance as long as `validate_timestamps` is enabled (and it
is by default).
swalkinshaw added a commit that referenced this issue Nov 10, 2022
OPcache was previously disabled in development to ensure no stale cached
PHP. However, there are still performance benefits to having OPcache
enabled with a `revalidate_freq` of `0` which will result in OPcache
checking for updates on every request (instead of the default of `60` in
production).
swalkinshaw added a commit that referenced this issue Nov 10, 2022
OPcache was previously disabled in development to ensure no stale cached
PHP. However, there are still performance benefits to having OPcache
enabled with a `revalidate_freq` of `0` which will result in OPcache
checking for updates on every request (instead of the default of `60` in
production).
swalkinshaw added a commit that referenced this issue Nov 11, 2022
OPcache was previously disabled in development to ensure no stale cached
PHP. However, there are still performance benefits to having OPcache
enabled with a `revalidate_freq` of `0` which will result in OPcache
checking for updates on every request (instead of the default of `60` in
production).
paulbrzeski pushed a commit to paulbrzeski/trellis that referenced this issue Mar 3, 2023
OPcache was previously disabled in development to ensure no stale cached
PHP. However, there are still performance benefits to having OPcache
enabled with a `revalidate_freq` of `0` which will result in OPcache
checking for updates on every request (instead of the default of `60` in
production).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants