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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24462367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:20:26  来源:igfitidea点击:

Parsing string to integer in BeanShell Sampler in JMeter

javastringintegerjmeterbeanshell

提问by TechCrunch

I'm trying to parse a string into integer in JMeter but failed due to following error. If I try to print the strings returned by vars.get, they look good.

我试图在 JMeter 中将字符串解析为整数,但由于以下错误而失败。如果我尝试打印 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" ;   
}

采纳答案by Dmitri T

Your code looks good however it can be a problem with currentPMCountand/or pmViolationMaxCountvariables.

您的代码看起来不错,但是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:

如果它们真的看起来不错并且看起来像整数并且不超过整数的最大值/最小值,则可以尝试以下操作:

  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:

    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
  3. My favourite: surround your code into try/catch block as follows:

    try{
        //your code here
    }
    catch (Exception ex){
        log.warn("Error in my script", ex);
        throw ex; // elsewise JMeter will "swallow" the above exception
    }
    
  1. 确保数字值周围没有“空格”字符,因为前导或尾随空格将导致转换失败。也许trim()在变量上调用方法可以帮助:

    int i = Integer.parseInt(vars.get("currentPMCount").trim());
    
  2. 如果您将脚本存储到一个文件中,然后在 Beanshell 断言中提供该文件的路径,您将获得“有问题”的行号
  3. 我最喜欢的:将您的代码包围在 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 methodmessage which tells nothing.

通过这种方式,您将获得更多信息丰富的堆栈跟踪,而不是Error invoking bsh method一无所获的糟糕消息。

See How to use BeanShell: JMeter's favorite built-in componentguide for more tips and tricks.

有关更多提示和技巧,请参阅如何使用 BeanShell:JMeter 最喜欢的内置组件指南。