public class ResponseTimer extends Object implements Handler
handler
, that adds a "X-Response-Time" header to all requests indicating how long it took to start sending a response in milliseconds.
It is generally most convenient to add a timer into the handler chain by using the decorator()
method, which provides a HandlerDecorator
.
import ratpack.handling.ResponseTimer;
import ratpack.test.embed.EmbeddedApp;
import ratpack.http.client.ReceivedResponse;
import static junit.framework.Assert.*;
public class Example {
public static void main(String... args) throws Exception {
EmbeddedApp.of(s -> s
.registryOf(r -> r
.add(ResponseTimer.decorator())
)
.handler(r ->
ctx -> ctx.render("ok")
)
).test(httpClient -> {
ReceivedResponse response = httpClient.get();
assertNotNull(response.getHeaders().get("X-Response-Time"));
});
}
}
See handle(Context)
for precise detail on what is timed, and the time value.
Modifier and Type | Field and Description |
---|---|
static String |
HEADER_NAME
The name of the header with the time value: "X-Response-Time".
|
Constructor and Description |
---|
ResponseTimer() |
Modifier and Type | Method and Description |
---|---|
static HandlerDecorator |
decorator()
Creates a handler decorator that prepends a response timer to the rest of the handlers.
|
void |
handle(Context ctx)
Adds the number of milliseconds of elapsed time between the invocation of this method and when the response is ready to be sent.
|
public static final String HEADER_NAME
public static HandlerDecorator decorator()
public void handle(Context ctx)
The timer is stopped, and the header added, by Response.beforeSend(ratpack.func.Action)
.
This means that the time value is the elapsed time and not “wall” time.
Similarly, it does not include the time to actually start sending data out over the socket.
It effectively times the application processing.
The value is in milliseconds, accurate to 5 decimal places.