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

}

转载于:https://www.cnblogs.com/kevinfuture/p/5061150.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值