Easy to play with HttpClient package HttpClient tool Class (iv), single-threaded calls and multi-threaded bulk call testing

Source: Internet
Author: User

This article mainly to share the test results of the tool class. The whole source code of the tool class is no longer shared separately, the source code is basically already in the article. Start our test.

Single-Threaded Call testing:

public static void Testone () throws Httpprocessexception{system.out.println ("--------Simple call (default post)--------"); String url = "http://tool.oschina.net/";//Simple call to String resp = httpclientutil.send (URL); SYSTEM.OUT.PRINTLN ("Request result content Length:" + resp.length ()); System.out.println ("\n#################################\n"); SYSTEM.OUT.PRINTLN ("--------Join header Settings--------"); Url= "http://blog.csdn.net/xiaoxian8023";//Set header information header[] Headers=httpheader.custom (). useragent ("mozilla/5.0"). Build ();//execute Request RESP = httpclientutil.send (URL, headers); SYSTEM.OUT.PRINTLN ("Request result content Length:" + resp.length ()); System.out.println ("\n#################################\n"); SYSTEM.OUT.PRINTLN ("--------Proxy settings (Bypass certificate validation)-------"); Url= "https://www.facebook.com/"; HttpClient client= hcb.custom (). Timeout (10000). Proxy ("127.0.0.1", 8087). SSL (). build ();//Use Default (Bypass certificate authentication)//execute Request RESP = Httpclientutil.send (Client,url); SYSTEM.OUT.PRINTLN ("Request result content Length:" + resp.length ()); System.out.println ("\n#################################\n"); SYSTEM.OUT.PRINTLN ("--------proxy settings (self-signed certificate+header+get method-------"), 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 ();//execute Request RESP = httpclientutil.send (client, URL, Httpmethods.get, headers); SYSTEM.OUT.PRINTLN ("Request result content Length:" + resp.length ()); System.out.println ("\n#################################\n");}
The test results are as follows:


Can see 4 calls, no problem.

Then try multithreaded calls now. I define an array with the address of 20 articles. I started the thread pool test for 20 threads, wrote a For loop for 20*50 times, and looked at the end of all threads with no error and How long it would take :

public static void Testmutiltask () {//URL list array string[] urls = {"http://blog.csdn.net/xiaoxian8023/article/details/ 49862725 "," http://blog.csdn.net/xiaoxian8023/article/details/49834643 "," http://blog.csdn.net/xiaoxian8023/ article/details/49834615 "," http://blog.csdn.net/xiaoxian8023/article/details/49834589 "," http://blog.csdn.net/ xiaoxian8023/article/details/49785417 "," http://blog.csdn.net/xiaoxian8023/article/details/48679609 "," http.// blog.csdn.net/xiaoxian8023/article/details/48681987 "," http://blog.csdn.net/xiaoxian8023/article/details/ 48710653 "," http://blog.csdn.net/xiaoxian8023/article/details/48729479 "," http://blog.csdn.net/xiaoxian8023/ article/details/48733249 "," http://blog.csdn.net/xiaoxian8023/article/details/48806871 "," http://blog.csdn.net/ xiaoxian8023/article/details/48826857 "," http://blog.csdn.net/xiaoxian8023/article/details/49663643 "," http.// Blog.csdn.net/xiaoxian8023/article/details/49619777 "," http://blog.csdn.net/xiaoxian8023/article/details/ 47335659 "," http://blog.csdn.net/xiaoxian8023/article/details/47301245 "," http://blog.csdn.net/xiaoxian8023/article/details/47057573 "," http.// blog.csdn.net/xiaoxian8023/article/details/45601347 "," http://blog.csdn.net/xiaoxian8023/article/details/ 45569441 "," http://blog.csdn.net/xiaoxian8023/article/details/43312929 ",};//set header information 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++) {//Start thread crawl Executors.execute (new getrunnable (urls[i%p            Agecount], headers, countdownlatch). Setclient (client));            } countdownlatch.await ();        Executors.shutdown (); } catch (Interruptedexception e) {e.printstacktrace (); } finally {System.out.println ("thread" + thread.currentthread (). GetName () + ", all threads are complete, start entering next!        ");        } Long end = System.currenttimemillis ();        SYSTEM.OUT.PRINTLN ("Total time elapsed (ms):--" + (End-start)); (7715+7705+7616)/3= 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---) Static class Getrunnable implements Runnable {private countdo        Wnlatch 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;        } @Overridepublic 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 () + "--Get content Length:" +response.length ());            } catch (Httpprocessexception e) {e.printstacktrace ();} finally {Countdownlatch.countdown ();  }        }    }

Define a Executorservice thread pool, use Countdownlatch to ensure that all threads are running, and test to see:

public static void Main (string[] args) throws Exception {//testone (); Testmutiltask ();}
The test results are as follows:

from the results can be clearly seen to execute 1000 calls, the total consumption is 51165, the average 51ms/, fast, and no error.

Well, this tool will share here, next time will share the asynchronous httpclient, please look forward to.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If you want to reprint please declare: "Transfer from http://blog.csdn.net/xiaoxian8023"

Easy to play with HttpClient package HttpClient tool Class (iv), single-threaded calls and multi-threaded bulk call testing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.