简体   繁体   English

如何正确使用 hashmap 和 JMeter 中的属性

[英]How to correct use hashmap and properties in JMeter

I faced the next problem.我面临下一个问题。 I have a.txt file, which contains data in the format:我有一个 .txt 文件,其中包含以下格式的数据:

"name"="value" “名称”=“值”

True data as example:真实数据为例:

counter=10
scheduleNewPeriodEnd=2100-12-31
scheduleNewPeriodStart=2100-01-01

And i took this code as example and trying to convert it to hashmap with JSR223 Sampler or Beanshell Sampler.以此代码为例,并尝试使用 JSR223 Sampler 或 Beanshell Sampler 将其转换为 hashmap。 Then i want to put some value from map to properties for use it in next thread with requests:然后我想将 map 中的一些值放到属性中,以便在下一个线程中使用请求:

    String filePath = "/soap/otherData.txt";
    String counter = ""; 
    String scheduleNewPeriodEnd = "";
    String scheduleNewPeriodStart = "";
    String arSchruleTypeId = "";
    String sarSchruleTypeId = "";

    HashMap map = new HashMap();
   
    String line;
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    while ((line = reader.readLine()) != null)
    {
        String[] parts = line.split("=", 2);
        if (parts.length >= 2)
        {
            String key = parts[0];
            String value = parts[1];
            map.put(key, value);
        } else {
            System.out.println("ignoring line: " + line);
        }
    }
   
    counter = map.get("counter");
    scheduleNewPeriodStart = map.get("scheduleNewPeriodStart");
    scheduleNewPeriodEnd = map.get("scheduleNewPeriodEnd");
    arSchruleTypeId = map.get("arSchruleTypeId");
    sarSchruleTypeId = map.get("sarSchruleTypeId");

    props.put(schPerStart,  scheduleNewPeriodStart);
    
    reader.close();

But it doesn't work.但它不起作用。 When i want to see what is props contains - the actual result this:当我想查看 props 包含的内容时 - 实际结果如下:

log.info("Property schPerStart is:  " + props.get("schPerStart"));

INFO o.a.j.u.BeanShellTestElement: Property schPerStart is:  ${schStart}

Expected result should be:预期结果应该是:

INFO o.a.j.u.BeanShellTestElement: Property schPerStart is: 2100-01-01

So i haven't any variable with name ${schStart} and don't understand why JMeter put it into props.所以我没有任何名称为 ${schStart} 的变量,也不明白为什么 JMeter 将它放入道具中。 The java code was tested in Idea and saving values to the map works correctly. java 代码在 Idea 中进行了测试,将值保存到 map 可以正常工作。 Is it i doing something wrong?是我做错了什么吗?

Question resolved.问题已解决。 Problem was that i wrapped code into class and he didn't call there.问题是我将代码包装到 class 并且他没有打电话给那里。 I just deleted class and method wrappers and it works fine now.我刚刚删除了 class 和方法包装器,现在它工作正常。

The problem is in the only line authored by you, just surround your schPerStart with quotation marks and the "code" should start working as you expect.问题出在您编写的唯一一行中,只需用引号将您的schPerStart括起来,“代码”应该开始按您的预期工作。

props.put("schPerStart",  scheduleNewPeriodStart);

Also be aware that starting from JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting as Groovy performance is much better comparing to Beanshell, see Apache Groovy - Why and How You Should Use It article for more details. Also be aware that starting from JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting as Groovy performance is much better comparing to Beanshell, see Apache Groovy - Why and How You Should Use It article for more details.

You won't have to change a single line, just copy and paste the whole snippet into the relevantJSR223 Test Element and enjoy (unless you would like to make your code more " groovy ")您不必更改一行,只需将整个代码段复制并粘贴到相关的JSR223 测试元素中即可享受(除非您想让您的代码更“ groovy ”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM