heavy Drupal 7 site performance issue - php

I am running a drupal 7 website on a Linux server (with 4 cores and 12GB RAM) with LEMP (nginx+php5-fpm+mysql).
The drupal installation has a large number of modules enabled(all of which are needed).
I also use apc + boost + memcache + authcache for caching. The caching seems to be working (i see pages being served from cache)
and the site has a reasonable response time.
I have run stress tests with the website running in a url like www-1.example.com. (www-1.example.com points to the ip of my webserver, let's say x.x.x.x)
and the results are fine (for up to 100 concurrent users)
The problem starts when I change the dns so that www.example.com also points to x.x.x.x. Then the cpu of my webserver (all 4 cores) reach 100% at short time.
I have been expereimenting with the following parameters on the www.conf file with no luck:
Configuration 1:
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 200
result: 100% cpu usage, low memory usage
Configuration final:
pm.max_children = 300
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 200
result: low cpu usage, 100% memory usage
Can anyone guide me to find the optimal comfiguration or has any idea on what can cause the 100% cpu usage?
How do i culculate the maximum number of concurrent usages that can run without problem based on server parameters?

Related

PHP server change between Apache and Nginx: 3x performance loss

Our current infra works fine with Apache + PHP and MySQL managed elsewhere. I have not done any particular optimization on this server. These are the initial configurations. Except for memory_limit and execution_time.
I am in the process of changing our infra to something more modern with bigger server and Nginx + PHP-FPM.
While working on this problem, I realize that we have big performance losses using this new infra. It's a shame to have more powerful servers and it's slower.
I watched the server load and nothing. Memory and CPU are barely 2%
I pulled out my Inspector badge and began to eliminate guesswork. Obviously, my tests use exactly the same case, same framework, same version of our app, ...
Here are my tests:
I installed on newer Apache + PHP servers and in fact Apache is even slower than with Nginx (no surprise :)).
I created a simple "hello wok" PHP file to test if it could come from Nginx. So I am not using our framework which could be involved.
Everything is normal TTFB times are almost equivalent (~ 24ms). Nginx on the new infra is 2ms faster.
While going with our framework, I added a piece of basic code which amounts to doing a "Hello wok" but using our framework.
Ah there, we have a problem. Finally ! Old infra 61ms, New infra: 184ms.
The new infra is therefore 3x slower to load the framework.
Obviously, I did a lot of optimizing the existing code but seeing that, I'll never be able to optimize it and make it 3x faster.
In all cases: Why the same code takes 3x longer to run on different environments.
The only thing that occurs to me right now is a PHP and PHP-FPM configuration problem.
In these configurations, do you know of any reasons that could have such a slowdown?
Obviously I looked for and tested things to optimize but it's always the same.
Here is my php.ini file:
date.timezone = Europe/Paris
opcache.enable = 0
apc.enabled = 1
apc.enable_cli = 1
upload_max_filesize = 128M
post_max_size = 128M
realpath_cache_size=4096k
realpath_cache_ttl=7200
display_errors = Off
display_startup_errors = Off
access.log = /proc/self/fd/2
error_log = /proc/self/fd/2
error_reporting = E_ALL ^ E_STRICT
; output_buffering=4096
pm = dynamic
pm.max_children = 5
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 1000
max_execution_time=600
memory_limit = 2048M
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 2048
; opcache.interned_strings_buffer = 32
; opcache.max_accelerated_files = 100000
; opcache.max_wasted_percentage = 15
opcache.revalidate_freq = 0
; opcache.validate_timestamps = 1
Do you have any advice ? recommendations ? ideas ?
thanks in advance
Are you doing well
EDIT #1
Server Information:
Old server:
VM with 1 processor / 2GB RAM
No database, just Apache + PHP
Framework specific cache system: file
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 6
model name : Common KVM processor
cpu MHz : 3504.002
cache size : 4096 KB
[...]
New server:
Docker container on a 3 processor / 4GB RAM server
I do not have the speeds of each processor. This is vCPU.
No database
Container PHP-FPM + Container Nginx
Framework specific cache system: APC
processor : 0
vendor_id : AuthenticAMD
cpu family : 23
model : 1
model name : AMD EPYC 7281 16-Core Processor
stepping : 2
cpu MHz : 2096.062
cache size : 512 KB
[...]
processor : 1
vendor_id : AuthenticAMD
cpu family : 23
model : 1
model name : AMD EPYC 7281 16-Core Processor
stepping : 2
cpu MHz : 2096.062
cache size : 512 KB
[...]
processor : 2
vendor_id : AuthenticAMD
cpu family : 23
model : 1
model name : AMD EPYC 7281 16-Core Processor
stepping : 2
cpu MHz : 2096.062
cache size : 512 KB
[...]
Others :
The database is managed in a provider
The sessions are stored in the database
Despite what you will read all over the internet, under low loads, Apache+mod_php is significantly faster than nginx+php-fpm. The latter architecture offers better capacity and scaling. That does not explain the difference in performance reported here.
The only thing you have told us about the two infrastructures is that one is old and one is new. If all you know about them is that you clicked a button in a cloud control panel then it can be difficult to find out more (if this is a Linux VM, check /proc/cpuinfo and mpstat paying particular attention to the steal time). The metrics you measured by testing will be highly dependent on single thread performance which is not a good indicator of throughput on a multi-cpu machine or any machine under heavy load.
But the first place I would be looking is at the storage latency / increasing opcache.revalidate_freq.

