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;
}
}