今天写了一个可以测试并发数和运行次数的压力测试代码

  1. 介绍一下为什么会写这么一个工具。
  2. 介绍一个这个工具怎么用的。

背景

最近在开发CoapServer端,以及模拟设备侧发送数据调用开发好的CoapServer的性能,进行压力测试。

自己没有找到合适的压力测试的工具,但是测试诉求相对比较简单,觉得用Java可以来控制测试。

测试维度:

  1. 一共模拟1W台设备,共计发送数据100W次
  2. 模拟多台设备同时发送数据。

代码和使用

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

如何使用?

  1. main方法中的循环此时是控制 运行数和并发数的
  2. 上面run方法,是控制你要测试的代码的。可以自定义。

效果展示

今天写了一个可以测试并发数和运行次数的压力测试代码。(Java)的更多相关文章

  1. 开源API测试工具 Hitchhiker v0.6更新 - 改进压力测试

    Hitchhiker 是一款开源的支持多人协作的 Restful Api 测试工具,支持Schedule, 数据对比,压力测试,支持上传脚本定制请求,可以轻松部署到本地,和你的team成员一起协作测试 ...

  2. (总结)Web性能压力测试工具之WebBench详解

      PS:在运维工作中,压力测试是一项很重要的工作.比如在一个网站上线之前,能承受多大访问量.在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验.但是,在压力测试中存在一个共性,那就是压力 ...

  3. 网站压力测试ab 命令

    网站压力测试ab 命令 author: headsen   chen         2017-10-25   10:06:35 个人原创,转载请注明作者,出处,否则依法追究法律责任! 1,制作一个a ...

  4. Web性能压力测试工具之WebBench

    在运维工作中,压力测试是一项很重要的工作.比如在一个网站上线之前,能承受多大访问量.在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验.但是,在压力测试中存在一个共性,那就是压力测试的结果 ...

  5. Web性能压力测试工具之WebBench详解

    PS:在运维工作中,压力测试是一项很重要的工作.比如在一个网站上线之前,能承受多大访问量.在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验.但是,在压力测试中存在一个共性,那就是压力测试 ...

  6. apache-ab并发负载压力测试 不错

    ab -n 3000 -c 3000 http://www.test.com/ c 100 即:每次并发3000 个 n 10000 即: 共发送3000 个请求 ab -t 60 -c 100 ht ...

  7. Jmeter压力测试笔记(6)性能调测-压力并发-模拟生产环境数据

    问题原因找到了,那就好办了. 找到阿里云技术人员,让他们强行给我们上架了一个共享代理模式的Redis. 并重新进行压力测试. 哦豁~ 开心,压力测试顺利,异常率大大降低实际为: 数据库DBA反馈,数据 ...

  8. 推荐一个linux下的web压力测试工具神器webbench

    推荐一个linux下的web压力测试工具神器webbench2014-04-30 09:35:29   来源:   评论:0 点击:880 用多了apache的ab工具之后你就会发现ab存在很多问题, ...

  9. jmeter压力测试的简单实例+badboy脚本录制(一个简单的网页用户登录测试的结果)

    JMeter的安装:在网上下载,在下载后的zip解压后,在bin目录下找到JMeter.bat文件,双击就可以运行JMeter. http://jmeter.apache.org/ 在使用jmeter ...

随机推荐

  1. JavaWeb——Http

    4.1.什么是http http(超文本传输协议)是一个简单的请求-响应协议,它通常运行在TCP之上. 文本:无链接 超文本:利用超链接将普通文本的信息组织在一起的超级文本 4.2.http两个时代的 ...

  2. Eclipse阿里云镜像源配置

    镜像下载.域名解析.时间同步请点击 阿里巴巴开源镜像站 一.什么是Eclipse Eclipse 是一个开放源代码的.基于 Java 的可扩展开发平台.就其本身而言,它只是一个框架和一组服务,用于通过 ...

  3. Mariadb开启密码复杂度

    mariadb开启密码复杂度 #安装插件# INSTALL SONAME 'simple_password_check'; #设置输入错误多少次锁定# set global max_password_ ...

  4. FPGA驱动LCD显示红绿蓝彩条

    实验目的:先简单熟悉LCD灯的驱动和时序图的代码实现.设计功能是让LCD显示红绿蓝三种颜色,即三个彩带.本次实验比较容易实现,主要是对LCD驱动时序图的理解和时序参数的配置. 实验条件:1.LCD原理 ...

  5. SVPWM实现概述

    SVPWM是FOC的基础,其实现流程大致如下所示: 1. 判断合成矢量所在扇区 2. 计算相邻矢量作用时间 3. 计算各桥臂导通时间 4. 得到各相PWM占空比 5. 更新相应寄存器值  SVPWM目 ...

  6. 如何看待malloc产生内存碎片

    上代码直接研究: int main() { int *heap_d; int *heap_e; int *heap_f; heap_d = (int *)malloc(10); heap_e = (i ...

  7. 一台 Linux 系统初始化环境后需要做一些什么安全工作?

    1.添加普通用户登陆,禁止 root 用户登陆,更改 SSH 端口号.        修改 SSH 端口不一定绝对哈.当然,如果要暴露在外网,建议改下.l    2.服务器使用密钥登陆,禁止密码登陆. ...

  8. Struts2框架提供的结果类型?

    已配置结果类型名 类 名 描 述 dispatcher org.apache.struts2.dispatcher.ServletDispatcherResult 默认结果类型,用来呈现JSP页面 c ...

  9. 如何用 Java 代码列出一个目录下所有的文件?

    如果只要求列出当前文件夹下的文件,代码如下所示: import java.io.File; class Test12 { public static void main(String[] args) ...

  10. Redis6.0配置文件翻译(Google手动翻译)

    原文链接(一般情况下你打不开这个网页):https://raw.githubusercontent.com/redis/redis/6.0/redis.conf Redis配置文件 请注意,为了读取配 ...