今天写了一个可以测试并发数和运行次数的压力测试代码。(Java)
今天写了一个可以测试并发数和运行次数的压力测试代码
- 介绍一下为什么会写这么一个工具。
- 介绍一个这个工具怎么用的。
背景
最近在开发CoapServer端,以及模拟设备侧发送数据调用开发好的CoapServer的性能,进行压力测试。
自己没有找到合适的压力测试的工具,但是测试诉求相对比较简单,觉得用Java可以来控制测试。
测试维度:
- 一共模拟1W台设备,共计发送数据100W次
- 模拟多台设备同时发送数据。
代码和使用
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.Utils;
import org.eclipse.californium.elements.exception.ConnectorException;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class PressTestClient {
static int count = 0;
//总访问量是client_num,并发量是thread_num
int thread_num = 10;
int client_num = 1000;
float avg_exec_time = 0;
float sum_exec_time = 0;
long first_exec_time = Long.MAX_VALUE;
long last_done_time = Long.MIN_VALUE;
float total_exec_time = 0;
String url = "";
String postData = "";
public PressTestClient(int thread_num, int client_num, String url, String postData) {
this.thread_num = thread_num;
this.client_num = client_num;
this.url = url;
this.postData = postData;
}
public void run() {
final PressTestClient currentObj = this;
final ConcurrentHashMap<Integer, ClientThreadRecord> records = new ConcurrentHashMap<Integer, ClientThreadRecord>();
// 建立ExecutorService线程池
ExecutorService exec = Executors.newFixedThreadPool(thread_num);
// thread_num个线程可以同时访问
// 模拟client_num个客户端访问
final CountDownLatch doneSignal = new CountDownLatch(client_num);
for (int i = 0; i < client_num; i++) {
Runnable run = new Runnable() {
public void run() {
int index = getIndex();
long st = System.currentTimeMillis();
try {
//测试的逻辑代码
TlsCoAPClient example = new TlsCoAPClient();
CoapClient coapClient = example.getClient("device_service");
CoapResponse response = null;
try {
System.out.println("start client request:" +index );
response = coapClient.get();
System.out.println("device_service: " + Utils.prettyPrint(response));
Thread.sleep(100);
} catch (ConnectorException | IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
records.put(index, new ClientThreadRecord(st, System.currentTimeMillis()));
doneSignal.countDown();//每调用一次countDown()方法,计数器减1
}
};
exec.execute(run);
}
try {
//计数器大于0 时,await()方法会阻塞程序继续执行
doneSignal.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
/**
* 获取每个线程的开始时间和结束时间
*/
for (int i : records.keySet()) {
ClientThreadRecord r = records.get(i);
sum_exec_time += ((double) (r.et - r.st)) / 1000;
if (r.st < first_exec_time) {
first_exec_time = r.st;
}
if (r.et > last_done_time) {
this.last_done_time = r.et;
}
}
this.avg_exec_time = this.sum_exec_time / records.size();
this.total_exec_time = ((float) (this.last_done_time - this.first_exec_time)) / 1000;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(4);
System.out.println("======================================================");
System.out.println("Thread Num: " + thread_num + ", Client Count: " + client_num + ".");
System.out.println("Avg Exec Time: " + nf.format(this.avg_exec_time) + " s");
System.out.println("Total Exec Time: " + nf.format(this.total_exec_time) + " s");
System.out.println("Throughput: " + nf.format(this.client_num / this.total_exec_time) + " /s");
}
public static int getIndex() {
return ++count;
}
public static void main(String[] args) {
//总访问量和并发量两重循环,依次增大访问
//访问量
for (int j = 500; j < 501; j += 100) {
//并发量
for (int i = 500; i < 501; i += 1) {
//要测试的URL
String url = "http://www.baidu.com/";
new PressTestClient(i, j, url, "").run();
}
}
System.out.println("finished!");
}
}
class ClientThreadRecord {
long st;
long et;
public ClientThreadRecord(long st, long et) {
this.st = st;
this.et = et;
}
}
如何使用?
- main方法中的循环此时是控制 运行数和并发数的
- 上面run方法,是控制你要测试的代码的。可以自定义。
效果展示


今天写了一个可以测试并发数和运行次数的压力测试代码。(Java)的更多相关文章
- 开源API测试工具 Hitchhiker v0.6更新 - 改进压力测试
Hitchhiker 是一款开源的支持多人协作的 Restful Api 测试工具,支持Schedule, 数据对比,压力测试,支持上传脚本定制请求,可以轻松部署到本地,和你的team成员一起协作测试 ...
- (总结)Web性能压力测试工具之WebBench详解
PS:在运维工作中,压力测试是一项很重要的工作.比如在一个网站上线之前,能承受多大访问量.在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验.但是,在压力测试中存在一个共性,那就是压力 ...
- 网站压力测试ab 命令
网站压力测试ab 命令 author: headsen chen 2017-10-25 10:06:35 个人原创,转载请注明作者,出处,否则依法追究法律责任! 1,制作一个a ...
- Web性能压力测试工具之WebBench
在运维工作中,压力测试是一项很重要的工作.比如在一个网站上线之前,能承受多大访问量.在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验.但是,在压力测试中存在一个共性,那就是压力测试的结果 ...
- Web性能压力测试工具之WebBench详解
PS:在运维工作中,压力测试是一项很重要的工作.比如在一个网站上线之前,能承受多大访问量.在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验.但是,在压力测试中存在一个共性,那就是压力测试 ...
- apache-ab并发负载压力测试 不错
ab -n 3000 -c 3000 http://www.test.com/ c 100 即:每次并发3000 个 n 10000 即: 共发送3000 个请求 ab -t 60 -c 100 ht ...
- Jmeter压力测试笔记(6)性能调测-压力并发-模拟生产环境数据
问题原因找到了,那就好办了. 找到阿里云技术人员,让他们强行给我们上架了一个共享代理模式的Redis. 并重新进行压力测试. 哦豁~ 开心,压力测试顺利,异常率大大降低实际为: 数据库DBA反馈,数据 ...
- 推荐一个linux下的web压力测试工具神器webbench
推荐一个linux下的web压力测试工具神器webbench2014-04-30 09:35:29 来源: 评论:0 点击:880 用多了apache的ab工具之后你就会发现ab存在很多问题, ...
- jmeter压力测试的简单实例+badboy脚本录制(一个简单的网页用户登录测试的结果)
JMeter的安装:在网上下载,在下载后的zip解压后,在bin目录下找到JMeter.bat文件,双击就可以运行JMeter. http://jmeter.apache.org/ 在使用jmeter ...
随机推荐
- 6月11日 python复习 mysql
01. 列举常见的关系型数据库和非关系型都有那些? 1.关系型数据库通过外键关联来建立表与表之间的关系,---------常见的有:SQLite.Oracle.mysql 2.非关系型数据库通常指数据 ...
- 分治FFT/NTT
粘板子: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; t ...
- CF1487G String Counting (容斥计数)
传送门 考虑$c[i]>n/3$这个关键条件!最多有2个字母数量超过$n/3$! 没有奇数回文?长度大于3的回文串中间一定是长度为3的回文串,所以合法串一定没有长度=3的回文,也就是$a[i]\ ...
- 如果一个 linux 新手想要知道当前系统支持的所有命令的列表,他需要怎么做?
使用命令 compgen -c,可以打印出所有支持的命令列表. [root@localhost ~]$ compgen -cl.lllswhichifthen elseelifficaseesacfo ...
- 什么是Spring Cloud Bus?
spring cloud bus 将分布式的节点用轻量的消息代理连接起来,它可以用于广播配置文件的更改或者服务直接的通讯,也可用于监控. 如果修改了配置文件,发送一次请求,所有的客户端便会重新读取配置 ...
- jdk 8 HashMap源码解读
转自:https://www.cnblogs.com/little-fly/p/7344285.html 在原来的作者的基础上,增加了本人对源代码的一些解读. 如有侵权,请联系本人 这几天学习了Has ...
- Vue报错之"[Vue warn]: Invalid prop: type check failed for prop "jingzinum". Expected Number with value NaN, got String with value "fuNum"."
一.报错截图 [Vue warn]: Invalid prop: type check failed for prop "jingzinum". Expected Number w ...
- FPGA入门到精通系列1:数字电路基础知识
本文主要介绍数字电路基础知识,用最简洁的内容介绍最核心的知识. 1.数字电路是什么? 数字电路是利用电源电压的高电平和低电平分别表示1和0,进而实现信息的表达.模拟信号:随时间连续变化的信号.处理 ...
- PCB设计常见规则及基本原则
一.PCB基础知识 1.全称:印制电路板或者印制线路板 2.分类 材质分类:硬板(Rigid PCB).软板FPC(Flexible PCB).软硬结合板(Rigid-Flex PCB).HDI板(含 ...
- 7分钟理解JS的节流、防抖及使用场景
前言 据说阿里有一道面试题就是谈谈函数节流和函数防抖.糟了,这可触碰到我的知识盲区了,好像听也没听过这2个东西,痛定思痛,赶紧学习学习.here we go! 概念和例子 函数防抖(debounce) ...