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

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值