thinking

          one platform thousands thinking

          java模擬并發操作進行壓力測試

          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 閱讀(10192) 評論(1)  編輯  收藏 所屬分類: Unit Test

          Feedback

          # re: java模擬并發操作進行壓力測試 2012-10-18 10:20 東笑言

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

          主站蜘蛛池模板: 尤溪县| 文化| 连平县| 泰兴市| 绩溪县| 迁安市| 德清县| 平塘县| 黔江区| 罗甸县| 衡东县| 汕头市| 瑞金市| 定陶县| 晋江市| 双城市| 台北市| 隆德县| 堆龙德庆县| 通山县| 常山县| 石楼县| 忻城县| 大连市| 噶尔县| 新泰市| 鲜城| 凤阳县| 罗甸县| 响水县| 合江县| 微博| 浦城县| 双峰县| 怀来县| 牙克石市| 德化县| 孙吴县| 宝山区| 沛县| 南通市|