Java实现压力测试---可输出请求信息、error信息
import java.io.;
import java.net.;
import java.util.;
import java.util.concurrent.;
public class Test{
//使用静态方法,创建出properties文件,然后输入参数,如线程数量、访问数量、rul等
public static String url = "";//url地址
public static int threadNum = 0;//线程数量
public static int clientNum = 0;//访问数量
public static boolean exceptInfo = true;//打印异常
public static boolean outputInfo= true;//打印出访问信息
public static int sleepTime= 1000;//打印出访问信息
public static List arrayList=new ArrayList();//获取参数
//public static String requestMethods = "";//请求方式
static{
try{
//这里我全部用字节流,如果需要就适当的转换为字符流
//创建文件写入流也就是属于字符流,也可以使用字节流,为了保证数据安全性,建议使用输入字节流;为了方便,建议使用字符流
//InputStreamReader:将字节流=》字符流;FileInputStream:读取文件流;FileWriter:文件写入流(字符流)
String proFileName = "properties.properties";
File file = new File(proFileName);
if(!file.exists()){
FileWriter createFile = new FileWriter(proFileName);
createProperties(proFileName);
//确实使用字节流不好用,之后的使用字符流吧!!!
}
readParmars(file);
//BufferedWriter writeBuffer = new BufferedWriter(readContent);
}catch(Exception e){
try{
File excepFileDirec =new File("error");
if(!(excepFileDirec.exists()&&excepFileDirec.isDirectory()))
excepFileDirec.mkdirs();
File excepFile = new File("error/error.txt");
if(!(excepFile.exists()))
excepFile.createNewFile();
OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(excepFile));
//另一种写法:OutputStreamWriter ow = new OutputStreamWriter(new FileWriter("error/error.txt"));
BufferedWriter bufferWriter = new BufferedWriter(ow);
bufferWriter.write("出现异常:"+e);
ow.flush();
ow.close();
}catch(Exception ex){
ex.printStackTrace();
}
e.printStackTrace();
}
url=arrayList.get(0).toString();
threadNum = (int)Integer.parseInt(arrayList.get(1).toString());
clientNum=(int)Integer.parseInt(arrayList.get(2).toString());
exceptInfo=(boolean)Boolean.parseBoolean(arrayList.get(3).toString());
outputInfo=(boolean)Boolean.parseBoolean(arrayList.get(4).toString());
if(arrayList.get(5)!=null)
sleepTime = (int)Integer.parseInt(arrayList.get(5).toString());
//requestMethods=arrayList.get(6).toString();
System.out.println("参数信息:"+arrayList);
System.out.println("参数信息:1、"+url+" 2、"+threadNum+" 3、"+clientNum+" 4、"+exceptInfo+" 5、"+outputInfo);
}
public static void main(String[] args){
//创建线程池
ExecutorService threadPool = Executors.newCachedThreadPool();
//设置信号量,也就是线程数(之前理解有误,并不是线程数,跟线程池两个概念)
final Semaphore semp = new Semaphore(threadNum);
for(int i=0;i<clientNum;i++){
Runnable run = new Runnable(){
public void run(){
try{
semp.acquire();
URL urls = new URL(url);//传入url地址
HttpURLConnection con = (HttpURLConnection)urls.openConnection();
//if(requestMethods!=null)
//con.setRequestMethod(requestMethods);//setRequestProperty请求属性不设置了
Thread.sleep(sleepTime);
if(outputInfo==true){
File outputInfoDirec =new File("info");//创建文件夹
if(!(outputInfoDirec.exists()&&outputInfoDirec.isDirectory()))
outputInfoDirec.mkdirs();
//读:获取线程访问的字节流,同时转为字符流
BufferedReader getInfo = new BufferedReader(new InputStreamReader(con.getInputStream()));
//写:写入文件
FileWriter fwInfo =new FileWriter("info/data_"+Thread.currentThread().getName()+".txt");
BufferedWriter setInfo = new BufferedWriter(fwInfo);
//从流中读取数据,存入文件中
String line = "";
while((line=getInfo.readLine())!=null){
setInfo.write(line);
fwInfo.flush();
}
fwInfo.close();
}
con.setDoOutput(true);
con.setDoInput(true);
semp.release();
}catch(Exception ee){
ee.printStackTrace();
}finally{
}
}
};
threadPool.execute(run);
}
threadPool.shutdown();
}
//读取参数
public static void readParmars(File file)throws Exception{
InputStreamReader getPar = new InputStreamReader(new FileInputStream(file));//获取读取流,同时转为字符流
BufferedReader getString = new BufferedReader(getPar);
String line= "";
while((line=getString.readLine())!=null){
arrayList.add(line.split("==")[1]);
}
getPar.close();
}
//写入参数:字节
public static void createProperties(String proFileName)throws Exception{
BufferedOutputStream bufferStream = new BufferedOutputStream(new FileOutputStream(proFileName));
testWriteBuffer(bufferStream,"Url Address==");//因为是按照字节流读取的,而汉字占用两个字节,所以如果使用汉字会出现乱码,因为字母是占用一个字节的
testWriteBuffer(bufferStream,"treadNum==");
testWriteBuffer(bufferStream,"clientNum==");
testWriteBuffer(bufferStream,"print Exception(true Or false)==");
testWriteBuffer(bufferStream,"print RequestData(true Or false)==");
testWriteBuffer(bufferStream,"sleep Time(default:2000)==");
//testWriteBuffer(bufferStream,"request Method(default:all)==");
bufferStream.flush();
bufferStream.close();
}
//写入数据:字节
public static void testWriteBuffer(BufferedOutputStream testWrite,String parmars)throws Exception{
char[] parmarsArray = parmars.toCharArray();
for(int i=0;i<parmarsArray.length;i++){
testWrite.write(parmarsArray[i]);
}
char[] enter = "\r\n".toCharArray();
for(int i=0;i<enter.length;i++){
testWrite.write(enter[i]);
}
}
}
Java实现压力测试---可输出请求信息、error信息的更多相关文章
- JMeter压力测试,http请求压测,5分钟让你学会如何压测接口!
JMeter压力测试 官网:https://jmeter.apache.org 最新款的jmeter需要java8的支持,所以请自行安装jdk8.这里就不啰嗦了. 可以根据自己的系统下载zip或者是t ...
- 记一次完整的java项目压力测试
总结:通过这次压力测试,增加了对程序的理解:假定正常情况下方法执行时间为2秒,吞吐量为100/s,则并发为200/s:假设用户可接受范围为10s,那么并发量可以继续增加到1000/s,到这个时候一切还 ...
- 使用apache的ab压力测试时失败请求原因
只要出现 Failed requests 就会多出现一行要求失败的各原因的数据统计,分别有 Connect, Length,与 Exception 三种,分别代表的意义为:Connect 无 ...
- apache bench(ab)压力测试模拟POSt请求
ab命令格式: -N|--count 总请求数,缺省 : 5w -C|--clients 并发数, 缺省 : 100 -R|--rounds 测试次数, 缺省 : 10 次 -S|-sleeptime ...
- Java项目压力测试(待补)
JVM监控使用ava自带jvisualvm,在java安装目录jdk1.*/bin下(有很多更高级的东西 线程2000以下,太多切换太消耗.CPU使用率30%以下,更健壮
- (转)学习使用Jmeter做压力测试(一)--压力测试基本概念
一.性能测试的概念 性能测试是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测试,两者可以结合进行. 通过负载测试,确定在各种工作负载下 ...
- 【转】学习使用Jmeter做压力测试(一)--压力测试基本概念
一.性能测试的概念 性能测试是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测试,两者可以结合进行. 通过负载测试,确定在各种工作负载下 ...
- 【转】jmeter压力测试
jmeter压力测试 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试但后来扩展到其他测试领域, 是压力测试的首选软件 ...
- 学习使用Jmeter做压力测试(一)--压力测试基本概念
学习使用Jmeter做压力测试(一)--压力测试基本概念 一.性能测试的概念 性能测试是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测 ...
随机推荐
- Struts2的通配符配置方式
Struts2的Action类很有意思,你可以使用3种方式来实现具体的Action类: 让你的Action类继承自ActionSupport类(项目中最常用这种方式,因为ActionSupport类中 ...
- Lua: 给 Redis 用户的入门指导
转自:http://www.oschina.net/translate/intro-to-lua-for-redis-programmers 可能你已经听说过Redis 中嵌入了脚本语言,但是你还没有 ...
- Oracle 连接字符串
<!--web.config--><connectionStrings> <add name="MSSQL" connectionString=&qu ...
- Drupal设置首页默认内容
接触Drupal时间不长,记录一下学习点滴~ Drupal首页的内容,默认是取node表的内容展示的,如果想让首页展示自己创表的内容怎么办呢?以Drupal7为例 在这个admin/config/sy ...
- 无法启动T-SQL调试。未能连接到计算机"."。这是在主机名解析时通常出现的暂时错误……
今天在按在SSMS中,按F11进行断点调试时,弹出错误:"无法启动T-SQL调试.未能连接到计算器".".这是在主机名解析时通常出现的暂时错误--", 如下图: ...
- 用perl发送数据到钓鱼站
一个皮皮果的游戏很火 估计很多人想黑 偶然发现一个钓鱼站: http://ppgpergame.com/ 钓用户名与密码 抓包后发现数据会发送到: http://ppgpergame.com/db.a ...
- mysql近几天的查询
今天:select*from table1 where to_days(时间字段名)=to_days(now()); 昨天:select*from table1 where to_days(now() ...
- swift 图像的压缩上传
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [Str ...
- PhotonServer 学习
版本:Photon-OnPremise-Server-SDK_v3-4-31-10808 输出文件夹:deploy/名称/bin PhotonServer.config 配置 <Applicat ...
- Documention
Object.bool Does the object exist? Object.name Components share the same name with the game object a ...