Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define a list in BeanShell (jmeter)

Could anyone help with BeanShell script? So, I'm trying to use a List in my code sample, however, I couldn't define a list properly. The code like this from BeanShell PostProcessor sampler:

import java.io.*;
import java.util.*;
import org.json.*;
import org.apache.jmeter.samplers.SampleResult;


    if ((prev.getResponseCode() != null) && (prev.getResponseCode().equals("200") == true)) {
        JSONObject response = new JSONObject(prev.getResponseDataAsString());
        JSONArray array = response.getJSONArray("users");
        List<String> users_list = new ArrayList<String>();
        for(int i = 0; i < array.length(); i++)  {
            JSONObject object = array.getJSONObject(i);
            users_list.add(object.getString("user_id"));
            print(users_list);
      }
    }

jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   In file: inline evaluation of: ``import java.io.*; import java.util.*; import org.json.*; import org.apache.jmete . . . '' Encountered "=" at line 10, column 31.
like image 898
Leo Avatar asked Mar 16 '15 16:03

Leo


People also ask

What is BeanShell function in JMeter?

What Is Beanshell in JMeter? BeanShell is one of the most advanced JMeter built-in components. It supports Java syntax and extends it with scripting features like loose types, commands, and method closures.

What is the difference between BeanShell and JSR223?

Well-behaved JSR223 Sampler with Groovy language and Compilation Cache feature enabled will work faster and have lesser memory footprint than Beanshell. Remember not to reference JMeter Functions or Variables in form of ${var} directly in Groovy script body.


2 Answers

This is the simple way I got to create a list in Jmeter:

Example: creating a list in the size of the number of threads (var parameter):

import java.util.List;
import java.util.ArrayList;

list = new ArrayList();

for(int i = 0; i < ${NumThreads}; i++)  {
    list.add(i);
}
like image 94
Shai Alon Avatar answered Nov 01 '22 20:11

Shai Alon


finally, solved as list = new ArrayList();

like image 25
Leo Avatar answered Nov 01 '22 20:11

Leo