Jmeter while controller doesn't seem to evaluate variables as numbers

青春壹個敷衍的年華 提交于 2019-12-10 21:06:26

问题


I am writing a jmeter script that keeps loading data until a table reaches a specified size. I have a while loop, in which I have one HTTP Sampler to loads the data, then another HTTP Sampler with an XPath Post-processor to check the table size (they call two different APIs). The reference variable of the XPath Post Processor is currentSize and I have a user defined variable maxSize, but using ${currentSize} < ${maxSize} as the condition for the while loop creates an infinite loop.

Thinking maybe the problem is that the output of XPath is a string, I've tried doing various things in beanshell to coerce it to a number, but I'm a beanshell noob, so I haven't been successful with that either. Can anyone guide me about how to get jmeter to recognize a variable as a number? (Preferably a decimal, but if I have to round to an int, I can live with that.)

Thanks!


回答1:


I think using __javascript(parseInt()) should suffice for you to check the condition.

e.g.

${__javaScript(parseInt(${time_elapsed_string}) < parseInt(${duration}))}




回答2:


Assuming that you have following variables:

  • currentSize
  • maxSize
  • continue

where continue is set via User Defined Variables and has the value of true

You can use following Beanshell code to check if current size is equal or greater than maximum size:

import java.math.BigDecimal;

String currentSize = vars.get("currentSize");
String maxSize = vars.get("maxSize");

BigDecimal currentSizeNumber = new BigDecimal(currentSize);
BigDecimal maxSizeNumber = new BigDecimal(maxSize);

if (currentSizeNumber.compareTo(maxSizeNumber) > -1){
    vars.put("continue", "false");
}

Make sure that following criteria are met:

  1. Your While Controller has ${continue} as a condition
  2. Beanshell Sampler, Pre / Post Processor or Assertion with the code above is added as a child of the While Controller

See How to use BeanShell guide for more details and kind of Beanshell cookbook.

Everything should work this way.

Hope this helps.



来源:https://stackoverflow.com/questions/22621792/jmeter-while-controller-doesnt-seem-to-evaluate-variables-as-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!