简体   繁体   English

在JMeter中的BeanShell Sampler中将字符串解析为整数

[英]Parsing string to integer in BeanShell Sampler in JMeter

I'm trying to parse a string into integer in JMeter but failed due to following error. 我正在尝试在JMeter中将字符串解析为整数但由于跟随错误而失败。 If I try to print the strings returned by vars.get, they look good. 如果我尝试打印vars.get返回的字符串,它们看起来很好。

2014/06/28 00:08:52 WARN  - jmeter.assertions.BeanShellAssertion: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval  Sourced file: inline evaluation of: ``if (ResponseCode != null && ResponseCode.equals ("200") == false ) {  int i = In . . . '' : Typed variable declaration : Method Invocation Integer.parseInt 

Following is my code 以下是我的代码

if (ResponseCode != null && ResponseCode.equals ("200") == false )
{
    int i = Integer.parseInt(vars.get("currentPMCount"));
    int j = Integer.parseInt(vars.get("pmViolationMaxCount"));
    if( i > j ){
        log.warn("PM count on server is greater than max allowed count.");
        }

        log.warn( "The return code is " + ResponseCode); // this goes to the JMeter log file
} 
else 
{
    Failure=true ;
    FailureMessage = "The response data size was not as expected" ;   
}

Your code looks good however it can be a problem with currentPMCount and/or pmViolationMaxCount variables. 您的代码看起来不错,但它可能是currentPMCount和/或pmViolationMaxCount变量的问题。

If they really look good and look like Integers and don't exceed maximum/minimum values of Integer you can try the following: 如果它们看起来很好并且看起来像整数并且不超过Integer的最大/最小值,您可以尝试以下方法:

  1. Make sure that there are no "space" characters around number value as leading or trailing space will cause conversion failure. 确保数字值周围没有“空格”字符,因为前导或尾随空格会导致转换失败。 Perhaps invoking trim() method on variable can help: 也许在变量上调用trim()方法可以帮助:

     int i = Integer.parseInt(vars.get("currentPMCount").trim()); 
  2. If you store your script into a file and then provide path to the file in Beanshell Assertion you'll get "problematic" line number 如果将脚本存储到文件中,然后在Beanshell断言中提供文件路径,则会出现“有问题”的行号
  3. My favourite: surround your code into try/catch block as follows: 我最喜欢的:将代码包围到try / catch块中,如下所示:

     try{ //your code here } catch (Exception ex){ log.warn("Error in my script", ex); throw ex; // elsewise JMeter will "swallow" the above exception } 

This way you'll get more informative stacktrace instead of lousy Error invoking bsh method message which tells nothing. 通过这种方式,您将获得更多信息性的堆栈跟踪而不是糟糕的Error invoking bsh method消息,它什么都不说。

See How to use BeanShell: JMeter's favorite built-in component guide for more tips and tricks. 请参阅如何使用BeanShell:JMeter最喜欢的内置组件指南,以获取更多提示和技巧。

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

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