I have the below requirement :-

  1. I have the User CSV file in which the Username & password is defined with which I have create all the users from the CSV file.
  2. Now I have couple of thread groups defined for different application pages which 'll have different API's.
  3. For all the thread groups, I have different no of threads assigned .(Let's say ,for thread group1 ,I have 5 threads from User CSV file which can be from line 1 to line 5 & thread group2 can have 3 threads which can go from line 6 to line 8 & so on). I want to get the user & password details from above mentioned file which requires only user name & password from the file.
  4. for all the users which are used in thread group 'll generate the token for each & every user which I want to append in a file (if possible, can we append it in same user file against the each & every user) & then later I want to use it for other activities.

2nd requirement :-

  1. I have a API call in which I am getting a total_count in response header (let say 3500).
  2. I want to compare the this count with any specific no either defined as variable (let say ${max_count} = 5000)
  3. Now I want to iterate the loop with a condition say : if total_count < ${max_count}, then loop should iterate for max_count else :- loop count should iterate for total_count only.

How can I do that with bean shell processor.

The solution I have tried :

  1. for the 1st point , I went with set-up thread which should be the 1st thread to be run.

  2. for 2nd & 3rd point, I have defined the thread groups with different CSV files for each thread (which I don't want as solution), as it may go for more than 100 csv files to be uploaded & used as the no of threads in each group are not identical .

  3. for point 4, I haven't got any solution for it & same is the case for 2nd requirement.

1

There are 1 best solutions below

1
Dmitri T On

First of all Beanshell scripting is some form of a performance anti-pattern, since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting. See Apache Groovy: What Is Groovy Used For? for more information on Groovy scripting in Jmeter.

Example code implementing the "below requirement" would be something like this:

def engine = ctx.getEngine();
def test = engine.getClass().getDeclaredField("test");
test.setAccessible(true);

def testPlanTree = test.get(engine)

def threadGroupSearch = new org.apache.jorphan.collections.SearchByClass<>(org.apache.jmeter.threads.ThreadGroup.class)
testPlanTree.traverse(threadGroupSearch)
def threadGroups = threadGroupSearch.getSearchResults()

def threadGroupIndex = ((ctx.getThread().getThreadName() =~ ctx.getThreadGroup().getName() + ' (\\d+)')[0][1]) as int

int offset = 0

0.upto(threadGroupIndex - 1, {
    if (threadGroupIndex > 1) {
        offset += threadGroups.toArray()[it - 1].getNumThreads()
    }
})

def threadNum = 0
if (threadGroupIndex == 1) {
    threadNum = ctx.getThread().getThreadNum()
} else {
    threadNum = ctx.getThread().getThreadNum() - 1
}
def lineFromCSV = offset + threadNum
def line = new File('test.csv').readLines().get(lineFromCSV)

//do what you need with this "line" here

With regards to "2nd requirement" you can put the following __groovy() function into your Loop Controller's "Loop Count" input:

${__groovy((vars.get('total_count') as int) < (vars.get('max_count') as int) ? vars.get('max_count') : vars.get('total_count'),)}