moderntribe / square-one

Modern Tribe's legacy WordPress framework for the classic editor.
GNU General Public License v2.0
89 stars 20 forks source link

Add optimized opcache configuration #930

Closed defunctl closed 2 years ago

defunctl commented 2 years ago

What does this do/fix?

Improves container PHP performance. The default max accelerated files out of the box is 10000, and a fresh square-one has 16388.

Devs can run the following in their project to determine the amount of PHP files that exist, and adjust their project's default accordingly.

find . -type f -print | grep php | wc -l

QA

I'd suggest you copy these into a (modern) project you're working on right now, do a so stop; so start and work as normal. Let us know in the comments if anything strange happens. I'm mostly curious for macOS users.

Specifically a bit concerned about this one on a mac: https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.file_update_protection

You can also dump https://www.php.net/manual/en/function.opcache-get-status.php to see some stats such.

Tests

Does this have tests?

dpellenwood commented 2 years ago

Thanks for taking a stab at this, Justin.

I didn't do a thorough test on the functional usability of this change, but I did confirm that changing PHP files were reflected on reload in the browser.

I ran some Apache Bench numbers against main and this branch and noticed only slight improvements in requests per second and average response times. I also tested the new VirtioFS file system for Docker to see how that affects these times.

I used the following bench commands to test: ab -n 100 -c 10 https://square1.tribe/

Test Conditions:

main branch using default Docker file system and xDebug ON

