JMeter上傳文件壓力測試


1.新建測試計划

測試計划頁並設置用戶變量,第一次初始上傳的文件名稱、文件路徑

 

2.添加HTTP信息管理

 

如下圖: 加入Cookie值

 

 

 

3.BeanShell preprocessor請求文件數據預處理
難點:文件壓力測試,每次上傳的文件必須不同
解決方式:(1)文件進行每次插入新數據,保存為新文件1M_Sensitive+新生成的隨機數.csv

 

 

 BeanShell代碼如下:

import java.io.*;
import java.util.Random;

String filePath=vars.get("Path");
String fileName=vars.get("Name");
System.out.println(filePath+" This is origin file path!");
System.out.println(fileName+" This is origin file name!");

try {
File file = new File(filePath+"\\temp");
//判斷文件不為null或文件目錄存在
if (!file.exists()){
file.mkdir();//創建文件夾
}else if(file.list().length==0){
System.out.println("文件夾為空,無需初始化刪除");
}else{
//取得這個目錄下的所有子文件對象
File[] files = file.listFiles();
//遍歷該目錄下的文件對象
for (File f: files){
f.delete();
}
}
Random ra =new Random();
int num=ra.nextInt(10000)+1;
long r1 = System.currentTimeMillis();
//File csvFile = new File("D:\\Work\\TestData\\Excel\\testData_2_sensitive.xlsx"); // CSV數據文件
String csvFile = filePath+"\\"+fileName;
BufferedReader br = new BufferedReader(new FileReader(csvFile)); // 附加

String newFileName = filePath+"\\temp\\"+fileName.split("\\.")[0]+(ra.nextInt(10000)+1)+"."+fileName.split("\\.")[1];
BufferedWriter bw = new BufferedWriter(new FileWriter(newFileName, true)); // 附加

int bufferSize = 100;
char[] chars = new char[bufferSize];
int read = 0;
while ((read = br.read(chars, 0, bufferSize)) != -1) {
bw.write(chars, 0, read);
}

// 添加新的數據行
bw.append("Good lucky!" + r1);
vars.put("fileList",String.valueOf(newFileName));
System.out.println(newFileName+" This is new file name!");
bw.close();
br.close();
} catch (FileNotFoundException e) {
// File對象的創建過程中的異常捕獲
e.printStackTrace();
} catch (IOException e) {
// BufferedWriter在關閉對象捕捉異常
e.printStackTrace();
}

4.添加HTTP請求
(1)設置http協議:https、服務器IP、請求方式、路徑

(2)重點設置參數:a.設置UserId, 傳參形式必須改成form-data (如下圖)

 

 

 

b.設置文件上傳參數(如下圖)

 

 

5.添加響應斷言

 

 

6.添加查看結果樹、聚合報告進行數據監控

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM