需求
進(jìn)行一個(gè)post請求接口的壓測,有如下需求
- 請求的header中,time取系統(tǒng)當(dāng)前時(shí)間,token取time的md5值
- 請求的body是JSON格式,body中含有一個(gè)數(shù)組,壓測要求數(shù)組按照最大長度5000來構(gòu)造
- 原始的csv文件大約40萬條數(shù)據(jù),每一行標(biāo)識一條數(shù)據(jù)。實(shí)際請求的body數(shù)組來源于這里
經(jīng)過分析,該需求可以通過Jmeter來實(shí)現(xiàn)
背景知識
BeanShell與Java的關(guān)系
Jmeter的BeanShell PreProcessoBeanShell組件實(shí)質(zhì)上是一個(gè)Java源代碼解釋器,也就是說我們從Intellij Idea中寫的代碼,是可以拷貝到beanshell中直接執(zhí)行的
當(dāng)缺少類時(shí)
歸根結(jié)底,BeanShell是一個(gè)小型的Java源代碼解釋器,使用外部類的時(shí)候,也需要導(dǎo)入該類。具體的說就是缺少import中涉及的lib包,不過這個(gè)不算什么大問題,可以通過下載bundle放到j(luò)meter的ext/lib中解決
以本文想要組裝成Json body為例,需要使用到j(luò)son的lib,但是直接在jmeter中使用,會報(bào)錯(cuò),找不到這個(gè)對象
import org.json.JSONArray;
import org.json.JSONObject;
讓jmeter支持的方法是
1. 訪問https://mvnrepository.com/
2. 選擇一個(gè)最合適的版本,點(diǎn)擊下載bundle文件
3. 將該jar包放到j(luò)meter安裝目錄lib/ext下,并重新啟動jmeter

image.png

image.png
JMX文件的執(zhí)行流程
建立3個(gè)線程組,順次執(zhí)行
線程組1:大文件切分
新建線程組 -> 新建HTTP Request -> 新建BeanShell PreProcessor
import java.io.*;
String targetFile = "D:/test/aaa.csv";//源文件目錄
String saveDir = "D:/test";//分割后文件目錄
long splitSize = 5000;//每個(gè)文件記錄條數(shù)
String saveFileName = "target";//分割后文件名開頭
String suffix = "csv";//分割后文件格式后綴
public static void splitFile(String targetFile, String saveDir , String saveFileName, String suffix,long splitSize) throws Exception {
if( !saveDir.endsWith("\\") ){
saveDir += File.separator;
}
File file = new File(targetFile);
if (!file.exists()) {
throw new Exception("目標(biāo)路徑:[ " + targetFile + " ] 有錯(cuò)誤...");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String str = null;
long len = 0;
log.info("開始寫入......請等待......");
long startTime = System.currentTimeMillis();
BufferedWriter writer = null;
while ((str = reader.readLine()) != null) {
long txtSize = (len / splitSize) + 1;
String fileName = saveDir + saveFileName + txtSize + "." + suffix;
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true)));
writer.write(str + System.lineSeparator() );
writer.flush();
len ++;
writer.close();
}
reader.close();
log.info("寫入完畢,一共 " + len + " 記錄,耗時(shí):" + ( System.currentTimeMillis() - startTime ) / 1000 + " s" );
}
try {
splitFile(targetFile, saveDir, saveFileName, suffix, splitSize); //調(diào)用文件分割方法
} catch (Exception e) {
e.printStackTrace();
}
線程組2:組裝body
基于上一步生成的小文件,每個(gè)小文件組裝成一條請求的body,并將所有的body寫入最終的壓測文件中
新建線程組 -> 新建HTTP Request -> 新建BeanShell PreProcessor
import org.apache.commons.io.FileUtils;
import java.io.File;
import org.json.JSONArray;
import org.json.JSONObject;
// 指定文件夾路徑
String folderPath = "D:/test";
// 指定新文件路徑
String newFilePath = "D:/test/rawbody";
// 獲取文件夾下的所有文件
File folder = new File(folderPath);
File[] files = folder.listFiles();
// 創(chuàng)建新文件
File newFile = new File(newFilePath);
// 遍歷每個(gè)文件并讀取內(nèi)容
for (File file : files) {
// 讀取文件內(nèi)容
boolean yes = file.getName().startsWith("target");
if(!yes){
continue;
}
JSONObject json;
try{
FileInputStream inputStream = new FileInputStream(folderPath+"/"+file.getName());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String str = null;
json = new JSONObject();
json.put("reqId", "test_" + UUID.randomUUID());
JSONArray ids = new JSONArray();
while((str = bufferedReader.readLine()) != null)
{
String[] deviceIds = str.split(",");
String deviceIdType = "test";
String deviceId = deviceIds[0];
JSONObject item = new JSONObject();
item.put("deviceId", deviceId);
item.put("deviceType", deviceIdType);
ids.put(item);
}
json.put("deviceInfos", ids);
//close
inputStream.close();
bufferedReader.close();
}catch (Exception e){
e.printStackTrace();
}
// 寫入新文件
FileUtils.writeStringToFile(newFile, json.toString()+"\n", "UTF-8", true);
}
線程組3:發(fā)送壓測請求

壓測線程組的示例
- 新建User Defined Variables,用于配置變量和引用
- 新建HTTP Header Manager,按照如下設(shè)置time和token
time設(shè)置為${__time(,ts)},token設(shè)置為${__MD5(${ts})}
- 新建CSV Data Set Config,變量名稱設(shè)置為mybody
- 新建HTTP Request,配置為POST請求,消息體數(shù)據(jù)為${mybody}
完成上述配置后,可以設(shè)置線程數(shù)和循環(huán)次數(shù),發(fā)送壓測請求了