Class ResponseTimer

  • All Implemented Interfaces:

    public class ResponseTimer
    extends Object
    implements Handler
    A 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.core.handling.ResponseTimer;
     import ratpack.test.embed.EmbeddedApp;
     import ratpack.core.http.client.ReceivedResponse;
     import static org.junit.jupiter.api.Assertions.*;
     public class Example {
       public static void main(String... args) throws Exception {
         EmbeddedApp.of(s -> s
           .registryOf(r -> r
           .handler(r ->
             ctx -> ctx.render("ok")
         ).test(httpClient -> {
           ReceivedResponse response = httpClient.get();

    See handle(Context) for precise detail on what is timed, and the time value.

    • Field Detail


        public static final String HEADER_NAME
        The name of the header with the time value: "X-Response-Time".
        See Also:
        Constant Field Values
    • Constructor Detail

      • ResponseTimer

        public ResponseTimer()
    • Method Detail

      • decorator

        public static HandlerDecorator decorator()
        Creates a handler decorator that prepends a response timer to the rest of the handlers.
        a handler decorator.
      • handle

        public void handle​(Context ctx)
        Adds the number of milliseconds of elapsed time between Request.getTimestamp() and when the response is ready to be sent.

        The timer is stopped, and the header added, by Response.beforeSend(Action). This means that the time value is the elapsed time, commonly referred to as wall clock time, and not CPU 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.

        Specified by:
        handle in interface Handler
        ctx - the handling context.