文章目录
显示
Python微信订餐小程序课程视频
https://blog.csdn.net/m0_56069948/article/details/122285951
Python实战量化交易理财系统
https://blog.csdn.net/m0_56069948/article/details/122285941
今天写了一个可以测试并发数和运行次数的压力测试代码
- 介绍一下为什么会写这么一个工具。
- 介绍一个这个工具怎么用的。
背景
最近在开发CoapServer端,以及模拟设备侧发送数据调用开发好的CoapServer的性能,进行压力测试。
自己没有找到合适的压力测试的工具,但是测试诉求相对比较简单,觉得用Java可以来控制测试。
测试维度:
- 一共模拟1W台设备,共计发送数据100W次
- 模拟多台设备同时发送数据。
代码和使用
Copyimport 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 records = new ConcurrentHashMap();
// 建立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方法,是控制你要测试的代码的。可以自定义。