轻松把玩HttpClient之封装HttpClient工具类(四),单线程调用及多线程批量调用测试

本文介绍了一款HTTP客户端工具的测试过程及结果,包括单线程和多线程调用测试,展示了工具在不同场景下的稳定性和性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载地址:http://blog.youkuaiyun.com/xiaoxian8023/article/details/49910885

本文主要来分享一下该工具类的测试结果。工具类的整体源码不再单独分享,源码基本上都已经在文章中了。开始我们的测试。

单线程调用测试:


    public static void testOne() throws HttpProcessException{  
          
        System.out.println("--------简单方式调用(默认post)--------");  
        String url = "http://tool.oschina.net/";  
        //简单调用  
        String resp = HttpClientUtil.send(url);  
        System.out.println("请求结果内容长度:"+ resp.length());  
          
        System.out.println("\n#################################\n");  
          
        System.out.println("--------加入header设置--------");  
        url="http://blog.youkuaiyun.com/xiaoxian8023";  
        //设置header信息  
        Header[] headers=HttpHeader.custom().userAgent("Mozilla/5.0").build();  
        //执行请求  
        resp = HttpClientUtil.send(url, headers);  
        System.out.println("请求结果内容长度:"+ resp.length());  
      
        System.out.println("\n#################################\n");  
          
        System.out.println("--------代理设置(绕过证书验证)-------");  
        url="https://www.facebook.com/";  
        HttpClient client= HCB.custom().timeout(10000).proxy("127.0.0.1", 8087).ssl().build();//采用默认方式(绕过证书验证)  
        //执行请求  
        resp = HttpClientUtil.send(client,url);  
        System.out.println("请求结果内容长度:"+ resp.length());  
      
        System.out.println("\n#################################\n");  
      
        System.out.println("--------代理设置(自签名证书验证)+header+get方式-------");  
        url = "https://sso.tgb.com:8443/cas/login";  
        client= HCB.custom().timeout(10000).ssl("D:\\keys\\wsriakey","tomcat").build();  
        headers=HttpHeader.custom().keepAlive("false").connection("close").contentType(Headers.APP_FORM_URLENCODED).build();  
        //执行请求  
        resp = HttpClientUtil.send(client, url, HttpMethods.GET, headers);  
        System.out.println("请求结果内容长度:"+ resp.length());  
      
        System.out.println("\n#################################\n");  
    }  

测试结果如下:


可以看到4次调用,都没有问题。

那么现在试试多线程调用吧。我定义一个数组,里面有20篇文章的地址。我启动20个线程的线程池来测试,写了一个20*50次的for循环,看看全部线程结束时有没有报错能用多长时间

    public static void testMutilTask(){  
        // URL列表数组  
        String[] urls = {  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/49862725",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/49834643",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/49834615",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/49834589",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/49785417",  
                  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/48679609",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/48681987",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/48710653",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/48729479",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/48733249",  
      
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/48806871",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/48826857",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/49663643",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/49619777",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/47335659",  
      
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/47301245",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/47057573",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/45601347",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/45569441",  
                "http://blog.youkuaiyun.com/xiaoxian8023/article/details/43312929",   
                };  
          
        // 设置header信息  
        Header[] headers = HttpHeader.custom().userAgent("Mozilla/5.0").build();  
        HttpClient client= HCB.custom().timeout(10000).build();  
          
         long start = System.currentTimeMillis();          
            try {  
                int pagecount = urls.length;  
                ExecutorService executors = Executors.newFixedThreadPool(pagecount);  
                CountDownLatch countDownLatch = new CountDownLatch(pagecount*100);           
                for(int i = 0; i< pagecount*100;i++){  
                    //启动线程抓取  
                    executors.execute(new GetRunnable(urls[i%pagecount], headers, countDownLatch).setClient(client));  
                }  
                countDownLatch.await();  
                executors.shutdown();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            } finally {  
                System.out.println("线程" + Thread.currentThread().getName() + ", 所有线程已完成,开始进入下一步!");  
            }  
               
            long end = System.currentTimeMillis();  
            System.out.println("总耗时(毫秒): -> " + (end - start));  
            //(7715+7705+7616)/3= 23 036/3= 7 678.66---150=51.2  
            //(9564+8250+8038+7604+8401)/5=41 857/5=8 371.4--150  
            //(9803+8244+8188+8378+8188)/5=42 801/5= 8 560.2---150  
    }  
      
     static class GetRunnable implements Runnable {  
            private CountDownLatch countDownLatch;  
            private String url;  
            private Header[] headers;  
            private HttpClient client = null;  
              
            public GetRunnable setClient(HttpClient client){  
                this.client = client;  
                return this;  
            }  
      
            public GetRunnable(String url, Header[] headers,CountDownLatch countDownLatch){  
                this.url = url;  
                this.headers = headers;  
                this.countDownLatch = countDownLatch;  
            }  
            @Override  
            public void run() {  
                try {  
                    String response = null;  
                    if(client!=null){  
                        response = HttpClientUtil.send(client, url, headers);  
                    }else{  
                        response =  HttpClientUtil.send(url, headers);  
                    }  
                    System.out.println(Thread.currentThread().getName()+"--获取内容长度:"+response.length());  
                } catch (HttpProcessException e) {  
                    e.printStackTrace();  
                } finally {  
                    countDownLatch.countDown();  
                }  
            }  
        }   

定义了一个ExecutorService的线程池,使用CountDownLatch来保证所有线程都运行完毕,测试一下看看:


    public static void main(String[] args) throws Exception {  
    /       testOne();  
        testMutilTask();  
    }  

测试结果如下:


从结果中可以清楚的看到执行1000次调用,总消耗是51165,平均51ms/个,速度快,而且没有报错。

好了,本工具就分享到这里,下次会分享异步的HttpClient,敬请期待。
代码已上传https://github.com/Arronlong/httpclientUtil



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值