在ApacheJMeter中为每个HTTP请求发送新数据
我有一个包含身体数据的HTTP采样器:在ApacheJMeter中为每个HTTP请求发送新数据,apache,http,scripting,jmeter,httprequest,Apache,Http,Scripting,Jmeter,Httprequest,我有一个包含身体数据的HTTP采样器: { "voice": "Nancy", "basic": "sad", "type": "basic", "text": "She fell." } 我将线程数设置为50,将爬升周期设置为10秒。 我有一组句子需要在“文本”字段中填写 总数:50(每个请求一个) 也就是说,当启动一个新请求时,我需要在“text”字段中添加一个新句子 如何处理这种情况是使用BeanShell脚本还是uuid???使用以下采样器将“一次性控制器”添加到测试计划中,以初始化文
{
"voice": "Nancy",
"basic": "sad",
"type": "basic",
"text": "She fell."
}
我将线程数设置为50,将爬升周期设置为10秒。
我有一组句子需要在“文本”字段中填写
总数:50(每个请求一个)
也就是说,当启动一个新请求时,我需要在“text”字段中添加一个新句子
如何处理这种情况是使用BeanShell脚本还是uuid???使用以下采样器将“一次性控制器”添加到测试计划中,以初始化文件读取
-->比恩希尔取样器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"));
-->比恩希尔取样器2
${__CSVRead(${CWD}/<<yourfilename.csv>>,*hMyText)}
${__CSVRead(*hMyText,next)}
然后将http采样器中的请求主体替换为
{
"voice": "Nancy",
"basic": "sad",
"type": "basic",
"text": "${mynewtext}"
}
您不需要任何脚本,只需使用以下函数:
{
"voice": "Nancy",
"basic": "sad",
"type": "basic",
"text": "${__StringFromFile(/path/to/file/with/text.txt,,,)}"
}
有关此JMeter函数和其他JMeter函数的更多信息,请参阅posts系列
如果文件的列数大于1,则使用该列可能更可行 另一个简单的方法是使用Config元素->“CSV数据集配置”。 将所有50个句子放在文本文件中的任意路径中,如D:/myfile.txt
现在在CSV数据集配置中,设置文件名ie complete location,在请求参数中定义变量名为“myvalue”,并将参数传递为${myvalue}。我们可以通过多种方式解决此问题,下面是两种方式 1.使用“\uuuRandomString()” 2.使用BeanShell程序 3.使用“随机变量”和历元时间(配置元素->随机变量) 建议的选项为1和3
{
"voice": "Nancy",
"basic": "sad",
"type": "basic",
"text":"${__RandomString(32,abcdefghijklmnopqrstvuwxyz,0123456789)}",
}vars.put("mynewtext", "${__CSVRead(*hMyText,0)}");
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);
对于源代码,请单击此处我尝试了此函数,但它可以正常工作,但它会随机挑选字符串,但不是按特定顺序挑选。有关于如何使字符串有序的帮助吗?每次调用它时,它都会从文件中挑选下一个字符串,值不应该是随机的。
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);
{
"voice": "Nancy",
"basic": "sad",
"type": "basic",
"text":"${random_variable}"
}
{
"voice": "Nancy",
"basic": "sad",
"type": "basic",
"text":"perf text ${__javaScript((new Date().getTime()))}_${UUID}@c1.dev"
}