How to fix problem with website speed (500 records in one page) on Nginx + php-fpm server

I moved me shop to new server and I have problem with loading time. I have 500 products in one page (no pagination), first loading takes about 8 seconds (in old server was 2 sec maximum). Then website works very fast because cache plugin is working.
I have Prestashop 1.7.5.2 with very good cache plugin and two powerful servers:
Only with database: Apache, phpMyAdmin (RAM 60 GB, Processor 16 vCores, SSD)
Nginx, php-fpm 7.2 (RAM 60 GB, Processor 16 vCores, SSD)
Only page with products have this problem. I know, 500 products without pagination is not perfect idea but it have to be like that.
Maybe can be wrong php-fpm config?
Currently I have this:
pm = ondemand
pm.max_children = 16
pm.max_requests = 4000
pm.process_idle_timeout = 30s
I will be very grateful for your help.

php-fpm slow log: curl_multi_select takes over 10 sec

I have a strange problem. On an amazon m4.large with nginx and php-fpm , that sits behind a loadbalancer and with a memcached and s3 bucket when the visitors reach certain number, the CPU of the instance hits almost 97% for a short period of time. I have enabled the slow logging functionality of php-fpm and found multiple occurances of this:
script_filename = /var/www/xxx/web/app.php
[0x00007f231c23c6c0] curl_multi_select() /var/www/xxx/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php:101
[0x00007f231c23c560] tick() /var/www/xxx/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php:125
[0x00007f231c23c448] execute() /var/www/xxx/vendor/guzzlehttp/promises/src/Promise.php:246
[0x00007f231c23c338] invokeWaitFn() /var/www/xxx/vendor/guzzlehttp/promises/src/Promise.php:223
[0x00007f231c23c208] waitIfPending() /var/www/xxx/vendor/guzzlehttp/promises/src/Promise.php:267
[0x00007f231c23c0d8] invokeWaitList() /var/www/xxx/vendor/guzzlehttp/promises/src/Promise.php:225
[0x00007f231c23bfa8] waitIfPending() /var/www/xxx/vendor/guzzlehttp/promises/src/Promise.php:267
[0x00007f231c23be78] invokeWaitList() /var/www/xxx/vendor/guzzlehttp/promises/src/Promise.php:225
[0x00007f231c23bd48] waitIfPending() /var/www/xxx/vendor/guzzlehttp/promises/src/Promise.php:62
[0x00007f231c23bc08] wait() /var/www/xxx/vendor/aws/aws-sdk-php/src/AwsClientTrait.php:58
[0x00007f231c23bac0] execute() /var/www/xxx/vendor/league/flysystem-aws-s3-v3/src/AwsS3Adapter.php:319
[0x00007f231c23b998] getMetadata() /var/www/xxx/vendor/league/flysystem-aws-s3-v3/src/AwsS3Adapter.php:342
[0x00007f231c23b8a0] getSize() /var/www/xxx/vendor/league/flysystem/src/Filesystem.php:329
[0x00007f231c23b720] getSize() /var/www/xxx/vendor/league/glide-symfony/src/Responses/SymfonyResponseFactory.php:38
What i am trying to do is, to fetch an image from a S3 bucket. Both the instances from where i am executing this code and the bucket sit in the same aws zone. So the latency shall be pretty low. I have configured the aws-sdk-php with the following params:
timeout = 5
connect_timeout = 1
delay = 0
PHP and PHP-FPM are in version 5.6.36
this is an outtake from the php-fpm configuration:
pm = dynamic
pm.max_children = 505
pm.start_servers = 15
pm.min_spare_servers = 15
pm.max_spare_servers = 135
pm.max_requests = 5000
The latency time of the loadbalancer peaks, when the visitors rise and the CPU load reaches ~97%. The RAM usage is pretty low though.
Does someone had a similar problem with curl_multi_selection() taking so long?

understand pm.max_children tuning

I have done some investigations and I have found this to calculate and adjust pm.max_children value
https://myshell.co.uk/blog/2012/07/adjusting-child-processes-for-php-fpm-nginx/
but for example :
I have 8Gb on my server
I'm hosting 30 websites
php-fpm average process size around 40mb
php-fpm max process size around 80mb
I want to allocate 5Gb max of my memory to php-fpm processes
If I apply this :
pm.max_children = Total RAM dedicated to the web server / Max child process size
So in my case :
pm.max_children = 5120 / 80 = 64
But If I add pm.max_children = 64 on each php-fpm website conf files, this means every website can use 64 children process X size of 1 process (ex 40mb) = 2560Mb
And If we imagine, at the same time, all the 30 website have reach the pm.max_children value, we will have : 2560Mb (max per website) x 30 websites = 76 800 Mb
Am I correct?
Si yes, this means when many websites are hosted on the same server, we have to divide the result of the calcul pm.max_children = 5120 / 80 = 64 by the number of websites hosted (here 30).
So 64 / 30 = 2,1 and pm.max_children = 2 per website
Is it correct or not?
Thanks

How to calculate used memory for php-fpm?

Suppose I have a php application that runs with php-fpm. Below are the options for php/php-fpm and opcache. How can I calculate how much memory is used by all of the components?
memory_limit=128M
...
opcache.memory_consumption=128M
...
[www]
pm = dynamic
pm.max_children = 2
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 2
Here is my thinking. php-fpm is starting 1 process with 128 MB and opcache 128MB = 256 MB. If php-fpm creates a new server/process we will have 2 processes with 256 MB and opcache 128 MB = 384 MB. Is that correct?
The background is to calculator the correct memory limits for a Kubernetes deployment.
If you're talking about how much memory the app uses, you can use something like
ps aux | grep -P "fpm|COMMAND"
Once you have that, 10MB for e.g., you know how many MB your FPM thread is using. This is your child.
Next you need to know how much memory your server is using. Open htop, sort by mem (shift + m), take all your top mem usages above 1MB, which are not php related, add them up, thats your current server usage (non php).
If you can load your server when you check this that helps, particularly if your running mysql, that uses alot of memory, so helps if you load your server a bit there when you do your check.
If you find for e.g. your 8GB server uses only 512MB mem, you can reserve some extra space for it, say 750MB, or 1GB. that leaves the remaining 7GB to divide by your app mem usage. 10MB/7GB = 700 threads/childs.

Categories