Java 模拟并发操作压力测试代码

  1. import java.io.BufferedReader;   
  2.   import java.io.File;   
  3.   import java.io.FileInputStream;   
  4.   import java.io.InputStreamReader;   
  5.   import java.io.PrintWriter;   
  6.   import java.net.HttpURLConnection;   
  7.   import java.net.URL;   
  8.   import java.util.HashMap;   
  9.   import java.util.Map;   
  10.   import java.util.concurrent.ExecutorService;   
  11.   import java.util.concurrent.Executors;   
  12.   import java.util.concurrent.Semaphore;   
  13.   public class ConcurrentTest {   
  14.   private static int thread_num = 200;   
  15.   private static int client_num = 460;   
  16.   private static Map keywordMap = new HashMap();   
  17.   static {   
  18.   try {   
  19.   InputStreamReader isr = new InputStreamReader(new FileInputStream(   
  20.   new File("clicks.txt")), "GBK");   
  21.   BufferedReader buffer = new BufferedReader(isr);   
  22.   String line = "";   
  23.   while ((line = buffer.readLine()) != null) {   
  24.   keywordMap.put(line.substring(0, line.lastIndexOf(":")), "");   
  25.   }   
  26.   } catch (Exception e) {   
  27.   e.printStackTrace();   
  28.   }   
  29.   }   
  30.   public static void main(String[] args) {   
  31.   int size = keywordMap.size();   
  32.   // TODO Auto-generated method stub   
  33.   ExecutorService exec = Executors.newCachedThreadPool();   
  34.   // 50个线程可以同时访问   
  35.   final Semaphore semp = new Semaphore(thread_num);   
  36.   // 模拟2000个客户端访问   
  37.   for (int index = 0; index < client_num; index++) {   
  38.   final int NO = index;   
  39.   Runnable run = new Runnable() {   
  40.   public void run() {   
  41.   try {   
  42.   // 获取许可  
  43. semp.acquire();   
  44.   System.out.println("Thread:" + NO);   
  45.   String host = "http://10.99.23.42:7001/KMQueryCenter/query.do?";   
  46.   String para = "method=getQueryResult&pageNum=1&pageSize=5&"   
  47.   + "queryKeyWord="   
  48.   + getRandomSearchKey(NO)   
  49.   + "&questionID=-1&questionIdPath=-1&searchType=1"   
  50.   + "&proLine=&proSeries=&proType=" + NO;   
  51.   System.out.println(host + para);   
  52.   URL url = new URL(host);// 此处填写供测试的url   
  53.   HttpURLConnection connection = (HttpURLConnection) url   
  54.   .openConnection();   
  55.   // connection.setRequestMethod("POST");   
  56.   // connection.setRequestProperty("Proxy-Connection",   
  57.   // "Keep-Alive");   
  58.   connection.setDoOutput(true);   
  59.   connection.setDoInput(true);   
  60.   PrintWriter out = new PrintWriter(connection   
  61.   .getOutputStream());   
  62.   out.print(para);   
  63.   out.flush();   
  64.   out.close();   
  65.   BufferedReader in = new BufferedReader(   
  66.   new InputStreamReader(connection   
  67.   .getInputStream()));   
  68.   String line = "";   
  69.   String result = "";   
  70.   while ((line = in.readLine()) != null) {   
  71.   result += line;   
  72.   }   
  73.   // System.out.println(result);   
  74.   // Thread.sleep((long) (Math.random()) * 1000);   
  75.   // 释放   
  76.   System.out.println("第:" + NO + " 个");   
  77.   semp.release();   
  78.   } catch (Exception e) {   
  79.   e.printStackTrace();   
  80.   }   
  81.   }   
  82.   };   
  83.   exec.execute(run);   
  84.   }   
  85.   // 退出线程池   
  86.   exec.shutdown();   
  87.   }   
  88.   private static String getRandomSearchKey(final int no) {   
  89.   String ret = "";   
  90.   int size = keywordMap.size();   
  91.   // int wanna = (int) (Math.random()) * (size - 1);   
  92.   ret = (keywordMap.entrySet().toArray())[no].toString();   
  93.   ret = ret.substring(0, ret.lastIndexOf("="));   
  94.   System.out.println("\t" + ret);   
  95.   return ret;   
  96.   }   
  97.   }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值