textpattern / textpattern-com-website

Official website of the Textpattern project.
https://textpattern.com
GNU General Public License v2.0
24 stars 8 forks source link

Web Informer widget causing delay in initial server response #343

Closed philwareham closed 3 years ago

philwareham commented 3 years ago

We've ascertained that fetching the HTML for the web informer widget causes around 300ms of lag on the initial server response - pushing us into a non-pass on Core Web Vitals. I'd like to reduce that lag if possible but we can't cache the HTML, for reasons.

Currently the code to fetch this HTML is:

if (!gps('txpreview')) {
    $opts = array('http' => array('timeout' => 1.6));
    $url = 'https://website.informer.com/widget/textpattern.com-2';
    $context = stream_context_create($opts);
    $widget = file_get_contents($url, false, $context);

    if ($widget) {
        echo $widget;
    }
}

I'm wondering if there is any performance gain from using stream_copy_to_stream instead of file_get_contents?

@bloke what do you think, and if so can you give me a pointer on the code needed?

Bloke commented 3 years ago

stream_copy_to_stream() or, failing that, a homegrown fgets() (or equivalent with a streaming context) might well be faster. Usually these things are only of benefit for large files, and this content is nowhere near what anyone would call 'large'. The delay may well be attributable to the round-trip connection rather than streaming, but it's gotta be worth a shot. I'll try a few tests locally and see what happens.

EDIT: And we're already using a streaming context with file_get_contents() so I'm not sure if there's any speed benefits to be had here.

philwareham commented 3 years ago

@rwetzlmayr hi Robert, do you know how the above is measured by customer? i.e. Is it based on hits to the endpoint HTML?

I'd like to cache the HTML for a period of time if I can, but not if it is based on direct hits.

rwetzlmayr commented 3 years ago

@philwareham I do not know whether this is measured by the customer but I don't think so...

philwareham commented 3 years ago

Thanks Robert, I will test some short caching of the HTML (say, 10 minutes) and see if it improves matters.

philwareham commented 3 years ago

OK, caching that item speeds the page up massively; was ~560ms, now ~110ms. Happy with that. If we can any kickback from customer I can revisit (but should hopefully be fine).

Screenshot 2021-02-12 at 09 53 53