Hi.

I use haterm for some tests and took the opportunity to close the open TODO about the times in the X-req and X-rsp headers.

This could be backported to 3.4 if considered useful, since haterm is a test tool. It applies as far as 3.4, where haterm first appeared.

Attached the patch series.

Regards
Aleks
From 22424edeac38f4f2356f11e931a1d5adcf50f0cd Mon Sep 17 00:00:00 2001
From: Aleksandar Lazic <[email protected]>
Date: Thu, 3 Dec 2026 21:00:47 +0100
Subject: [PATCH 4/4] REGTESTS: haterm: check the times reported in X-req and
 X-rsp

Check that X-req reports a time in microseconds, that X-rsp reports
the delay requested with ?t= and at least as much really spent
waiting, and that X-req reports -1 when the request body is only
drained after the response (?A=1). This is the first regtest for
haterm, it runs it with "mode haterm" in a regular frontend.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Aleksandar Lazic <[email protected]>
---
 reg-tests/haterm/haterm_times.vtc | 49 +++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 reg-tests/haterm/haterm_times.vtc

diff --git a/reg-tests/haterm/haterm_times.vtc b/reg-tests/haterm/haterm_times.vtc
new file mode 100644
index 000000000..e0de77e84
--- /dev/null
+++ b/reg-tests/haterm/haterm_times.vtc
@@ -0,0 +1,49 @@
+varnishtest "haterm: real times in the X-req and X-rsp headers"
+
+# The X-req header reports the time from the stream creation to the end of
+# the request, X-rsp the delay requested with ?t= and the one really spent
+# waiting, all in microseconds.
+
+feature cmd "$HAPROXY_PROGRAM -cc 'version_atleast(3.5-dev12)'"
+feature ignore_unknown_macro
+
+haproxy h1 -conf {
+    defaults
+	timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+	timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
+	timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"
+
+    frontend fe
+	mode haterm
+	bind "fd@${fe}"
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+	# no delay requested
+	txreq -url "/?s=0"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-req ~ "^time=[0-9]+ us$"
+	expect resp.http.x-rsp ~ ", time=0 us \\(0 real\\)$"
+
+	# 100 ms requested: at least 100000 us really spent
+	txreq -url "/?s=0&t=100"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-rsp ~ ", time=100000 us \\([1-9][0-9]{5,} real\\)$"
+
+	# body fully received before the response
+	txreq -req POST -url "/?s=0" -bodylen 1000
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-req ~ "^time=[0-9]+ us$"
+
+	# body drained after the response and sent only once the response
+	# was received: the request time is unknown when the headers are built
+	txreq -req POST -url "/?s=0&A=1" -nolen -hdr "Transfer-Encoding: chunked"
+	chunkedlen 100
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-req == "time=-1 us"
+	chunkedlen 0
+} -run
-- 
2.56.1

From be410a3a957ba215f7d1cdcedeccfbeb2a789874 Mon Sep 17 00:00:00 2001
From: Aleksandar Lazic <[email protected]>
Date: Thu, 3 Dec 2026 21:00:33 +0100
Subject: [PATCH 3/4] DOC: haterm: document the X-req and X-rsp headers

Describe the two informational headers of the responses and the times
they report in microseconds, including the -1 reported by X-req when
the request body is drained after the response.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Aleksandar Lazic <[email protected]>
---
 doc/haterm.txt | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/doc/haterm.txt b/doc/haterm.txt
index 3609731cb..7cf251bb7 100644
--- a/doc/haterm.txt
+++ b/doc/haterm.txt
@@ -138,3 +138,15 @@ HATerm:
      -  GET /?s=20k&c=1&t=700&K=30r HTTP/1.0
      -  GET /?r=500?s=0?c=0?t=1000 HTTP/1.0
 
+
+Each response carries two informational headers:
+
+    X-req: time=<req-time> us
+    X-rsp: id=dummy, code=<code>, cache=<0|1>[, chunked,] size=<size>,
+           time=<wait> us (<real-wait> real)
+
+<req-time> is the time in microseconds from the creation of the stream, once
+the request headers were received, to the end of the request, so it covers the
+upload of the request body. It is -1 when the body is drained after the
+response (?A=1). <wait> is the delay requested with ?t= and <real-wait> the one
+really spent waiting, both in microseconds.
-- 
2.56.1

