Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2025-01-07 13:07:19

spiridon
Member
From: Mariupol, Ukraine
Registered: 2011-01-30
Posts: 53
Website

Implement Server-Timing performance metrics

Hi, guys!

Do you think it will be difficult to implement performance metrics? It seems like it would be very useful…

The HTTP Server-Timing response header communicates one or more performance metrics about the request-response cycle to the user agent. It is used to surface backend server timing metrics (for example, database read/write, CPU time, file system access, etc.) in the developer tools in the user’s browser or in the PerformanceServerTiming interface.

You can get more information at this link

I tried it on a non-Textpattern project. This is how it looks in the browser developer tools.

Last edited by spiridon (2025-01-07 13:07:53)

Offline

#2 2025-01-07 13:30:57

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,498
Website GitHub

Re: Implement Server-Timing performance metrics

What sort of metrics would be useful, over and above those shown for query timing and memory utilisation at the bottom of the page in debugging mode?

Not sure how much granular detail we can supply to gauge things like FCP and render-blocking resources. I’ve never looked at the header. Interesting idea for a plugin though.

Last edited by Bloke (2025-01-07 13:33:52)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2025-01-07 16:44:26

spiridon
Member
From: Mariupol, Ukraine
Registered: 2011-01-30
Posts: 53
Website

Re: Implement Server-Timing performance metrics

As a performance engineer, it would be useful for me to see in the page response timings how much time the application server took out of the total, and it would also be very useful to have a label that the response is from the cache (for example I use the asy_jpcache plugin, it, of course, adds an X-Cache header to the response, but it would be very convenient to see this in the timings).

As for the requirements for the web server, it would be useful to keep track of the response_time duration and to be able to calculate the difference between response_time and application_time – this is the time it took to process the request, connect to the upstream, compress the response, etc. (This is not the case with Textpattern. I’m just describing my vision of how these metrics can be useful.)

The debugging information in the body of the page is useful, but it is more for the backend developer, or debugging the performance of the application server itself.

Implementing it as a plugin seems convenient because it will be needed only temporarily, so the ability to enable/disable it will come in handy. But will the plugin be able to get the necessary information? And how will it interact with other plugins if some plugin also requires adding performance metrics?

Offline

#4 2025-01-08 12:47:37

spiridon
Member
From: Mariupol, Ukraine
Registered: 2011-01-30
Posts: 53
Website

Re: Implement Server-Timing performance metrics

Damn. I’m stupid. Just add this code to the beginning of index.php:

$t_before = microtime(true);

And then this code to the end of the file:

$t_end = microtime(true);
$create_timing_entry = function($name, $dur) { return $name.';dur='.number_format($dur * 1000, 2, '.', ''); };
$timings = array(
  $create_timing_entry('prepend', $t_before - $_SERVER["REQUEST_TIME_FLOAT"]),
  $create_timing_entry('cms', $t_end - $t_before),
  $create_timing_entry('total', $t_end - $_SERVER["REQUEST_TIME_FLOAT"])
);
header('Server-Timing: '.implode(',', $timings));

And we’ll get what we want:

Offline

#5 2025-01-08 12:50:01

spiridon
Member
From: Mariupol, Ukraine
Registered: 2011-01-30
Posts: 53
Website

Re: Implement Server-Timing performance metrics

And if we add the end hook to the asy_jpcache plugin, we will get this:

Offline

#6 2025-01-08 18:27:08

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,498
Website GitHub

Re: Implement Server-Timing performance metrics

Nice. Can you add the necessary stuff as a pre-publish script or is that too late to send headers?


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#7 Yesterday 10:09:34

spiridon
Member
From: Mariupol, Ukraine
Registered: 2011-01-30
Posts: 53
Website

Re: Implement Server-Timing performance metrics

No. It should be a post-publish script because it should measure the time spent building the page.

Right now the code that adds headers invokes at the end of index.php

The pre-publish script calls the asy_jpcache plugin in which I also added headers before exit (in case the page was found in the cache).

Offline

#8 Yesterday 10:33:33

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,498
Website GitHub

Re: Implement Server-Timing performance metrics

Duh, of course. Sorry for being dim.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB