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

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

单线程调用测试:

[java]  view plain  copy
 print ?
  1. public static void testOne() throws HttpProcessException{  
  2.       
  3.     System.out.println("--------简单方式调用(默认post)--------");  
  4.     String url = "http://tool.oschina.net/";  
  5.     //简单调用  
  6.     String resp = HttpClientUtil.send(url);  
  7.     System.out.println("请求结果内容长度:"+ resp.length());  
  8.       
  9.     System.out.println("\n#################################\n");  
  10.       
  11.     System.out.println("--------加入header设置--------");  
  12.     url="http://blog.csdn.net/xiaoxian8023";  
  13.     //设置header信息  
  14.     Header[] headers=HttpHeader.custom().userAgent("Mozilla/5.0").build();  
  15.     //执行请求  
  16.     resp = HttpClientUtil.send(url, headers);  
  17.     System.out.println("请求结果内容长度:"+ resp.length());  
  18.   
  19.     System.out.println("\n#################################\n");  
  20.       
  21.     System.out.println("--------代理设置(绕过证书验证)-------");  
  22.     url="https://www.facebook.com/";  
  23.     HttpClient client= HCB.custom().timeout(10000).proxy("127.0.0.1"8087).ssl().build();//采用默认方式(绕过证书验证)  
  24.     //执行请求  
  25.     resp = HttpClientUtil.send(client,url);  
  26.     System.out.println("请求结果内容长度:"+ resp.length());  
  27.   
  28.     System.out.println("\n#################################\n");  
  29.   
  30.     System.out.println("--------代理设置(自签名证书验证)+header+get方式-------");  
  31.     url = "https://sso.tgb.com:8443/cas/login";  
  32.     client= HCB.custom().timeout(10000).ssl("D:\\keys\\wsriakey","tomcat").build();  
  33.     headers=HttpHeader.custom().keepAlive("false").connection("close").contentType(Headers.APP_FORM_URLENCODED).build();  
  34.     //执行请求  
  35.     resp = HttpClientUtil.send(client, url, HttpMethods.GET, headers);  
  36.     System.out.println("请求结果内容长度:"+ resp.length());  
  37.   
  38.     System.out.println("\n#################################\n");  
  39. }  
测试结果如下:


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

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

[java]  view plain  copy
 print ?
  1. public static void testMutilTask(){  
  2.     // URL列表数组  
  3.     String[] urls = {  
  4.             "http://blog.csdn.net/xiaoxian8023/article/details/49862725",  
  5.             "http://blog.csdn.net/xiaoxian8023/article/details/49834643",  
  6.             "http://blog.csdn.net/xiaoxian8023/article/details/49834615",  
  7.             "http://blog.csdn.net/xiaoxian8023/article/details/49834589",  
  8.             "http://blog.csdn.net/xiaoxian8023/article/details/49785417",  
  9.               
  10.             "http://blog.csdn.net/xiaoxian8023/article/details/48679609",  
  11.             "http://blog.csdn.net/xiaoxian8023/article/details/48681987",  
  12.             "http://blog.csdn.net/xiaoxian8023/article/details/48710653",  
  13.             "http://blog.csdn.net/xiaoxian8023/article/details/48729479",  
  14.             "http://blog.csdn.net/xiaoxian8023/article/details/48733249",  
  15.   
  16.             "http://blog.csdn.net/xiaoxian8023/article/details/48806871",  
  17.             "http://blog.csdn.net/xiaoxian8023/article/details/48826857",  
  18.             "http://blog.csdn.net/xiaoxian8023/article/details/49663643",  
  19.             "http://blog.csdn.net/xiaoxian8023/article/details/49619777",  
  20.             "http://blog.csdn.net/xiaoxian8023/article/details/47335659",  
  21.   
  22.             "http://blog.csdn.net/xiaoxian8023/article/details/47301245",  
  23.             "http://blog.csdn.net/xiaoxian8023/article/details/47057573",  
  24.             "http://blog.csdn.net/xiaoxian8023/article/details/45601347",  
  25.             "http://blog.csdn.net/xiaoxian8023/article/details/45569441",  
  26.             "http://blog.csdn.net/xiaoxian8023/article/details/43312929",   
  27.             };  
  28.       
  29.     // 设置header信息  
  30.     Header[] headers = HttpHeader.custom().userAgent("Mozilla/5.0").build();  
  31.     HttpClient client= HCB.custom().timeout(10000).build();  
  32.       
  33.      long start = System.currentTimeMillis();          
  34.         try {  
  35.             int pagecount = urls.length;  
  36.             ExecutorService executors = Executors.newFixedThreadPool(pagecount);  
  37.             CountDownLatch countDownLatch = new CountDownLatch(pagecount*100);           
  38.             for(int i = 0; i< pagecount*100;i++){  
  39.                 //启动线程抓取  
  40.                 executors.execute(new GetRunnable(urls[i%pagecount], headers, countDownLatch).setClient(client));  
  41.             }  
  42.             countDownLatch.await();  
  43.             executors.shutdown();  
  44.         } catch (InterruptedException e) {  
  45.             e.printStackTrace();  
  46.         } finally {  
  47.             System.out.println("线程" + Thread.currentThread().getName() + ", 所有线程已完成,开始进入下一步!");  
  48.         }  
  49.            
  50.         long end = System.currentTimeMillis();  
  51.         System.out.println("总耗时(毫秒): -> " + (end - start));  
  52.         //(7715+7705+7616)/3= 23 036/3= 7 678.66---150=51.2  
  53.         //(9564+8250+8038+7604+8401)/5=41 857/5=8 371.4--150  
  54.         //(9803+8244+8188+8378+8188)/5=42 801/5= 8 560.2---150  
  55. }  
  56.   
  57.  static class GetRunnable implements Runnable {  
  58.         private CountDownLatch countDownLatch;  
  59.         private String url;  
  60.         private Header[] headers;  
  61.         private HttpClient client = null;  
  62.           
  63.         public GetRunnable setClient(HttpClient client){  
  64.             this.client = client;  
  65.             return this;  
  66.         }  
  67.   
  68.         public GetRunnable(String url, Header[] headers,CountDownLatch countDownLatch){  
  69.             this.url = url;  
  70.             this.headers = headers;  
  71.             this.countDownLatch = countDownLatch;  
  72.         }  
  73.         @Override  
  74.         public void run() {  
  75.             try {  
  76.                 String response = null;  
  77.                 if(client!=null){  
  78.                     response = HttpClientUtil.send(client, url, headers);  
  79.                 }else{  
  80.                     response =  HttpClientUtil.send(url, headers);  
  81.                 }  
  82.                 System.out.println(Thread.currentThread().getName()+"--获取内容长度:"+response.length());  
  83.             } catch (HttpProcessException e) {  
  84.                 e.printStackTrace();  
  85.             } finally {  
  86.                 countDownLatch.countDown();  
  87.             }  
  88.         }  
  89.     }   

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

[java]  view plain  copy
 print ?
  1. public static void main(String[] args) throws Exception {  
  2. /       testOne();  
  3.     testMutilTask();  
  4. }  
测试结果如下:

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

好了,本工具就分享到这里,下次会分享异步的HttpClient,敬请期待。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值