写点什么

WORK-07- 压力测试程序

用户头像
蒜泥精英
关注
发布于: 2020 年 07 月 22 日
WORK-07-压力测试程序



压力测试程序,启动10个并发,每个并发压测10轮次,输出压测平均响应时间和95%的响应时间

【输出截图-启动10个并发线程】



【执行压测】



【输出压测结果】



【执行入口】

package com.study.work006;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws InterruptedException {
StreeTestToolV2 toolinst= new StreeTestToolV2();
// write your code here
System.out.println("HELLO ,StressTestTool!");
//输入 并发数、循环周期、压测地址
//输出 平均响应时间、95%响应时间
int iThreadCnt=10;
int iCycleCnt=10;
String strurl="https://www.baidu.com";
//执行压测方案
System.out.println("BGING DOSTRESS");
StreeTestToolV2.doStress( iThreadCnt, strurl, iCycleCnt,toolinst.getDataInfo());
//等待线程全部都执行完。
while (toolinst.getDataInfo().size()<iCycleCnt*iThreadCnt )
{
// System.out.println("current list size="+toolinst.getDataInfo().size());
Thread.sleep(1000);
};
System.out.println("current list size="+toolinst.getDataInfo().size());
System.out.println("END DOSTRESS");
//收集执行结果,放入数组中
long LResultArr[] =new long[iCycleCnt*iThreadCnt];
for(int cnt=0;cnt<(iCycleCnt*iThreadCnt);cnt++) {
long ltmp=toolinst.getDataInfo().get(cnt);
System.out.println("ltmp="+ltmp);
LResultArr[cnt]=ltmp;
}
//平均压测耗时
System.out.println("平均压测耗时:"+toolinst.average(LResultArr) );
//排序,然后打出第95个
Arrays.sort(LResultArr);
System.out.println("95%的耗时:"+LResultArr[94]);
}
}



【压测工具类】

package com.study.work006;
import java.util.ArrayList;
import java.util.List;
public class StreeTestToolV2 {
private List<Long> stressDataInfo;
public StreeTestToolV2()
{
this.stressDataInfo=new ArrayList<Long>();
}
public List<Long> getDataInfo()
{
return this.stressDataInfo;
}
public static void doStress(int iThreadCnt,String strurl,int iCycleCnt,List stressDataInfo)
{
for(int i=0;i<iThreadCnt;i++)
{
TestWorkThread tmp=new TestWorkThread("stress-thread"+i,strurl,iCycleCnt,stressDataInfo);
tmp.start();
}
}
public double average(long[] x) {
int n = x.length; //数列元素个数
double sum = 0;
for (long i : x) { //求和
sum += i;
}
return sum / n;
}
}



【压测线程的执行类】

package com.study.work006;
import java.util.List;
public class TestWorkThread implements Runnable{
private Thread threadinst;
private String strthreadinstname;
private String strurl;
private int iCycleCnt;
private List stressDataInfo;
public TestWorkThread(String strthreadinstname, String strurl, int iCycleCnt, List stressDataInfo)
{
this.strthreadinstname=strthreadinstname;
this.strurl=strurl;
this.iCycleCnt=iCycleCnt;
this.stressDataInfo=stressDataInfo;
System.out.println("Creating "+this.strthreadinstname +" strurl="+this.strurl + " iCycleCnt="+this.iCycleCnt);
}
public void start()
{
System.out.println("Starting "+this.strthreadinstname);
if(this.threadinst==null)
{
this.threadinst=new Thread(this,this.strthreadinstname);
threadinst.start();
}
}
@Override
public void run() {
System.out.println("Running "+this.strthreadinstname);
TestHttp testhttp=new TestHttp();
String strurl=this.strurl;
String strout=null;
long datainfo[] =new long[this.iCycleCnt];
for(int cnt=0;cnt<this.iCycleCnt;cnt++) {
long starttime = System.currentTimeMillis();
String strret = testhttp.httpRequest(strurl, "GET", strout);
long endtime = System.currentTimeMillis();
long usedtime=endtime-starttime;
datainfo[cnt]=usedtime;
System.out.println("[" + this.strthreadinstname + "]" +"第"+cnt+"次"+ "strret=" + strret);
}
//将数据写入工具的LIST中
for(int cnt=0;cnt<this.iCycleCnt;cnt++) {
this.stressDataInfo.add(datainfo[cnt]);
}
}
}



用户头像

蒜泥精英

关注

还未添加个人签名 2018.09.19 加入

还未添加个人简介

评论

发布
暂无评论
WORK-07-压力测试程序