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 = "?";
String para = "method=getQueryResultpageNum=1pageSize=5"
+ "queryKeyWord="
+ getRandomSearchKey(NO)
+ "questionID=-1questionIdPath=-1searchType=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;
}
}
你在loadrunner 中的JAVAVUSER编写开发就行了,测试代码是根据你的需求来开发编写的,不是通用的代码!
经典的自然是loadrunner,可以进行并发压力测试,很实用,可以模拟多IP,多用户同时运行,可以设置运行间隔,可以参考下相关的文档,建议去csdn上下一个相关的教程
你好,你这个问题问的好抽象。测试是测什么,测你写的程序,当你连你的程序是什么的类型的时候还没有弄清楚,怎么能写压力测试呢,你是要测并发呢?还是测效率呢?
所以这个要根据实际情况来确定。
使用loadrunner,进行压力测试,有比较详细的测试结果,
就是配置和使用loadrunner比较麻烦,不过网上有详细的教程,去下载一个吧,
自己写循环去测试,极不准确