From 41d094e4b214226fd8347f7811edf373304b9a25 Mon Sep 17 00:00:00 2001
From: Aleksandar Lazic <[email protected]>
Date: Thu, 3 Dec 2026 21:00:18 +0100
Subject: [PATCH 2/4] MINOR: haterm: report the real times in the X-req and
 X-rsp headers

The X-req and X-rsp headers inherited from httpterm always reported
"time=0 ms" and "time=0 ms (0 real)", marked as TODO in the code. This
makes them useless to see how long a request took to arrive or how long
haterm really waited before responding.

The dates of the stream are now set using now_ns. X-req reports the
time from the stream creation to the end of the request, which covers
the upload of the request body, or -1 when the body is drained after
the response (?A=1). X-rsp reports the delay requested with ?t=
followed by the one really spent waiting. Both are in microseconds
("us") since most responses take less than a millisecond.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Aleksandar Lazic <[email protected]>
---
 src/haterm.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 64 insertions(+), 8 deletions(-)

diff --git a/src/haterm.c b/src/haterm.c
index 26eddc1ea..78421f17f 100644
--- a/src/haterm.c
+++ b/src/haterm.c
@@ -275,6 +275,7 @@ static int hstream_htx_buf_rcv(struct connection *conn, struct hstream *hs)
 		if (!htx_expect_more(htxbuf(&hs->req))) {
 		    fin = 1;
 		    hs->flags |= HS_ST_HTTP_EOM_RCVD;
+		    hs->req_eom_ns = now_ns;
 		    break;
 		}
 
@@ -628,6 +629,7 @@ static int hstream_build_http_resp(struct hstream *hs)
 	struct htx *htx;
 	unsigned int flags = HTX_SL_F_IS_RESP | HTX_SL_F_XFER_LEN | (!(hs->flags & HS_ST_OPT_CHUNK_RES) ?  HTX_SL_F_CLEN : 0);
 	struct htx_sl *sl;
+	ullong wait_us;
 	char *end;
 
 	TRACE_ENTER(HS_EV_HSTRM_RESP, hs);
@@ -679,15 +681,35 @@ static int hstream_build_http_resp(struct hstream *hs)
 		goto err;
 	}
 
-	/* XXX TODO time?  XXX */
+	/* time from the stream creation to the end of the request, in
+	 * microseconds, or -1 if the request was not fully received yet
+	 * (e.g. its body is drained after the response with ?A=1).
+	 */
 	chunk_reset(&trash);
