OkHttp中使用EventListener统计请求详细耗时遇到的问题

背景

在项目中,我们会抽样统计接口请求详细的耗时信息,比如dns耗时,连接耗时,https握手耗时等等,通常我们都是使用okhttp的EventListener来统计,但是结果出来后我们发现,有小部分连接耗时都大于(response结束 - request开始)的时间。

问题

该问题主要是因为我们项目中接口请求的OkHttpClient是单例的,在使用EventListener时,这一个EventListener会handle所有的请求,如果请求并发的话,其中的数据就会错乱。查看okhttp javadoc可以知道,确实是有问题的。


okhttpclient

在看doc文档的时候发现eventListenerFactory方法感觉就是我们想要的, 每个请求通过factory创建一个新的EventListen。验证后确实没问题,不过有个问题是EventListener是请求过程中回调的方式,如果埋点发送不想在EventListener里面发的话,做起来还是有点困难。不过在阅读文档的时候发现,可以通过okhttp3.Request.Builder#tag(java.lang.Class, T)解决。相关代码如下:
1、定义一个需要统计结果的model类,

public class NetEventModel {

    public long fetch_duration; //请求发出到拿到数据,不包括本地排队时间
    public long dns_duration; //dns解析时间
    public long connect_duration; // 创建socket通道时间
    public long secure_duration; // ssl握手时间,connect_duration包含secure_duration
    public long request_duration; // writeBytes的时间
    public long response_duration; // readBytes的时间
    public long serve_duration; // 相当于responseStartDate - requestEndDate

}

2、创建Request的时候通过request builder把这个model通过tag设置进去。

        Request.Builder builder = new Request.Builder();
        builder.headers(Headers.of(headers));
        builder.url(url);
        if (model != null)
            builder.tag(NetEventModel.class, model);
        request = builder.build();

3、创建EventListenerFactory, 通过call对象拿到request从而拿到第二部设进去的model。

class PicEventListenerFactory implements EventListener.Factory {
    @Override
    public EventListener create(Call call) {
        NetEventModel tag = call.request().tag(NetEventModel.class);
        return tag != null ? new NetEventListener(tag) : EventListener.NONE;
    }
}

4、具体负责统计的NetEventListener

public class NetEventListener extends EventListener {
    private long callStart;
    //建立连接
    private long dnsStart;
    private long connectStart;
    private long secureConnectStart;

    //连接已经建立
    private long requestStart;
    private long responseStart;

    public long MAX_TIME = 30000;

    private NetEventModel model;

    public NetEventListener(NetEventModel model) {
        this.model = model;
    }

    public NetEventModel getModel() {
        return model;
    }

    @Override
    public void callStart(Call call) {
        callStart = System.currentTimeMillis();
    }

    @Override
    public void dnsStart(Call call, String domainName) {
        dnsStart = System.currentTimeMillis();
    }

    @Override
    public void dnsEnd(Call call, String domainName, List inetAddressList) {
        model.dns_duration = System.currentTimeMillis() - dnsStart;

        if (model.dns_duration >= MAX_TIME) {
            model.dns_duration = 0;
        }
    }

    @Override
    public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) {
        connectStart = System.currentTimeMillis();
    }

    @Override
    public void secureConnectStart(Call call) {
        secureConnectStart = System.currentTimeMillis();
    }

    @Override
    public void secureConnectEnd(Call call, Handshake handshake) {
        model.secure_duration = System.currentTimeMillis() - secureConnectStart;

        if (model.secure_duration >= MAX_TIME) {
            model.secure_duration = 0;
        }
    }

    @Override
    public void connectEnd(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol) {
        model.connect_duration = System.currentTimeMillis() - connectStart;

        if (model.connect_duration >= MAX_TIME) {
            model.connect_duration = 0;
        }

        //因为connectionAcquired可能会有多次,那么请求从此处开始计时
        requestStart =  System.currentTimeMillis();
        model.request_duration = 0;
    }

    @Override
    public void connectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol, IOException ioe) {
        model.connect_duration = System.currentTimeMillis() - connectStart;

        if (model.connect_duration >= MAX_TIME) {
            model.connect_duration = 0;
        }
    }

    @Override
    public void requestHeadersEnd(Call call, Request request) {
        model.request_duration = System.currentTimeMillis() - requestStart;

        if (model.request_duration >= MAX_TIME) {
            model.request_duration = 0;
        }
    }

    @Override
    public void requestBodyEnd(Call call, long byteCount) {
        model.request_duration = System.currentTimeMillis() - requestStart;

        if (model.request_duration >= MAX_TIME) {
            model.request_duration = 0;
        }
    }

    @Override
    public void responseHeadersStart(Call call) {
        responseStart = System.currentTimeMillis();
        model.response_duration = 0;
    }

    @Override
    public void responseHeadersEnd(Call call, Response response) {
        model.response_duration = System.currentTimeMillis() - responseStart;

        if (model.response_duration >= MAX_TIME) {
            model.response_duration = 0;
        }
    }

    @Override
    public void responseBodyStart(Call call) {
        if (responseStart == 0) {
            responseStart = System.currentTimeMillis();
        }
    }

    @Override
    public void responseBodyEnd(Call call, long byteCount) {
        model.response_duration = System.currentTimeMillis() - responseStart;

        if (model.response_duration >= MAX_TIME) {
            model.response_duration = 0;
        }

        model.serve_duration = responseStart - (requestStart + model.request_duration);

        if (model.serve_duration >= MAX_TIME) {
            model.serve_duration = 0;
        }
    }

    @Override
    public void callEnd(Call call) {
        model.fetch_duration = System.currentTimeMillis() - callStart;

        if (model.fetch_duration >= MAX_TIME) {
            model.fetch_duration = 0;
        }
    }

    @Override
    public void callFailed(Call call, IOException ioe) {
        model.fetch_duration = System.currentTimeMillis() - callStart;

        if (model.fetch_duration >= MAX_TIME) {
            model.fetch_duration = 0;
        }
    }
}

这样请求结束后,就可以通过NetEventModel对象拿到本次请求详细的耗时数据。

你可能感兴趣的:(OkHttp中使用EventListener统计请求详细耗时遇到的问题)