r/PHPhelp • u/jamesphw • Apr 24 '23
PHP API Slowness
I'm running PHP 8.1 on Apache, and have a PHP API for a react-based application.
We recently added caching for API calls, and to my great surprise discovered very little performance improvement.
On Chrome, it shows that most of the request time is "Waiting for server response". Here's an example here using the Edge network tools.
If I measure the API script time from start to end (by using microtime() at the beginning and end), execution is < 1ms. This is down from 300-400ms before the caching. Yet somehow the time for the browser client isn't reduced!
How can I reduce the "Waiting for Server Response" time as shown in Chrome? Or, where can I look to understand where this time is being spent by Apache or PHP?
4
Apr 24 '23
First thing I'd want to check is that the opcache is enabled.
5
u/allen_jb Apr 24 '23
You would have to be loading a seriously significant number of files before opcache creates a delay of more than 1 second.
I would look for other causes:
(As mentioned in another post) If the microtime() call is not at the very very beginning of the script (eg. it's at the beginning of the controller / action code), there could be something executing earlier that's creating a significant delay. (Personally I think this is the most likely explanation, with no additional information to go on)
Look for delays inbetween pieces of your tech stack. What's going on at each step? Is there a delay between the webserver and php-fpm? Why? Check the logs for warnings of errors. (As an example, I'm not sure what the potential delay times involved are, but I believe if all the php-fpm workers are busy it may start queueing requests until a worker becomes available. php-fpm will usually scream in its logs if a pool gets anywhere near "busy").
1
u/jamesphw Apr 24 '23
I'm not sure what php-fpm is. No errors in the apache error log.
If there was a delay (e.g. hitting database or something), then it should show up in the overall script measurement.
I have a feeling there is something on the apache side slowing things because of a concurrent connection (there are multiple API requests at the same time, however the delay is not because of the browser limitation of 6 concurrent connections.
1
u/BenL90 Apr 25 '23
php-fpm is defacto nowdays on PHP Stack, it use proxy fcgi from apache/nginx to php.sock to render the php file.. so...
3
u/eurosat7 Apr 24 '23 edited Apr 24 '23
Maybe you added session handling to make the cache unique foreach user?
Are you using sessions with the default session handler relying on the filesystem? Its blocking by default and only allows one request at a time foreach session: It waits for a session_close(), which in most frameworks is done implicitly with the end of the php script.
If one session can have parallel calls you might want to switch over to p.e. memcached based session handling.
Install a memcache daemon and start it. Install php_memcached and activate this php extension.
Edit your php.ini
session.save_handler = memcached
session.save_path = "localhost:11211"To be save restart workers/webserver so you apply settings.
Maybe also need to change theese
session.lazy_write = On
memcached.sess_locking = Off1
2
u/Plastonick Apr 24 '23
I think you'll need to show more of what's going on.
90% of the time, slow PHP APIs are down to database requests.
1
u/AdAutomatic1446 Apr 24 '23
try to log the time in different points of the script, maybe line by line, this way you will know which takes the most and should be optimised. Also is this on local environment or some production / staging env? are you using any framework specific functions inside ? try removing thosr just for testing purposes
7
u/HolyGonzo Apr 24 '23
Just to confirm are you using microtime at the VERY beginning and VERY end of your script? So there is literally no other code at all running before or after? For example, you're not just adding it to the start and end of a script that is included/required by some other parent script? And you don't have any auto-prepend / auto-append / registered shutdown handlers?