2013-01-09 47 views
0

我正試圖編寫一個壓力測試程序來打擊我們的Web服務器。我正在嘗試使用線程模擬多個用戶訪問服務器。但是,我遇到的問題是99.9%的時間使用1個線程工作。 2個線程似乎在10-20%的時間內工作,而且更多似乎總是失敗。如何正確編寫多線程測試程序

這裏是我的代碼運行的片斷,試圖生成測試線程:

public static final MAX = 1; // (or 2, or 100 while testing) 
    <snip> 
    <snip> 

    String newParams = "{ bunch, of, test, parameters }"; 
    Callable<String> call1; 
    ExecutorService pool = Executors.newFixedThreadPool(10); 
    Future<String> f1; 

    for (int i = 0; i < MAX; i++) { 
    //  newParams = newParams + slight changes each iteration for each thread; 
     call1 = new HttpPostClass.HttpPost(url, newParams); 
     f1 = pool.submit(call1); 
    } 
    pool.shutdown(); 

我不知道這是否是因爲我重新使用相同的可贖回/未來對象的循環或如果這是由於我自己無知/經驗不足的多線程編程導致這個問題。

在此先感謝您提供的任何幫助。

+1

它是如何失敗? – SLaks

+2

你知道,有很多現有的壓力測試網站的解決方案...(jmeter,loadrunner,grinder想到非正式) – jtahlborn

+1

什麼是HttpPostClass?我注意到[org.apache.http.client.methods.HttpPost](http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/methods/HttpPost.html )被註釋爲不是線程安全的 – Jackson

回答