Requests per second:    3.60 [#/sec] (mean)
Time per request:       2777.358 [ms] (mean)
Time per request:       277.736 [ms] (mean, across all concurrent requests)
Transfer rate:          129.69 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       12   17   8.1     14      45
Processing:   933 2569 362.0   2624    3522
Waiting:      897 2561 363.3   2616    3515
Total:        965 2586 358.3   2638    3539

main branch using default Docker file system and xDebug OFF

Requests per second:    5.16 [#/sec] (mean)
Time per request:       1939.210 [ms] (mean)
Time per request:       193.921 [ms] (mean, across all concurrent requests)
Transfer rate:          185.74 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       12   19   8.8     15      45
Processing:   477 1386 467.5   1301    5402
Waiting:      477 1384 463.8   1300    5358
Total:        520 1405 468.4   1317    5432

feature/php-optimization (this) branch using default Docker file system and xDebug ON

Requests per second:    3.65 [#/sec] (mean)
Time per request:       2738.450 [ms] (mean)
Time per request:       273.845 [ms] (mean, across all concurrent requests)
Transfer rate:          131.53 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       11   18   8.5     15      43
Processing:   937 2536 355.7   2553    3559
Waiting:      896 2528 357.5   2545    3553
Total:        978 2554 351.8   2568    3573

feature/php-optimization (this) branch using default Docker file system and xDebug OFF

Requests per second:    7.12 [#/sec] (mean)
Time per request:       1404.481 [ms] (mean)
Time per request:       140.448 [ms] (mean, across all concurrent requests)
Transfer rate:          256.45 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       10   18   9.3     15      47
Processing:   401 1297 210.6   1306    1889
Waiting:      397 1295 210.6   1305    1887
Total:        421 1315 209.2   1322    1907

VirtioFS for Docker Tests:

main branch using default Docker file system and xDebug ON

Requests per second:    6.23 [#/sec] (mean)
Time per request:       1604.965 [ms] (mean)
Time per request:       160.496 [ms] (mean, across all concurrent requests)
Transfer rate:          161.81 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        8   20  10.0     15      49
Processing:   690 1435 217.6   1427    2116
Waiting:      689 1434 217.8   1427    2116
Total:        727 1455 216.8   1441    2154

main branch using default Docker file system and xDebug OFF

Requests per second:    10.93 [#/sec] (mean)
Time per request:       915.075 [ms] (mean)
Time per request:       91.508 [ms] (mean, across all concurrent requests)
Transfer rate:          283.80 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        9   22  13.2     17      55
Processing:   284  553 320.3    476    3476
Waiting:      284  552 320.0    476    3472
Total:        338  574 322.5    498    3501

feature/php-optimization (this) branch using default Docker file system and xDebug ON

Requests per second:    5.17 [#/sec] (mean)
Time per request:       1933.986 [ms] (mean)
Time per request:       193.399 [ms] (mean, across all concurrent requests)
Transfer rate:          134.28 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       11   33  20.0     25      83
Processing:   707 1715 328.0   1618    2932
Waiting:      706 1714 328.1   1618    2932
Total:        767 1747 328.2   1662    2992

feature/php-optimization (this) branch using default Docker file system and xDebug OFF

Requests per second:    9.08 [#/sec] (mean)
Time per request:       1101.111 [ms] (mean)
Time per request:       110.111 [ms] (mean, across all concurrent requests)
Transfer rate:          235.85 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        9   25  12.1     23      76
Processing:   347  627 427.2    548    4653
Waiting:      347  627 426.7    548    4648
Total:        393  652 432.7    565    4729

Clearly, there is some slight improvement with the opCache changes, and a pretty drastic improvement with the VirtioFS change. Testing this on an Mac with fewer resources may actually surface a more noticeable difference.

My machine & Docker specs for reference:

defunctl commented 2 years ago

@dpellenwood thanks so much for testing this!

Those numbers still make me super sad vs docker on Linux :(

I'm using hey which is a very similar tool:

hey -n 100 -c 10 -h2 https://square1.tribe/

main branch with this php.ini and xdebug on:

Summary:
  Total:    1.4411 secs
  Slowest:  0.2267 secs
  Fastest:  0.0831 secs
  Average:  0.1378 secs
  Requests/sec: 69.3918

Response time histogram:
  0.083 [1] |■
  0.097 [0] |
  0.112 [5] |■■■■■
  0.126 [25]    |■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.141 [37]    |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.155 [15]    |■■■■■■■■■■■■■■■■
  0.169 [6] |■■■■■■
  0.184 [5] |■■■■■
  0.198 [4] |■■■■
  0.212 [0] |
  0.227 [2] |■■

Latency distribution:
  10% in 0.1187 secs
  25% in 0.1249 secs
  50% in 0.1316 secs
  75% in 0.1463 secs
  90% in 0.1735 secs
  95% in 0.1837 secs
  99% in 0.2267 secs

Details (average, fastest, slowest):
  DNS+dialup:   0.0003 secs, 0.0831 secs, 0.2267 secs
  DNS-lookup:   0.0001 secs, 0.0000 secs, 0.0021 secs
  req write:    0.0000 secs, 0.0000 secs, 0.0000 secs
  resp wait:    0.1342 secs, 0.0679 secs, 0.1923 secs
  resp read:    0.0001 secs, 0.0000 secs, 0.0002 secs

Status code distribution:
  [200] 100 responses

main branch with this php.ini and xdebug off:

Summary:
  Total:    0.6496 secs
  Slowest:  0.1245 secs
  Fastest:  0.0487 secs
  Average:  0.0619 secs
  Requests/sec: 153.9310

Response time histogram:
  0.049 [1] |■
  0.056 [37]    |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.064 [32]    |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.071 [21]    |■■■■■■■■■■■■■■■■■■■■■■■
  0.079 [3] |■■■
  0.087 [1] |■
  0.094 [0] |
  0.102 [2] |■■
  0.109 [1] |■
  0.117 [1] |■
  0.124 [1] |■

Latency distribution:
  10% in 0.0524 secs
  25% in 0.0544 secs
  50% in 0.0595 secs
  75% in 0.0650 secs
  90% in 0.0688 secs
  95% in 0.0942 secs
  99% in 0.1245 secs

Details (average, fastest, slowest):
  DNS+dialup:   0.0004 secs, 0.0487 secs, 0.1245 secs
  DNS-lookup:   0.0001 secs, 0.0000 secs, 0.0015 secs
  req write:    0.0000 secs, 0.0000 secs, 0.0001 secs
  resp wait:    0.0581 secs, 0.0205 secs, 0.0872 secs
  resp read:    0.0001 secs, 0.0000 secs, 0.0002 secs

Status code distribution:
  [200] 100 responses

A better real world, development benchmark would be to send a single request, since we're not doing concurrent requests when developing and just reloading a page to see changes, say something like:

hey -n 20 -c 1 -h2 https://square1.tribe/

main branch with this php.ini and xdebug on:

Summary:
  Total:    1.3566 secs
  Slowest:  0.0910 secs
  Fastest:  0.0575 secs
  Average:  0.0678 secs
  Requests/sec: 14.7433

Response time histogram:
  0.057 [1] |■■■■
  0.061 [10]    |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.064 [0] |
  0.068 [0] |
  0.071 [2] |■■■■■■■■
  0.074 [1] |■■■■
  0.078 [2] |■■■■■■■■
  0.081 [0] |
  0.084 [2] |■■■■■■■■
  0.088 [0] |
  0.091 [2] |■■■■■■■■

Latency distribution:
  10% in 0.0579 secs
  25% in 0.0591 secs
  50% in 0.0599 secs
  75% in 0.0770 secs
  90% in 0.0906 secs
  95% in 0.0910 secs
  0% in 0.0000 secs

Details (average, fastest, slowest):
  DNS+dialup:   0.0005 secs, 0.0575 secs, 0.0910 secs
  DNS-lookup:   0.0000 secs, 0.0000 secs, 0.0009 secs
  req write:    0.0000 secs, 0.0000 secs, 0.0000 secs
  resp wait:    0.0672 secs, 0.0573 secs, 0.0909 secs
  resp read:    0.0001 secs, 0.0001 secs, 0.0001 secs

Status code distribution:

main branch with this php.ini and xdebug off:

Summary:
  Total:    0.4667 secs
  Slowest:  0.0911 secs
  Fastest:  0.0156 secs
  Average:  0.0233 secs
  Requests/sec: 42.8508

Response time histogram:
  0.016 [1] |■■■
  0.023 [13]    |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.031 [5] |■■■■■■■■■■■■■■■
  0.038 [0] |
  0.046 [0] |
  0.053 [0] |
  0.061 [0] |
  0.068 [0] |
  0.076 [0] |
  0.084 [0] |
  0.091 [1] |■■■

Latency distribution:
  10% in 0.0160 secs
  25% in 0.0163 secs
  50% in 0.0175 secs
  75% in 0.0265 secs
  90% in 0.0281 secs
  95% in 0.0911 secs
  0% in 0.0000 secs

Details (average, fastest, slowest):
  DNS+dialup:   0.0003 secs, 0.0156 secs, 0.0911 secs
  DNS-lookup:   0.0001 secs, 0.0000 secs, 0.0013 secs
  req write:    0.0000 secs, 0.0000 secs, 0.0000 secs
  resp wait:    0.0229 secs, 0.0154 secs, 0.0847 secs
  resp read:    0.0001 secs, 0.0000 secs, 0.0001 secs

Status code distribution:
  [200] 20 responses

If you have time, I'd be curious how things are for you if combine these changes into testing this PR: https://github.com/moderntribe/square-one/pull/931

Making sure to read the instructions and the comments fully before giving it a go.


Machine Specs:

-------------- 
OS: Manjaro Linux x86_64 
Kernel: 5.18.6-1-MANJARO 
Uptime: 3 days, 58 mins 
Packages: 1517 (pacman) 
Shell: bash 5.1.16 
Resolution: 1920x1080, 1920x1080, 1440x2560 
DE: Xfce 
WM: Xfwm4 
WM Theme: Arc-Dark 
Theme: Arc-Dark [GTK2/3] 
Icons: ePapirus-Dark [GTK2/3] 
Terminal: xfce4-terminal 
Terminal Font: Source Code Pro Semi-Bold 11 
CPU: AMD Ryzen 9 3900X (24) @ 3.800GHz 
GPU: NVIDIA GeForce RTX 2080 SUPER 
Memory: 21774MiB / 32007MiB 
ChrisMKindred commented 2 years ago

I have cycled through all the different test cases with a new SquareOne build and the most recent Main. I didn't experience any issues while updating blocks and testing out some other updates. I didn't see a noticeable performance boost in practice, but statistically, there was a small performance increase.

Here are the numbers I am getting running ab -n 100 https://square1.tribe/.

Main Branch php.ini xDebug off

Requests per second:    5.21 [#/sec] (mean)
Time per request:       191.799 [ms] (mean)
Time per request:       191.799 [ms] (mean, across all concurrent requests)
Transfer rate:          110.50 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       11   13   4.2     12      45
Processing:   117  178 271.3    138    2804
Waiting:      116  178 271.3    138    2804
Total:        128  192 274.6    150    2850

Main Branch php.ini xDebug on

Requests per second:    4.02 [#/sec] (mean)
Time per request:       249.007 [ms] (mean)
Time per request:       249.007 [ms] (mean, across all concurrent requests)
Transfer rate:          85.12 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       11   13   2.5     13      36
Processing:   191  236 109.4    221    1294
Waiting:      191  236 109.4    221    1294
Total:        204  249 111.8    234    1330so 

Main Branch updated php.ini xDebug off

Requests per second:    6.40 [#/sec] (mean)
Time per request:       156.145 [ms] (mean)
Time per request:       156.145 [ms] (mean, across all concurrent requests)
Transfer rate:          135.73 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       12   13   1.2     12      22
Processing:   116  144 137.4    128    1500
Waiting:      116  143 137.4    128    1500
Total:        129  156 138.4    140    1523

Main Branch updated php.ini xDebug on

Requests per second:    4.15 [#/sec] (mean)
Time per request:       240.906 [ms] (mean)
Time per request:       240.906 [ms] (mean, across all concurrent requests)
Transfer rate:          87.98 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       11   13   2.1     12      33
Processing:   187  228 150.0    198    1232
Waiting:      187  228 150.0    198    1232
Total:        199  241 151.4    211    1264

Environment Specs 2019 MacBook Pro, 2.4 GHz Quad-Core Intel Core i5, 8 GB Memory, Monterey 12.4 😭 Docker For Mac Version 4.10.1 (82475) with 4 CPUs, 7 GB Memory

dpellenwood commented 2 years ago

Hmmm.... Not sure why this got merged with the site footer. :\

Looks like I borked a merge conflict somewhere. Reopening and restoring the branch now...