thinking

          one platform thousands thinking

          java模擬并發(fā)操作進(jìn)行壓力測試

          http://www.qqread.com/java/2010/01/c488170.html

          import java.io.BufferedReader;

          import java.io.File;

          import java.io.FileInputStream;

          import java.io.InputStreamReader;

          import java.io.PrintWriter;

          import java.net.HttpURLConnection;

          import java.net.URL;

          import java.util.HashMap;

          import java.util.Map;

          import java.util.concurrent.ExecutorService;

          import java.util.concurrent.Executors;

          import java.util.concurrent.Semaphore;

          public class ConcurrentTest {

          private static int thread_num = 200;

          private static int client_num = 460;

          private static Map keywordMap = new HashMap();

          static {

          try {

          InputStreamReader isr 
          = new InputStreamReader(new FileInputStream(

          new File("clicks.txt")), "GBK");

          BufferedReader buffer 
          = new BufferedReader(isr);

          String line 
          = "";

          while ((line = buffer.readLine()) != null) {

          keywordMap.put(line.substring(
          0, line.lastIndexOf(":")), "");

          }

          catch (Exception e) {

          e.printStackTrace();

          }

          }

          public static void main(String[] args) {

          int size = keywordMap.size();

          // TODO Auto-generated method stub

          ExecutorService exec 
          = Executors.newCachedThreadPool();

          // 50個線程可以同時訪問

          final Semaphore semp = new Semaphore(thread_num);

          // 模擬2000個客戶端訪問

          for (int index = 0; index < client_num; index++) {

          final int NO = index;

          Runnable run 
          = new Runnable() {

          public void run() {

          try {

          // 獲取許可

          semp.acquire();

          System.out.println(
          "Thread:" + NO);

          String host 
          = "http://10.99.23.42:7001/KMQueryCenter/query.do?";

          String para 
          = "method=getQueryResult&pageNum=1&pageSize=5&"

          + "queryKeyWord="

          + getRandomSearchKey(NO)

          + "&questionID=-1&questionIdPath=-1&searchType=1"

          + "&proLine=&proSeries=&proType=" + NO;

          System.out.println(host 
          + para);

          URL url 
          = new URL(host);// 此處填寫供測試的url

          HttpURLConnection connection 
          = (HttpURLConnection) url

          .openConnection();

          // connection.setRequestMethod("POST");

          // connection.setRequestProperty("Proxy-Connection",

          // "Keep-Alive");

          connection.setDoOutput(
          true);

          connection.setDoInput(
          true);

          PrintWriter out 
          = new PrintWriter(connection

          .getOutputStream());

          out.print(para);

          out.flush();

          out.close();

          BufferedReader in 
          = new BufferedReader(

          new InputStreamReader(connection

          .getInputStream()));

          String line 
          = "";

          String result 
          = "";

          while ((line = in.readLine()) != null) {

          result 
          += line;

          }

          // System.out.println(result);

          // Thread.sleep((long) (Math.random()) * 1000);

          // 釋放

          System.out.println(
          "第:" + NO + " 個");

          semp.release();

          catch (Exception e) {

          e.printStackTrace();

          }

          }

          };

          exec.execute(run);

          }

          // 退出線程池

          exec.shutdown();

          }

          private static String getRandomSearchKey(final int no) {

          String ret 
          = "";

          int size = keywordMap.size();

          // int wanna = (int) (Math.random()) * (size - 1);

          ret 
          = (keywordMap.entrySet().toArray())[no].toString();

          ret 
          = ret.substring(0, ret.lastIndexOf("="));

          System.out.println(
          "\t" + ret);

          return ret;

          }

          }

          如果本文對您有幫助并且要鼓勵我的話,請掃描如下二維碼支持本人的勞動成果,多謝了!

          posted on 2010-05-28 11:06 lau 閱讀(10183) 評論(1)  編輯  收藏 所屬分類: Unit Test

          Feedback

          # re: java模擬并發(fā)操作進(jìn)行壓力測試 2012-10-18 10:20 東笑言

          非常不錯,十分感謝你共享的代碼。  回復(fù)  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 洮南市| 任丘市| 阳信县| 定日县| 巩留县| 游戏| 正安县| 平和县| 枣阳市| 苍南县| 巨野县| 济阳县| 澎湖县| 伊金霍洛旗| 叙永县| 广河县| 双江| 黄骅市| 青川县| 吴桥县| 泸溪县| 乐平市| 宿迁市| 淮滨县| 太保市| 万山特区| 青州市| 通城县| 澄迈县| 双柏县| 平和县| 龙口市| 扶风县| 锦屏县| 石河子市| 富蕴县| 金阳县| 巴南区| 文成县| 克什克腾旗| 英吉沙县|