在大多数情况下,Jsoup比HttpURLConnection和HttpClient更胖的原因

问题描述 投票:0回答:1

我想比较标题中提到的三种实现的性能,我编写了一个Java程序来帮助我做到这一点。主要方法包含三个测试块,每个块如下所示:

        nb=0; time=0;
        for (int i = 0; i < 7; i++) {
            double v = methodX(url);
            if(v>0){
                nb++;
                time+=v;
            }
        }
        if(nb==0) nb=1;
        System.out.println("HttpClient : "+(time/ ((double) nb))+". Tries "+nb+"/7");

变量nb用于避免请求失败。现在方法methodX是以下之一:

    private static double testWithNativeHUC(String url){
        try {
            HttpURLConnection httpURLConnection= (HttpURLConnection) new URL(url).openConnection();
            httpURLConnection.addRequestProperty("User-Agent", UA);
            long before = System.currentTimeMillis();
            BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            while (bufferedReader.readLine()!=null);
            return System.currentTimeMillis()-before;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    private static double testWithHC(String url) {
        try {
            CloseableHttpClient httpClient = HttpClientBuilder.create().setUserAgent(UA).build();
            BasicResponseHandler basicResponseHandler = new BasicResponseHandler();
            long before = System.currentTimeMillis();
            CloseableHttpResponse response = httpClient.execute(new HttpGet(url));
            basicResponseHandler.handleResponse(response);
            return System.currentTimeMillis() - before;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    private static double testWithJsoup(String url){
        try{
            long before = System.currentTimeMillis();
            Jsoup.connect(url).execute().parse();
            return System.currentTimeMillis()-before;
        }catch (IOException e){
            e.printStackTrace();
            return -1;
        }
    }

我得到的输出如下。

用于网址https://stackoverflow.com

    HttpUrlConnection : 325.85714285714283. Tries 7/7
    HttpClient : 299.0. Tries 7/7
    Jsoup : 172.42857142857142. Tries 7/7

用于网址https://online.vfsglobal.dz

    HttpUrlConnection : 104.57142857142857. Tries 7/7
    HttpClient : 181.0. Tries 7/7
    Jsoup : 57.857142857142854. Tries 7/7

用于网址https://google.com/

    HttpUrlConnection : 251.28571428571428. Tries 7/7
    HttpClient : 259.57142857142856. Tries 7/7
    Jsoup : 299.85714285714283. Tries 7/7

用于网址https://algeria.blsspainvisa.com/book_appointment.php

    HttpUrlConnection : 112.57142857142857. Tries 7/7
    HttpClient : 194.85714285714286. Tries 7/7
    Jsoup : 67.42857142857143. Tries 7/7

用于网址https://tunisia.blsspainvisa.com/book_appointment.php

    HttpUrlConnection : 439.2857142857143. Tries 7/7
    HttpClient : 283.42857142857144. Tries 7/7
    Jsoup : 144.71428571428572. Tries 7/7

即使重复测试也得出相同的结果,我也没有在两次请求之间使用睡眠时间来获得快速结果,我相信这对结果没有太大影响。

谢谢,

java optimization jsoup httpclient httpurlconnection
1个回答
0
投票

这里是对JSoup代码的分析,它可以包含在一个简单的堆栈溢出答案中。

步骤1: JSoup.connect(url)Exact Method Body Description

方法主体:

return HttpConnection.connect(url);

第2步: HttpConnection.connect(url)Exact Method Body Description

方法主体:

Connection con = new HttpConnection();
con.url(url);
return con;

步骤3: new HttpConnection()Exact Constructor Body Description

构造函数主体:

public HttpConnection() {
    req = new Request();
    res = new Response();
}

步骤4: (HttpConnection).execute()Exact Method Body Description

方法主体:

res = Response.execute(req);
return res;

步骤5: Response.execute(req)Complete Class Here

由于'parse'的性质是您在前面提供的其他两个示例所不包括的额外工作,因此and since] JSoup似乎在计算出的度量标准上是胜利的,我将省略解析说明和源代码(可能很长)。

无论如何,通用的HTML下载和解析例程必须处理几种不同的情况,因此在JSoup情况下的代码量似乎远远超过HTTPClient和HTTPURLConnection所需要的。

也许这个答案会解释一些事情。 尝试仔细阅读JSoup的整个“请求”类代码。

© www.soinside.com 2019 - 2024. All rights reserved.