-	if (!chunk_strcat(&trash, "time=0 ms") ||
-	    !htx_add_header(htx, ist("X-req"), ist2(trash.area, trash.data))) {
+	if (!chunk_strcat(&trash, "time=")) {
+		TRACE_ERROR("could not build x-req HTX header", HS_EV_HSTRM_RESP, hs);
+		goto err;
+	}
+	if (hs->flags & HS_ST_HTTP_EOM_RCVD) {
+		end = ulltoa((hs->req_eom_ns - hs->accept_ns) / 1000,
+		             trash.area + trash.data, trash.size - trash.data);
+		if (!end)
+			goto err;
+		trash.data = end - trash.area;
+	}
+	else if (!chunk_strcat(&trash, "-1")) {
+		TRACE_ERROR("could not build x-req HTX header", HS_EV_HSTRM_RESP, hs);
+		goto err;
+	}
+	if (!chunk_strcat(&trash, " us")) {
+		TRACE_ERROR("could not build x-req HTX header", HS_EV_HSTRM_RESP, hs);
+		goto err;
+	}
+	if (!htx_add_header(htx, ist("X-req"), ist2(trash.area, trash.data))) {
 		TRACE_ERROR("could not add x-req HTX header", HS_EV_HSTRM_RESP, hs);
-	    goto err;
+		goto err;
 	}
 
-	/* XXX TODO time? XXX */
 	chunk_reset(&trash);
 	if (!chunk_strcat(&trash, "id=dummy, code=")) {
 		TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
@@ -714,10 +736,38 @@ static int hstream_build_http_resp(struct hstream *hs)
 		goto err;
 	trash.data = end - trash.area;
 
-	if (!chunk_strcat(&trash, ", time=0 ms (0 real)") ||
-	    !htx_add_header(htx, ist("X-rsp"), ist2(trash.area, trash.data))) {
+	/* the delay requested with ?t= followed by the one really spent
+	 * waiting, both in microseconds.
+	 */
+	if (!chunk_strcat(&trash, ", time=")) {
+		TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
+		goto err;
+	}
+	wait_us = 0;
+	if (tick_isset(hs->res_wait))
+		wait_us = (ullong)TICKS_TO_MS(hs->res_wait) * 1000;
+	end = ulltoa(wait_us, trash.area + trash.data, trash.size - trash.data);
+	if (!end)
+		goto err;
+	trash.data = end - trash.area;
+	if (!chunk_strcat(&trash, " us (")) {
+		TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
+		goto err;
+	}
+	wait_us = 0;
+	if (tick_isset(hs->res_time))
+		wait_us = (now_ns - hs->wait_ns) / 1000;
+	end = ulltoa(wait_us, trash.area + trash.data, trash.size - trash.data);
+	if (!end)
+		goto err;
+	trash.data = end - trash.area;
+	if (!chunk_strcat(&trash, " real)")) {
+		TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
+		goto err;
+	}
+	if (!htx_add_header(htx, ist("X-rsp"), ist2(trash.area, trash.data))) {
 		TRACE_ERROR("could not add x-rsp HTX header", HS_EV_HSTRM_RESP, hs);
-	    goto err;
+		goto err;
 	}
 
 	if (!htx_add_endof(htx, HTX_BLK_EOH)) {
@@ -988,6 +1038,7 @@ static struct task *process_hstream(struct task *t, void *context, unsigned int
 			/* The request body has always been fully received */
 			TRACE_STATE("no more expected data", HS_EV_HSTRM_RESP, hs);
 			hs->flags |= HS_ST_HTTP_EOM_RCVD;
+			hs->req_eom_ns = now_ns;
 		}
 
 		if (hstream_must_drain(hs)) {
@@ -1002,6 +1053,7 @@ static struct task *process_hstream(struct task *t, void *context, unsigned int
 		if (tick_isset(hs->res_wait)) {
 			TRACE_STATE("task scheduled", HS_EV_HSTRM_IO_CB, hs);
 			hs->res_time = tick_add(now_ms, hs->res_wait);
+			hs->wait_ns = now_ns;
 			task_schedule(t, hs->res_time);
 			goto leave;
 		}
@@ -1029,6 +1081,7 @@ static struct task *process_hstream(struct task *t, void *context, unsigned int
 		if (tick_isset(hs->res_wait) && !tick_isset(hs->res_time)) {
 			TRACE_STATE("task scheduled", HS_EV_HSTRM_IO_CB, hs);
 			hs->res_time = tick_add(now_ms, hs->res_wait);
+			hs->wait_ns = now_ns;
 			task_schedule(t, hs->res_time);
 			goto leave;
 		}
@@ -1148,6 +1201,9 @@ void *hstream_new(struct session *sess, struct stconn *sc, struct buffer *input)
 	hs->res_wait = TICK_ETERNITY;
 	hs->res_time = TICK_ETERNITY;
 	hs->req_meth = HTTP_METH_OTHER;
+	hs->accept_ns = now_ns;
+	hs->req_eom_ns = 0;
+	hs->wait_ns = 0;
 
 	if (sc_conn(sc)) {
 		const struct mux_ops *mux = sc_mux_ops(sc);
-- 
2.56.1

From aa3d2a0afd617f872476648610cb79a4d2c42843 Mon Sep 17 00:00:00 2001
From: Aleksandar Lazic <[email protected]>
Date: Thu, 3 Dec 2026 21:00:02 +0100
Subject: [PATCH 1/4] MINOR: haterm: add timestamps to the haterm stream

Add three dates to struct hstream: the date the stream was created,
the date the request was fully received and the date the response
delay started. They will be used to report the real times in the
X-req and X-rsp headers, which always report 0 for now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Aleksandar Lazic <[email protected]>
---
 include/haproxy/hstream-t.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/include/haproxy/hstream-t.h b/include/haproxy/hstream-t.h
index b9bdbcf54..96bd3bb57 100644
--- a/include/haproxy/hstream-t.h
+++ b/include/haproxy/hstream-t.h
@@ -26,6 +26,9 @@ struct hstream {
 	int res_wait;                /* time to wait before replying in ms */
 	int res_time;
 	enum http_meth_t req_meth;
+	ullong accept_ns;            /* date the stream was created */
+	ullong req_eom_ns;           /* date the request was fully received */
+	ullong wait_ns;              /* date the response delay started */
 };
 
 #endif /* _HAPROXY_HSTREAM_T_H */
-- 
2.56.1

Reply via email to