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信息的更多相关文章

  1. JMeter压力测试,http请求压测,5分钟让你学会如何压测接口!

    JMeter压力测试 官网:https://jmeter.apache.org 最新款的jmeter需要java8的支持,所以请自行安装jdk8.这里就不啰嗦了. 可以根据自己的系统下载zip或者是t ...

  2. 记一次完整的java项目压力测试

    总结:通过这次压力测试,增加了对程序的理解:假定正常情况下方法执行时间为2秒,吞吐量为100/s,则并发为200/s:假设用户可接受范围为10s,那么并发量可以继续增加到1000/s,到这个时候一切还 ...

  3. 使用apache的ab压力测试时失败请求原因

    只要出现 Failed requests 就会多出现一行要求失败的各原因的数据统计,分别有 Connect, Length,与 Exception 三种,分别代表的意义为:Connect      无 ...

  4. apache bench(ab)压力测试模拟POSt请求

    ab命令格式: -N|--count 总请求数,缺省 : 5w -C|--clients 并发数, 缺省 : 100 -R|--rounds 测试次数, 缺省 : 10 次 -S|-sleeptime ...

  5. Java项目压力测试(待补)

    JVM监控使用ava自带jvisualvm,在java安装目录jdk1.*/bin下(有很多更高级的东西 线程2000以下,太多切换太消耗.CPU使用率30%以下,更健壮

  6. (转)学习使用Jmeter做压力测试(一)--压力测试基本概念

    一.性能测试的概念 性能测试是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测试,两者可以结合进行. 通过负载测试,确定在各种工作负载下 ...

  7. 【转】学习使用Jmeter做压力测试(一)--压力测试基本概念

    一.性能测试的概念 性能测试是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测试,两者可以结合进行. 通过负载测试,确定在各种工作负载下 ...

  8. 【转】jmeter压力测试

    jmeter压力测试 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试但后来扩展到其他测试领域, 是压力测试的首选软件 ...

  9. 学习使用Jmeter做压力测试(一)--压力测试基本概念

    学习使用Jmeter做压力测试(一)--压力测试基本概念 一.性能测试的概念 性能测试是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测 ...

随机推荐

  1. js的特殊运算符

    1)三元条件运算符: c是一个布尔值,当c为true的时候,取冒号左边a的值,否取冒号右边的b的值: 2)逗号运算符: 值从左到右依次计算,取最右边的,例如例子里的val,会取最右边的值3: 特殊运算 ...

  2. 安卓工程中定义的app_name等报错解决办法 工程上有叹号

    类似于"app_name" is not translated in af, am, ar, be, bg, ca, cs, da, de, el, en-rGB, es, es- ...

  3. asp.net时间范围查询

    1.首先要查询类表中的一个时间段,要明确我的数据库中只有一个时间字段,我们先将他拆分一下. if ($("#news_OpenTime").val() != "" ...

  4. jquery 操作listbox 左右相互选择

    实现左右两个listbox的相互选择功能 代码如下: function ListBox_Move(listfrom, listto) { var size = $j("#" + l ...

  5. Oracle 锁模式

    0:none  1:null 空  2:Row-S 行共享(RS):共享表锁  3:Row-X 行专用(RX):用于行的修改  4:Share 共享锁(S):阻止其他DML操作  5:S/Row-X ...

  6. Openjudge-NOI题库-对齐输出

     题目描述 Description 读入三个整数,按每个整数占8个字符的宽度,右对齐输出它们.  输入输出格式 Input/output 输入格式: 只有一行,包含三个整数,整数之间以一个空格分开. ...

  7. <hdu-2032>杨辉三角

    这是杭电hdu上杨辉三角的链接:http://acm.hdu.edu.cn/showproblem.php?pid=2032  Problem Description: 还记得中学时候学过的杨辉三角吗 ...

  8. Java与JavaScript中判断两字符串是否相等的区别

    JavaScript是一种常用的脚本语言,这也决定了其相对于其他编程语言显得并不是很规范.在JavaScript中判断两字符串是否相等 直接用==,这与C++里的String类一样.而Java里的等号 ...

  9. CSS-负边距原理

    一.负边距原理 正边距以相邻模块的位置为参考点进行移动,并对周围模块进行合理地排挤. 负边距即margin的四个边界值为负值. 在html中使用负边距margin-left和margin-top相当于 ...

  10. hibernate简单的增删改查

    获取当前线程的session protected Session getSession() { return sessionFactory.getCurrentSession(); } 增加:save ...