Java ApacheHttpClient:测量响应时间的正确方法

Java ApacheHttpClient:测量响应时间的正确方法,java,apache-httpclient-4.x,apache-commons-httpclient,Java,Apache Httpclient 4.x,Apache Commons Httpclient,我试图使用ApacheHttpClient来测量服务器响应时间。我已经在下面设置了测试代码。我假设tp3显示(发送到接收)时间。在以下试验中,tp3=198.13ms。根据谷歌Chrome,服务器响应时间为17ms(快181.13ms) 我错过什么了吗 使用ApacheHttpClient测量服务器响应时间的最佳方法是什么 public static void main(String[] args) throws Exception { long tp0 = Sys

我试图使用ApacheHttpClient来测量服务器响应时间。我已经在下面设置了测试代码。我假设tp3显示(发送到接收)时间。在以下试验中,tp3=198.13ms。根据谷歌Chrome,服务器响应时间为17ms(快181.13ms)

我错过什么了吗

使用ApacheHttpClient测量服务器响应时间的最佳方法是什么


     public static void main(String[] args) throws Exception {

        long tp0 = System.nanoTime();

        CloseableHttpClient httpclient = HttpClients.createDefault();
        long tp1 = System.nanoTime() - tp0;

        HttpGet httpGet = new HttpGet("http://example.com");
        long tp2 = System.nanoTime() - tp0;

        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        long tp3 = System.nanoTime() - tp0;

        long tp4 = 0;
        long tp5 = 0;
        long tp6 = 0;
        long tp7 = 0;
        StatusLine resp;

        try {
            resp = response1.getStatusLine();
            tp4 = System.nanoTime() - tp0;

            HttpEntity entity1 = response1.getEntity();
            tp5 = System.nanoTime() - tp0;

            EntityUtils.consume(entity1);
            tp6 = System.nanoTime() - tp0;

        } finally {
            response1.close();
            tp7 = System.nanoTime() - tp0;
        }

        System.out.println("tp1 = " + fmt(tp1)); //tp1 = 611.26ms
        System.out.println("tp2 = " + fmt(tp2-tp1)); //tp2 = 3.16ms
        System.out.println("tp3 = " + fmt(tp3-tp2)); //tp3 = 198.13ms
        System.out.println("tp4 = " + fmt(tp4-tp3)); //tp4 = .01ms
        System.out.println("tp5 = " + fmt(tp5-tp4)); //tp5 = .00ms
        System.out.println("tp6 = " + fmt(tp6-tp5)); //tp6 = .91ms
        System.out.println("tp7 = " + fmt(tp7-tp6)); //tp7 = .01ms

        System.out.println("Response = " + resp);

    }

    private static String fmt(double val){
        val = val / 1e6;
        DecimalFormat formatter = new DecimalFormat("#,###,###,###.00");
        return formatter.format(val) + "ms";
    }


方法论与任何其他代码实践相同。如果您对微观基准测试感兴趣,可以使用该库。无论如何,如果您需要测量平均响应时间,请使用LoadRunner或JMeter等特殊负载测试工具,停止重新发明wheal。