r/PHPhelp • • 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?

8 Upvotes

20 comments sorted by

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?

1

u/jamesphw Apr 24 '23

Yes, that's correct. The microtime wraps the main code (ie. the api/endpoint.php file).

It's the very first and very last thing that gets run.

4

u/HolyGonzo Apr 24 '23

Are you also CERTAIN that the start time variable is not accidentally overwritten? For example, if you did:

<?php
$start = microtime(true);
...bunch of code...
$start = microtime(true); // <-- somewhere buried in another piece of code, or maybe in some included file
...more code...
$elapsed = microtime(true) - $start;

A <1ms execution time seems awfully fast unless you're not doing much except reading from a cached file. That's generally not enough time to even connect to a local DB.

The simplest way to verify is to use a variable name that is likely to be unique:

<?php
$start_fullscript_zzz = microtime(true);
...code
$elapsed = microtime(true) - $start_fullscript_zzz;

You mentioned both Apache and React. Usually with React, I see people running React's socket server (specifically the HttpServer instance). Can you clarify the setup you have?

3

u/jamesphw Apr 24 '23

Well, the issue wasn't that I was capturing the microtime at the wrong moment, but rather that I didn't calculate the time in ms correctly (need to multiply by 1000 to get ms). Stupid mistake by me.

It doesn't quite answer why it's slow, but at least I can see my script execution is taking most of the time to return to the client.

2

u/HolyGonzo Apr 24 '23

So the script actually is taking most of the 1.38 seconds?

3

u/jamesphw Apr 24 '23

Yes. I've narrowed it down to concurrent requests to session_start() causing the slowness.

I'm aware that session_start requires a lock, but everywhere I can find that session_start is used, session_write_close is called as soon as writing to session variables is done. So I'm still working to find why it's so slow.

2

u/HolyGonzo Apr 24 '23

Are you doing any custom handling of sessions, or are you just using the default handling (files stored in your temp folder) ?

1

u/jamesphw Apr 24 '23

It's all the default handling. Would that make a difference?

1

u/PetahNZ Apr 24 '23

What are you storing in the session? If it small just use an encrypted cookie.

1

u/jamesphw Apr 24 '23

It is small, but it is not static. It's a 3rd party API token that needs to be refreshed every ~30-45 minutes and is tied to each specific user. It could be refreshed from any script, and once refreshed it must be shared across all scripts. Session variables are certainly not the only way to do it, but for now that's what I'm dealing with.

I can't figure out how to tell what is blocking a session_start though. As far as I'm aware, that should only be the case if another script has the session active.

→ More replies (0)

2

u/HolyGonzo Apr 24 '23

Well, it would if you were doing something custom and the custom handler was doing something odd.

With default handling, I would simply suggest that you try clearing your PHPSESSID cookie and try again. In the past, I ran into an issue where I had added session_write_close() to a script AFTER I already had the session locking problem, and it didn't seem to make any difference at all. Then I cleared my session ID and retested and it worked fine.

Also, make sure you have output buffering disabled.

4

u/[deleted] Apr 24 '23

First thing I'd want to check is that the opcache is enabled.

https://www.php.net/manual/en/book.opcache.php

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 = Off

1

u/BenL90 Apr 25 '23

huh, we can move session to memcache or redis? What kind of soccery is it?

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