java初級網站性能程序測試..

 

package demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * 客戶端請求模擬器
 * 
 * @author saro
 * 
 */

public class ClientMock implements Runnable {

private static final int CLIENT_COUNT = 10; // 用戶數
private final int SLEEP_TIME = 1000; // 某個用戶每次訪問的間隔時間
private int request_count = 500; // 每個用戶準備發出的請求個數
private int requested_count = 0; // 已發出請求個數
private Document doc; // 網頁文檔對象
List<String> dstUrls = new ArrayList<String>(); // 請求地址庫
private int errorCount = 0; // 失敗的請求個數

public static void main(String[] args) throws Exception {
// 原始的請求地址集
List<String> srcUrls = new ArrayList<String>() {
{
add("http://localhost:8080/eshop3/eshop/base!index.action");
add("http://localhost:8080/eshop3/eshop/bus2!getById?bus.id=93&mall.id=461609");
add("http://localhost:8080/eshop3/eshop/goods2!search");
add("http://localhost:8080/eshop3/eshop/shop2!get?boss.id=461761&shop.id=461764");
}
};

// 從原始地址形成地址庫
ClientMock mocker = new ClientMock();
List<String> dstUrls = new ArrayList<String>();
for (String srcUrl : srcUrls) {
dstUrls.addAll(mocker.getUrls(srcUrl));
}


// 模擬多用戶請求
for (int i = 0; i < CLIENT_COUNT; i++) {
ClientMock client = new ClientMock();
client.setDstUrls(dstUrls);
new Thread(client, "thread_" + i).start();
}

}

@Override
public void run() {
try {
int remainCount = dstUrls.size(); // 剩餘個數
//while (request_count > 0) {
while (true) {
int randomNum = new Random().nextInt(dstUrls.size());
if(requested_count%5 == 0){
System.out.println("\r\n" + Thread.currentThread().getName() + "--------------" + randomNum + "/" + requested_count + "/" + dstUrls.size() + "----------------");
}
doRequest(dstUrls.get(randomNum));
remainCount--;
request_count--;
requested_count++;
}
/*
if (request_count == 0) {
System.err.println("::Report-------------errorCount of" + Thread.currentThread().getName() + " :" + errorCount + "/" + requested_count);
}
*/
} catch (Exception e) {
e.printStackTrace();
}
}

/**
 * 發送請求
 * 
 * @param dstUrl
 *            要被訪問的地址
 * @throws Exception
 */
private void doRequest(String dstUrl) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(dstUrl.replaceAll(" ", ""));
try {
HttpResponse response = client.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
/*
if (statusCode != 200) {
errorCount++;
System.err.println(Thread.currentThread().getName() + "--------------errorCount: " + errorCount);
}
*/
// System.out.println(dstUrl);
Thread.sleep(SLEEP_TIME);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

/**
 * 從原始地址形成地址庫
 * 
 * @param baseUrl
 *            原始地址
 * @return
 * @throws IOException
 */
private List<String> getUrls(String baseUrl) throws IOException {
List<String> urls = new ArrayList<String>();
doc = Jsoup.connect(baseUrl.trim()).get();
String sufferURI = doc.baseUri();

for (Element element : doc.getElementsByTag("a")) {
String href = element.attr("href");
if (href.startsWith("http://")) {
urls.add(href);
} else {
urls.add(sufferURI + href);
}
}

System.out.println("------------------------- 獲得" + urls.size() + "條url地址 ----------------------------");
return urls;
}

/**
 * 設置要被訪問的地址庫
 * 
 * @param dstUrls
 *            地址庫集合
 */
private void setDstUrls(List<String> dstUrls) {
this.dstUrls = dstUrls;
}

/**
 * 獲得失敗請求的個數
 * 
 * @return
 */
private int getErrorCount() {
return this.errorCount;
}

}


發佈了29 篇原創文章 · 獲贊 14 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章