We have a fairly large site (~10mln monthly PVs) and switched to Site Kit a few months ago. I have a CS background and performance issues are of specific interest to me. I have been noticing more and more lately that any pages with the WP admin bar, such as the front pages when logged in or the backend pages, occasionally have colossal delays in server-side page generation time.
It's not uncommon for me to wait for a story to load for 20s+ that usually takes less than 0.5s. This doesn't affect regular visitors as they don't see the admin bar.
We turned on Query Monitor but all it did was confirm the issue is not in the SQL part of the page-building process.
We ended up building a custom debug system that attempts to analyze hooks and various modules and it consistently pointed to wp_head and wp_enqueue_scripts. This puzzled us so we then extended the system further, and this Site Kit closure is what it consistently points out now:
https://github.com/google/site-kit-wp/blob/develop/includes/Core/Admin_Bar/Admin_Bar.php#L90-L109.
Unfortunately, I don't have further details yet on what exactly is causing this delay, but it's driving me absolutely nuts as pages randomly take 10-20s+ to load on extremely beefy hardware (4x AMD EPYC 7601 32-Core Processor, 64GB RAM servers).

_Do not alter or remove anything below. The following sections will be managed by moderators only._
We have newrelic installed on one of the servers, and I just found an example with lots of details that proves Site Kit is the culprit and will hopefully let you come up with a solution. Please take a look.
To me, it looks like for whatever reason, Site Kit calls has_data_for_url for Analytics and Search Console synchronously with the page load, and then actually executes external Guzzle HTTP requests to get remote data, which ends up slowing down the main page load by many many seconds. Why is this happening? These sorts of requests should never happen synchronously and should be either loaded via Ajax or cron.





@archon810 Thank you for raising this issue and for the detailed traces from New Relic. Ironically, we do the has_data_for_url call you mentioned in an effort to _reduce_ page load impact (by not loading the admin-bar menu code for pages that lack data).
We cache this has data attribute, so you are probably seeing a load where the cache has expired. Some possible ideas:
@adamsilverstein Thanks for replying. Yeah, the typical Transient strategy is quite faulty in my opinion because it doesn't regenerate while serving stale cache and can therefore result in long load times as well as race conditions reloading the data. We've typically stayed away from this approach and served stale cache while regenerating.
Having said that, the approach in your second suggestion sounds fine, especially since it can probably be implemented and rolled out very quickly.
Moreover, having the Site Kit entry in the admin bar for some posts but not others has always confused me very much. I'd rather it be always present and say why the data isn't there or offer a way to sync it on the fly via Ajax.
@eclarke1 @fhollis This issue doesn't require its own IB or PR since the fix will be covered via #2508. We should still keep it open for tracking purposes because the bug described here is worth its own issue distinct from the #2508 enhancement. So basically we can consider this one an estimate of 0 and just move it along #2508 in the board.
Given the importance of this issue, could we maybe bump it up to https://github.com/google/site-kit-wp/milestone/62 instead of https://github.com/google/site-kit-wp/milestone/63? https://github.com/google/site-kit-wp/milestone/63 hasn't been started yet and is due in 10 days.
@aaemnnosttv This is coupled to #2508 - please move it along when done with CR (at least by the time this has been merged).