Andreas Lassak
Andreas Lassak

Reputation: 23

JMeter Beanshell Integer Error

i have problem with Integer in JMeter Beanshell Sampler/Pre/Post processor. I have some array of values, and i want to use each value of this array to set JMeterProperty for next use.

GOAL> have some N rows from query. For Example UPSTREAM column has diff values and i need it save them for next use. Because i will use them for call CMD.exe as parameters by "OS Processes Sampler".

So, if UPSTREAM_1=XXX, UPSTREAM_2=AAA, 
CMD.EXE will looks like: CMD.EXE -upstream_1 -upstream_2, etc.

From SQL i got Array of values

COUNT=31
UPSTREAM_#=31
UPSTREAM_1=XXX
UPSTREAM_2=AAA
....

In PostProcessor i set:

${__setProperty(COUNT, ${COUNT_1})};

I am trying this scrpit:

import java.util.*;
import java.text.*; 
import java.io.*;

int max = Integer.parseInt(vars.get(${COUNT_1})); //--COUNT=31, Integer doesnt work
int n = vars.get(${COUNT_1}); //--COUNT=31, this also doesnt work

for (int i=1;i<=n;i++)
{
   ${__setProperty(UPSTREAM_i, ${UPSTREAM_i})};
}

But JMeter log say that it dont know the "INT"

2016/10/03 14:52:13 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   Sourced file: inline evaluation of: `` import java.util.*; import java.text.*;  import java.io.*;  int max = Integer.p . . . '' : Typed variable declaration : Error in method invocation: Method get( int ) not found in class'org.apache.jmeter.threads.JMeterVariables' 
2016/10/03 14:52:13 WARN  - jmeter.protocol.java.sampler.BeanShellSampler: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: `` import java.util.*; import java.text.*;  import java.io.*;  int max = Integer.p . . . '' : Typed variable declaration : Error in method invocation: Method get( int ) not found in class'org.apache.jmeter.threads.JMeterVariables' 

Can somebody help me? Maybe i miss some libs or something. JRE/JDK i have.


UPDATE_1

User Variables all:

COUNT   ${__property(COUNT)}    Count of Rows from SQL Query

Test_Plan:

enter image description here

PostProcessor Log:

SamplerProperties:
variableNames=DATE,DOWNSTREAM,UPSTREAM,COUNT,etc.

JMeterVariables:
COUNT=31
COUNT_#=31
COUNT_1=31
COUNT_2=31
.....
DATE_#=31
DATE_1=04.10.2016
DATE_2=04.10.2016
.....
DOWNSTREAM_#=31
DOWNSTREAM_1=DDD11
DOWNSTREAM_2=DDD11
.....
UPSTREAM_#=31
UPSTREAM_1=XXX
UPSTREAM_2=AAA
....
JMeterProperties:
COUNT= 31
DATE= 04.10.2016
DOWNSTREAM= DDD11
TEST= 1

Debug Loop log:

SamplerProperties:
BeanShellSampler.query=import java.util.*;
 import java.text.*; 
 import java.io.*;
 int n = Integer.parseInt(vars.get("COUNT")); 
 for (int i=1;i<=n;i++)
 {
      props.setProperty("UPSTREAM_"+i, vars.get("UPSTREAM_i"));
 }
    JMeterVariables:
    COUNT=31
    COUNT_#=31
    COUNT_1=31
    COUNT_2=31
    .....
    DATE_#=31
    DATE_1=04.10.2016
    DATE_2=04.10.2016
    .....
    DOWNSTREAM_#=31
    DOWNSTREAM_1=DDD11
    DOWNSTREAM_2=DDD11
    .....
    UPSTREAM_#=31
    UPSTREAM_1=XXX
    UPSTREAM_2=AAA
    ....
    JMeterProperties:
    COUNT= 31
    DATE= 04.10.2016
    DOWNSTREAM= DDD11
    TEST= 1

This is in Jmeter log now:

2016/10/03 17:12:24 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``import java.util.*;  import java.text.*;   import java.io.*;  int n = Integer.pa . . . '' : Method Invocation props.setProperty 
2016/10/03 17:12:24 WARN  - jmeter.protocol.java.sampler.BeanShellSampler: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.util.*;  import java.text.*;   import java.io.*;  int n = Integer.pa . . . '' : Method Invocation props.setProperty 

UPDATE_2

UBIK LOAD PACK's solution works :), GOAL updated.

Upvotes: 2

Views: 6441

Answers (2)

Dmitri T
Dmitri T

Reputation: 168092

I don't know what you are trying to achieve, there are multiple problems with your approach.

  1. ${__setProperty(COUNT, ${COUNT_1})};

                          ^ here you set an extra space before the value
    

    Also to be totally clear, vars and props are different beasts and they don't have anything in common. If you set a property via __setProperty function, you won't be able to access it via vars.get(), you will have to use props.get() instead.

  2. vars.get(${COUNT_1}) won't work, you should have to change it to one of the following:

    • `vars.get("COUNT_1");
    • `${COUNT_1}

    In general it is not recommended to inline JMeter Functions and Variables into Beanshell (and other) scripts as they may be interpreted differently depending on their value.

  3. With regards to

    Method get( int ) not found in class'org.apache.jmeter.threads.JMeterVariables'

    I don't know how you managed to put an integer into JMeter Variables, but if you did, you should be accessing it via vars.getObject() function like

    int n = vars.getObject("COUNT");
    
  4. Troubleshooting tip: surround your code with the try block, you will be able to get a way more informative error messages in jmeter.log file like:

    try {
        //your code here
    }
    catch (Throwable ex) {
        log.error("something wrong", ex);
        throw ex;
    }
    

I would also recommend getting familiarized with Debugging JDBC Sampler Results in JMeter guide

Upvotes: 0

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34536

Your Beanshell script has many errors:

 import java.util.*;
 import java.text.*; 
 import java.io.*;
 int n = Integer.parseInt(vars.get("COUNT")); 
 for (int i=1;i<=n;i++)
 {
      props.setProperty("UPSTREAM_"+i, vars.get("UPSTREAM_"+i));
 }

Note what you are doing is setting in props (global) what you get from vars (specific to each user) , so this may be wrong.

Read:

Upvotes: 0

Related Questions