<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    thinking

    one platform thousands thinking

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

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

    Feedback

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

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


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 成人毛片18岁女人毛片免费看| 久久99热精品免费观看牛牛| 四虎影院免费视频| 亚洲最大成人网色香蕉| av免费不卡国产观看| 亚洲一区二区三区高清视频| 免费人成网站在线观看10分钟| 亚洲国产成人精品无码区在线秒播 | www亚洲精品久久久乳| 国产一区二区三区无码免费| 色妞www精品视频免费看| 国产精品麻豆免费版| 一级特黄色毛片免费看| 亚洲日产韩国一二三四区| 免费视频一区二区| 亚洲熟妇无码爱v在线观看| 日韩精品无码区免费专区| 亚洲精品无码av片| 精品国产日韩亚洲一区| 国产精品偷伦视频观看免费| 亚洲色欲或者高潮影院| 午夜免费福利在线| 国产精品无码免费专区午夜| 亚洲精选在线观看| 性一交一乱一视频免费看| 免费看又黄又爽又猛的视频软件 | 久久久久亚洲AV无码专区体验| 四虎精品视频在线永久免费观看| 最新亚洲精品国偷自产在线| 免费夜色污私人影院在线观看| a级毛片免费观看视频| 亚洲欧洲在线播放| 免费人成在线观看播放国产| 99久久免费国产特黄| 亚洲成a人片在线观看中文!!!| 国产中文字幕免费观看| 免费国产成人α片| 亚洲GV天堂无码男同在线观看| 久久久亚洲精品蜜桃臀| 免费中文熟妇在线影片 | 亚洲AV综合色区无码二区爱AV|