Using multithreading to simulate multiple users concurrent access to one or more tomcat, test performance Java__java

Source: Internet
Author: User
Tags getmessage
Java code    package test;      import java.net.httpurlconnection;    import java.net.sockettimeoutexception;   import java.net.url;   Import  java.net.URLConnection;   import java.util.concurrent.countdownlatch;       import org.apache.log4j.logger;      public class callhttprequest  implements runnable {       private static logger log  = logger.getlogger (callhttprequest.class);       public   static int successrequest = 0;       public  static  int failRequest = 0;       public  static int  timeOutRequest = 0;       private final String  hoststring =  "http://localhost:";       private String port;       private String puductPartURL;           private CountDownLatch begin;       private countdownlatch  end;          callhttprequest (string port, string  puductparturl, countdownlatch begin,                countdownlatch end)  {            this.port = port;            this.puductparturl = puductparturl;            this.begin = begin;           this.end =  end;       }           private string makefullurl ()  {           return hostString + port +  puductparturl;          }       private   static synchronized void incrementsuccesscount () {             successRequest++;       }               private  static synchronized void  incrementfailcount () {            failRequest++;        }               Private static synchronized void incrementtimeoutcount () {             timeoutrequest++;       }                @Override        public void run ()  {            string urlstr = makefullurl ();            URL url;            try {                begin.await ();                Url = new url (URLSTR);                urlconnection urlconnection = url.openconnection ();                HttpURLConnection httpConnection =  ( HttpURLConnection) &NBSp urlconnection;                Httpconnection.setconnecttimeout (3000);                int responsecode = httpconnection.getresponsecode ();                if  (responsecode ==  HTTPURLCONNECTION.HTTP_OK)  {                    incrementsuccesscount ();                } else {                    incrementfailcount ();                }            } catch  (sockettimeoutexception e)  {                Incrementtimeoutcount ();                Log.error (E.getmessage (),  e);           } catch   (exception e)  {                log.error (E.getmessage (),  e);           }  finally {                End.countdown ();           }           }     }  

Java code Package test;   Import Java.io.BufferedReader; Import Java.io.FileReader;

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.