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();

  1. OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(excepFile));
  2. //另一种写法:OutputStreamWriter ow = new OutputStreamWriter(new FileWriter("error/error.txt"));
  3. BufferedWriter bufferWriter = new BufferedWriter(ow);
  4. bufferWriter.write("出现异常:"+e);
  5. ow.flush();
  6. ow.close();
  7. }catch(Exception ex){
  8. ex.printStackTrace();
  9. }
  10. e.printStackTrace();
  11. }
  12. url=arrayList.get(0).toString();
  13. threadNum = (int)Integer.parseInt(arrayList.get(1).toString());
  14. clientNum=(int)Integer.parseInt(arrayList.get(2).toString());
  15. exceptInfo=(boolean)Boolean.parseBoolean(arrayList.get(3).toString());
  16. outputInfo=(boolean)Boolean.parseBoolean(arrayList.get(4).toString());
  17. if(arrayList.get(5)!=null)
  18. sleepTime = (int)Integer.parseInt(arrayList.get(5).toString());
  19. //requestMethods=arrayList.get(6).toString();
  20. System.out.println("参数信息:"+arrayList);
  21. System.out.println("参数信息:1、"+url+" 2、"+threadNum+" 3、"+clientNum+" 4、"+exceptInfo+" 5、"+outputInfo);
  22. }
  23. public static void main(String[] args){
  24. //创建线程池
  25. ExecutorService threadPool = Executors.newCachedThreadPool();
  26. //设置信号量,也就是线程数(之前理解有误,并不是线程数,跟线程池两个概念)
  27. final Semaphore semp = new Semaphore(threadNum);
  28. for(int i=0;i<clientNum;i++){
  29. Runnable run = new Runnable(){
  30. public void run(){
  31. try{
  32. semp.acquire();
  33. URL urls = new URL(url);//传入url地址
  34. HttpURLConnection con = (HttpURLConnection)urls.openConnection();
  35. //if(requestMethods!=null)
  36. //con.setRequestMethod(requestMethods);//setRequestProperty请求属性不设置了
  37. Thread.sleep(sleepTime);
  38. if(outputInfo==true){
  39. File outputInfoDirec =new File("info");//创建文件夹
  40. if(!(outputInfoDirec.exists()&&outputInfoDirec.isDirectory()))
  41. outputInfoDirec.mkdirs();
  42. //读:获取线程访问的字节流,同时转为字符流
  43. BufferedReader getInfo = new BufferedReader(new InputStreamReader(con.getInputStream()));
  44. //写:写入文件
  45. FileWriter fwInfo =new FileWriter("info/data_"+Thread.currentThread().getName()+".txt");
  46. BufferedWriter setInfo = new BufferedWriter(fwInfo);
  47. //从流中读取数据,存入文件中
  48. String line = "";
  49. while((line=getInfo.readLine())!=null){
  50. setInfo.write(line);
  51. fwInfo.flush();
  52. }
  53. fwInfo.close();
  54. }
  55. con.setDoOutput(true);
  56. con.setDoInput(true);
  57. semp.release();
  58. }catch(Exception ee){
  59. ee.printStackTrace();
  60. }finally{
  61. }
  62. }
  63. };
  64. threadPool.execute(run);
  65. }
  66. threadPool.shutdown();
  67. }
  68. //读取参数
  69. public static void readParmars(File file)throws Exception{
  70. InputStreamReader getPar = new InputStreamReader(new FileInputStream(file));//获取读取流,同时转为字符流
  71. BufferedReader getString = new BufferedReader(getPar);
  72. String line= "";
  73. while((line=getString.readLine())!=null){
  74. arrayList.add(line.split("==")[1]);
  75. }
  76. getPar.close();
  77. }
  78. //写入参数:字节
  79. public static void createProperties(String proFileName)throws Exception{
  80. BufferedOutputStream bufferStream = new BufferedOutputStream(new FileOutputStream(proFileName));
  81. testWriteBuffer(bufferStream,"Url Address==");//因为是按照字节流读取的,而汉字占用两个字节,所以如果使用汉字会出现乱码,因为字母是占用一个字节的
  82. testWriteBuffer(bufferStream,"treadNum==");
  83. testWriteBuffer(bufferStream,"clientNum==");
  84. testWriteBuffer(bufferStream,"print Exception(true Or false)==");
  85. testWriteBuffer(bufferStream,"print RequestData(true Or false)==");
  86. testWriteBuffer(bufferStream,"sleep Time(default:2000)==");
  87. //testWriteBuffer(bufferStream,"request Method(default:all)==");
  88. bufferStream.flush();
  89. bufferStream.close();
  90. }
  91. //写入数据:字节
  92. public static void testWriteBuffer(BufferedOutputStream testWrite,String parmars)throws Exception{
  93. char[] parmarsArray = parmars.toCharArray();
  94. for(int i=0;i<parmarsArray.length;i++){
  95. testWrite.write(parmarsArray[i]);
  96. }
  97. char[] enter = "\r\n".toCharArray();
  98. for(int i=0;i<enter.length;i++){
  99. testWrite.write(enter[i]);
  100. }
  101. }

}

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. Linux scp命令

    语法 scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P p ...

  2. 面向对象UML中类关系

    如果你确定两件对象之间是is-a的关系,那么此时你应该使用继承:比如菱形.圆形和方形都是形状的一种,那么他们都应该从形状类继承而不是聚合.如果你确定两件对象之间是has-a的关系,那么此时你应该使用聚 ...

  3. hdu 4122 Alice's mooncake shop(单调队列)

    题目链接:hdu 4122 Alice's mooncake shop 题意: 有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接 ...

  4. iOS消息推送相关

    远程推送 iOS开发之实现App消息推送:http://blog.csdn.net/shenjie12345678/article/details/41120637 国内90%以上的iOS开发者,对A ...

  5. hdu 1846 Brave Game 简单博弈

    Problem Description 十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫<勇敢者的游戏>(英文名称:Zathura),一直到现在,我依然对于电影中 ...

  6. Hive 常用函数

    参考地址:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF 1. parse_url(url, partToExt ...

  7. 解决Windows内存问题的两个小工具RamMap和VMMap(这个更牛更好)

    来源:http://www.cr173.com/html/13006_1.html .net程序内存监测分配工具(CLR Profiler for .NET Framework 4)官方安装版 类型: ...

  8. Python笔记4-20151029

    一.切片 L = [''Michael','Sarah','Tracy','Bob','Jack'] 取前N个元素,也就是索引为0-(N-1)的元素,可以用循环: >>> r = [ ...

  9. sudo命令出错 must set be suid

    特殊权限 4,2 ,1 4  suid,控制用户执行的文件,以文件所属用户的身份执行.当一个可执行的文件拥有suid时,会将文件拥有者的x位变成s(---s--x--x),这时称为set uid,简写 ...

  10. Java Day03 面向对象程序设计

    1.面向对象 面向对象是指一种程序设计泛型,同时也是一种程序开发的方法. 2.类 类是一种抽象的概念,类中包含了数据与对数据的操纵. 具有相同特性(数据元素)和行为(功能)的对象的抽象就是类.类是对象 ...