使用多線程類比多使用者並發訪問一個或多個tomcat,測試效能 java__java

來源:互聯網
上載者:User
Java代碼   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) 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代碼   package test;      import java.io.BufferedReader;   import java.io.FileReader;  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.