【问题标题】:Send new data for each HTTP Request in Apache JMeter在 Apache JMeter 中为每个 HTTP 请求发送新数据
【发布时间】:2017-02-02 17:15:19
【问题描述】:

我有一个包含正文数据的 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??

【问题讨论】:

    标签: apache http scripting jmeter httprequest


    【解决方案1】:

    使用以下采样器在您的测试计划中添加“一次性控制器”以初始化读取的文件

    --> 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 采样器中的请求正文替换为

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

    【讨论】:

      【解决方案2】:

      您不需要任何脚本,只需使用 __StringFromFile() 函数,例如:

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

      有关此功能和其他 JMeter 功能的更多信息,请参阅How to Use JMeter Functions 系列帖子。

      如果您的文件有 > 1 列,则使用CSV Data Set Config 可能更可行。

      【讨论】:

      • 我试过这个函数,它可以工作,但它随机挑选字符串,但不是按特定顺序。任何帮助如何按顺序排列字符串?
      • 每次调用时都会从文件中选择下一个字符串,值不应该是随机的。
      【解决方案3】:

      另一种简单的方法是使用配置元素->“CSV 数据集配置”。 将所有 50 个句子放在任何路径中的文本文件中,例如 D:/myfile.txt 现在在 CSV 数据集配置中,设置文件名,即完整位置,将变量名定义为“myvalue”,并在请求参数中将参数传递为 ${myvalue}。

      【讨论】:

        【解决方案4】:

        我们可以通过多种方式解决这个问题,以下是两种方式 1. 使用“__RandomString()” 2.使用BeanShell程序 3.使用“随机变量”和纪元时间(配置元素->随机变量)

        推荐的选项是 1 和 3

        1. "使用"__RandomString()":下面是代码sn-p

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

          }

        2 。使用 BeanShell 程序:下面是使用 beanshell Preprocessor 的代码 sn-p

        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);
        

        第二步:在“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"
        }
        

        来源请点这里enter link description here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-12
          • 1970-01-01
          相关资源
          最近更新 更多