Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. If I try to print the strings returned by vars.get, they look good.

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" ;   
}
like image 909
TechCrunch Avatar asked Jun 28 '14 00:06

TechCrunch


Video Answer


1 Answers

Your code looks good however it can be a problem with currentPMCount and/or pmViolationMaxCount variables.

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
    }
    

This way you'll get more informative stacktrace instead of lousy Error invoking bsh methodmessage which tells nothing.

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

like image 160
Dmitri T Avatar answered Oct 19 '22 03:10

Dmitri T