简体   繁体   中英

How to get the time it took to connect on a request basis using Jetty Client

I want to compute per request the time between the begining of request and time connection is established (TCP connection establishment).

I asked this question question but it was ambiguous.

I had a look at proposed answer, maybe I misunderstood, but I understand that Connection.Listener works on a global basis at least (Overall connect time)

So I tried the approach based on SocketAddressResolver:

    private static class CustomSocketAddressResolver implements SocketAddressResolver {
        private StopWatch stopWatch = new StopWatch();
        
        private SocketAddressResolver adaptee;
        
        public CustomSocketAddressResolver(SocketAddressResolver adaptee) {
            this.adaptee = adaptee;
        }
        
        public long getConnectTime() {
            return stopWatch.getTime();
        }


        @Override
        public void resolve(String host, int port, Promise<List<InetSocketAddress>> promise) {
            stopWatch.reset();
            stopWatch.start();          
            adaptee.resolve(host, port, new Promise<List<InetSocketAddress>>() {

                @Override
                public void succeeded(List<InetSocketAddress> result) {
                    // Add as first address an invalid address so that we test
                    // that the connect operation iterates over the addresses.
                    stopWatch.stop();
                    promise.succeeded(result);
                }

                @Override
                public void failed(Throwable x) {
                    stopWatch.stop();
                    promise.failed(x);
                }
            });
        }
    }

Then I can get connect time like this:

 ((CustomSocketAddressResolver) httpClient.getSocketAddressResolver()).getConnectTime()

It works but is there a better way?

I want to compute per request the time between the begining of request and time connection is established (TCP connection establishment).

Perhaps there is a misunderstanding here.

HTTP/1.1 and HTTP/2 use persistent TCP connections. A TCP connection is created, kept open, and reused for many requests.

For example, a TCP connection is created and then reused for, say, 1067 requests, and then still kept open.

It would not make much sense to compute the time between the beginning of the 1067th request and the time the connection was first established (or I would love to hear a use case for that).

I know of case where connections remain open for days.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM