zoukankan      html  css  js  c++  java
  • java并发压力测试

    package com.test;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.io.UnsupportedEncodingException;
    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 Test {
     private static int thread_num=150;
     private static int client_num=500;
     private static Map keywordMap=new HashMap();
     
     static {
      InputStreamReader isr;
      try {
       isr = new InputStreamReader(new FileInputStream(new File("D:/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) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      
     }

     public static void main(String[] args) {
      int size=keywordMap.size();
      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);
         //System.out.println("semp:" + semp);
         String host = "http://192.168.30.31:8088/AndroidWebService";
         
         //System.out.println(host + para);
         System.out.println("host--"+host);
         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.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--"+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("ret--"+" " + ret);
      return ret;
     }

    }

     

     

    http://www.blogjava.net/alinglau36/archive/2010/05/28/322118.html

     

  • 相关阅读:
    spring配置文件中util:properties和context:property-placeholder
    为啥Spring和Spring MVC包扫描要分开?
    spring和springmvc父子容器关系
    springmvc请求参数获取的几种方法
    请求转发(Forward)和重定向(Redirect)的区别
    如何设计一个秒杀系统
    Maven使用之packing篇
    ECharts基本设置
    @RequestParam详解以及加与不加的区别
    从网页提取的关键字
  • 原文地址:https://www.cnblogs.com/liuzhuqing/p/7480460.html
Copyright © 2011-2022 走看看