Slevin
Slevin

Reputation: 4232

Why are my concurrent AJAX requests to PHP Scripts/WP REST API so slow?

After some investigation, I updated the title of the question. Please see my updates below.

Original question:

I'm building a website with Wordpress and make sometimes use of async calls to WP REST API endpoints.

Calling this endpoints from my AJAX functions leads often to TTFB times from at least ~780ms:

enter image description here

But if I open the URL/endpoint directly in the browser I get TTFB times that are 4-5 time faster:

enter image description here

I wonder where the delays come frome. I'm running this page on my local dev server with Apache 2.4, HTTP/2 and PHP 7 enabled.

What is the best way to monitor such performance "issues"?

Please mind: I'm not using Wordpress' built-in AJAX-functionality. I'm just calling something like

axios.get(`${url}/wp-json/wp/v2/flightplan`)

inside a React component that I've mounted in my homepage-template.

Update

Damn interesting: clearing cookies reduces TTFB a lot:

enter image description here

Update 2

After removing the other both AJAX calls, the flightplan request performs much faster. I think there are some issues with concurrent AJAX requests. I've read a bit about sessions locking, but since Wordpress and all of the installed plugins doesn't make use of sessions, this can't be the reason.

Update 3

Definitively, it has something to do with my local server setup. Just deployed the site to a "real" webserver:

enter image description here

But it would still be interesting to know how to setup a server that can handle concurrent better.

Update 4

I made a little test: calling 4 dummy-requests before calling the "real" ones. The script only returns a "Foobar" string. Everything looks fine at this time:

enter image description here

But when adding sleep(3) to the dummy AJAX-script, all other requests takes much longer too:

enter image description here

Why?

Upvotes: 12

Views: 2486

Answers (1)

MaximeK
MaximeK

Reputation: 2071

Because your Ajax call will wait the loading of all your WP plugins :)

So you need to make some test without plugin, and activate one by one to see which one slow down your ajax call.

Upvotes: 3

Related Questions