java使用jmeter生成jmx之导入cvs文件
前言:
继上一篇,使用java生成jmx文件,并运行,但是我们发现,传递的数据只有一个请求体,也就是当不设置循环调用的时候,该接口之调用一次那么有没有什么办法,我们根据请求体传递的参数不同来调用多次?
当然有了,我们使用jmeter自带的导入csv文件功能,我们将每次请求的不同的请求体放入到csv文件中
创建csv文件
文件内容如下图所示:
我们使用java代码生成。
package com.yiyang.myfirstspringdemo.utils;
import com.yiyang.myfirstspringdemo.model.Passenger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static com.yiyang.myfirstspringdemo.service.JemterTest.JMETER_ENCODING;
import static com.yiyang.myfirstspringdemo.service.JemterTest.NUMBER_THREADS;
/**
* @Author 刘翊扬
* @Date 2020/10/11 3:12 下午
* @Version 1.0
*/
@Slf4j
@Component
public class CVSUtils {
static String format = "\"%s\"";
/**
* 生成为CVS文件
*
* @param exportData 源数据List
* @param outPutPath 文件路径
* @param fileName 文件名称
* @return
*/
private static File createCSVFile(List<List<String>> exportData, String outPutPath, String fileName) {
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!file.exists()) {
if (file.mkdirs()) {
log.info("创建cvs文件成功");
} else {
log.error("创建cvs文件失败");
}
}
// 定义文件名格式并创建
csvFile = File.createTempFile(fileName, ".csv", new File(outPutPath));
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile)));
for (List<String> exportDatum : exportData) {
writeRow(exportDatum, csvFileOutputStream);
csvFileOutputStream.newLine();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (csvFileOutputStream != null) {
try {
csvFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 如果临时文件,不需要在使用完成之后,记得删除。我这里就先不删除了。
// csvFile.delete();
// csvFile.deleteOnExit();
}
return csvFile;
}
/**
* 写一行数据
* @param row 数据列表
* @param csvWriter
* @throws IOException
*/
private static void writeRow(List<String> row, BufferedWriter csvWriter) throws IOException {
for (String data : row) {
csvWriter.write(new String(data.getBytes(JMETER_ENCODING), JMETER_ENCODING));
// csvWriter.write(data);
}
}
/***
* 将json弄成csv文件可以识别的,保证这个请求体(json格式)在同一列
* @param object
* @return
* @throws IllegalAccessException
*/
private static String cvsFormat(Object object) throws IllegalAccessException {
StringBuilder sb = new StringBuilder();
sb.append(String.format(format, "{"));
Class<?> aClass = object.getClass();
Field[] declaredFields = aClass.getDeclaredFields();
int i = 0;
for(Field field : declaredFields) {
field.setAccessible(true);
Object value = field.get(object);
if (value == null) {
continue;
}
String name = field.getName();
sb.append(String.format(format, name));
sb.append(String.format(format, ":"));
sb.append(String.format(format, value));
if (i < declaredFields.length - 1) {
sb.append(String.format(format, ","));
}
i++;
}
sb.append(String.format(format, "}"));
return sb.toString();
}
public static File getCsvPath () {
String outFile = "/Users/liufei/Downloads/jmter";
String filename = "my_replay_data";
List<List<String>> listList = new ArrayList<List<String>>();
List<String> list = null;
list = new ArrayList<>();//一个List为一行
// 标题
list.add("body");
listList.add(list);
// 内容
int count = NUMBER_THREADS;
for (int i = 0; i < count; i++){
List<String> colList = new ArrayList<>();
Passenger passenger = new Passenger();
String uuId = UUID.randomUUID().toString().substring(0, 8);
passenger.setName("刘翊扬_" + uuId);
passenger.setPassword("123456_" + uuId);
String json = null;
try {
json = cvsFormat(passenger);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
colList.add(json);
listList.add(colList);
}
return createCSVFile(listList, outFile, filename);
}
/***
* 获取文件的行数
* @param file
* @return
* @throws IOException
*/
public static int getTotalLines(File file) throws IOException {
long startTime = System.currentTimeMillis();
FileReader in = new FileReader(file);
LineNumberReader reader = new LineNumberReader(in);
reader.skip(Long.MAX_VALUE);
int lines = reader.getLineNumber();
reader.close();
long endTime = System.currentTimeMillis();
System.out.println("统计文件行数运行时间: " + (endTime - startTime) + "ms");
return lines;
}
}
注意:因为请求体中含有中文,所以为了避免乱码:需要保持编码一样(楼主使用的是UTF-8)
- 首先在写入csv文件设置编码。
- jemter解析csv文件时,需要设置编码
- http发送请求时,需要设置内容编码
以上三个保持一致就不会出现乱码了。
详细内容:请查看这篇文章
生成的csv,在jmx中指定需要导入csv文件的路径
我们修改JmterTest.java
类中的run方法。(在上一篇文章中)
private static void run() {
String url = "localhost";
String port = "8088";
String api = "/mongo/insert";
/** 由于csv文件的请求体列中标题是body */
String request = "${body}";
String jemterHome = "/Users/liufei/Downloads/apache-jmeter-5.3";
JMeterUtils.setJMeterHome(jemterHome);
JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir() + "/jmeter.properties");
//JMeterUtils.initLocale();
// 获取TestPlan
TestPlan testPlan = getTestPlan();
// 获取设置循环控制器
LoopController loopController = getLoopController();
// 获取Http请求信息
HTTPSamplerProxy httpSamplerProxy = getHttpSamplerProxy(url, port, api, request);
// 获取结果:如汇总报告、察看结果树
List<ResultCollector> resultCollector = getResultCollector(replayLogPath);
// 获取CVSData设置
/** === 主要是这里的变化 ====== */
File cvsPath = CVSUtils.getCsvPath();
int number = 0;
try {
// 去掉body标题列
number = CVSUtils.getTotalLines(cvsPath) - 1;
} catch (IOException e) {
e.printStackTrace();
}
CSVDataSet csvDataSet = getCSVDataSet(cvsPath.getAbsolutePath());
// 获取线程组
ThreadGroup threadGroup = getThreadGroup(loopController, number);
// 获取设置吞吐量
ConstantThroughputTimer constantThroughputTimer = getConstantThroughputTimer(20);
// 获取请求头信息
HeaderManager headerManager = getHeaderManager();
HashTree fourHashTree = new HashTree();
resultCollector.stream().forEach(item -> fourHashTree.add(item));
fourHashTree.add(headerManager);
/** 将csvDataSet添加进去 */
fourHashTree.add(csvDataSet);
HashTree thirdHashTree = new HashTree();
// 注意:设置吞吐量需要和Http请求同一级,否则无效
thirdHashTree.add(constantThroughputTimer);
thirdHashTree.add(httpSamplerProxy, fourHashTree);
HashTree secondHashTree = new HashTree();
secondHashTree.add(threadGroup, thirdHashTree);
HashTree firstTreeTestPlan = new HashTree();
firstTreeTestPlan.add(testPlan, secondHashTree);
try {
SaveService.saveTree(firstTreeTestPlan, new FileOutputStream(jmxPath));
} catch (IOException e) {
e.printStackTrace();
}
// 第一种方式:运行
StandardJMeterEngine jMeterEngine = new StandardJMeterEngine();
jMeterEngine.configure(firstTreeTestPlan);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
jMeterEngine.run();
System.out.println("运行成功!!!");
// 使用命令
/* String command = JMeterUtils.getJMeterBinDir() + "/jmeter -n -t " + jmxPath + " -l /Users/liufei/Downloads/jmter/replay_result.jtl";
Runtime.getRuntime().exec(command);
System.out.println(command);*/
}
主要修改的有三个地方:
第7行和、第29 ~ 38行、52行