簡體   English   中英

為Apache JMeter中的每個HTTP請求發送新數據

[英]Send new data for each HTTP Request in Apache JMeter

我有一個包含正文數據的HTTP采樣器:

{
"voice": "Nancy",
"basic": "sad",
"type": "basic",
"text": "She fell."
}

我將線程數設置為50,並將加速時間設置為10秒。 我有一組句子需要在“文本”字段中填寫。

She had your dark suit in greasy washwater all year.
Don't ask me to carry an oily rag like that.
This was easy for me.
Jane may earn more money by working hard.
She is thinner than I am.
Bright sunshine shimmers on the ocean.
......... another 42 sentences 

總計:50(每個請求一個)

那就是當一個新的請求被觸發時,我需要一個新的句子在“文本”字段中。

如何使用BeanShell腳本或uuid處理這種情況?

使用以下采樣器將“僅控制器”添加到您的測試計划中,以初始化讀取的文件

-> Beanshell采樣器1

import org.apache.jmeter.services.FileServer; 
//String sCWd = new String(FileServer.getFileServer().getBaseDir().replace("\\\\", "/"));
String sCWd = new String(FileServer.getFileServer().getBaseDir());
log.info("CWDString=" + sCWd.replace("\\", "/"));

vars.put("CWD", sCWd.replace("\\\\", "/"));
log.info("CWD=" + vars.get("CWD"));

-> Beanshell采樣器2

${__CSVRead(${CWD}/<<yourfilename.csv>>,*hMyText)}
${__CSVRead(*hMyText,next)}

然后在您的主要HTTP請求采樣器下,添加一個beanshell預處理器

HTTP采樣器

-> Beanshell預處理器

vars.put("mynewtext", "${__CSVRead(*hMyText,0)}");

然后將您的請求正文替換為http sampler

{
"voice": "Nancy",
"basic": "sad",
"type": "basic",
"text": "${mynewtext}"
}

您不需要任何腳本,只需使用__StringFromFile()函數即可:

{
  "voice": "Nancy",
  "basic": "sad",
  "type": "basic",
  "text": "${__StringFromFile(/path/to/file/with/text.txt,,,)}"
}

有關此和其他JMeter函數的更多信息,請參見如何使用JMeter函數帖子系列。

如果文件的列數> 1,則使用CSV數據集配置可能更可行。

另一種簡單的方法是使用“配置元素”->“ CSV數據集配置”。 將所有50個句子放在文本文件中的任何路徑下,例如D:/myfile.txt。現在,在CSV數據集配置中,設置文件名(即完整位置),將變量名定義為“ myvalue”,並在請求參數中將參數傳遞為$ {myvalue}。

我們可以通過多種方式解決此問題,以下是兩種方式:1.使用“ __RandomString()” 2.使用BeanShell程序3.使用“隨機變量”和紀元時間(配置元素->隨機變量)

推薦的選項是1和3

  1. “使用“ __RandomString()”:以下是代碼段

     { "voice": "Nancy", "basic": "sad", "type": "basic", "text":"${__RandomString(32,abcdefghijklmnopqrstvuwxyz,0123456789)}", 

    }

2。 使用BeanShell程序:以下是使用beanshell預處理程序的代碼段

Step 1: Add "Beanshell preprocessor" to "Http" sampler as child

import java.util.Random;
  String str="abcdefghijklmnopqrst1234567890";
  int String_Length=32;
  String randomSting="";
  for(int i=1;i<=String_Length;i++){
   Random randomVal=new Random();
   int randomInt=randomVal.nextInt(str.length());
   randomSting+=str.substring(randomInt, randomInt+1);
   }     
vars.put("random_variable",randomSting);

步驟2:在“ Http Sampler”中調用random_variable(在beanshell預處理器中定義),如下所示

{
  "voice": "Nancy",
  "basic": "sad",
  "type": "basic",
  "text":"${random_variable}"
  }
  1. 將“隨機變量”添加為“ http采樣器”作為子代,並在http采樣器中調用隨機變量名稱(是UUID) 在此處輸入圖片說明
 { "voice": "Nancy", "basic": "sad", "type": "basic", "text":"perf text ${__javaScript((new Date().getTime()))}_${UUID}@c1.dev" } 

對於源,請單擊此處在此處輸入鏈接描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM