I have a test that loops as suggested here:
Is it possible to loop a test in JMeter?
I have a Thread Group with 100 users and a loop count of 5.
A Runtime Controller to run for 30 seconds.
Now when the Runtime Controller finishes I would like to increment a variable that I can read inside my BeanShell sampler in my test. At the end of the test this variable should be equal to the loop count.
Ok figured it out!
I used a Counter element and set it to "Track counter independently for each user".
The variable increments only after each loop.
Also very important the Counter has to be under Thread Group but not inside the Runtime Controller.
Since you use Beanshell you can access current loop number as simple as
vars.getIteration();
See JMeterVariables class JavaDoc to see what else could be done using it, How to use BeanShell: JMeter's favorite built-in component guide for advanced information on Beanshell scripting in Apache JMeter and remember that the method will work only on Thread Group level, the value won't increment inside Loop Controller
Related
I am developing a Test Harness, for which some on here have assisted, so great thanks to you all.
But as part of my Test Harness journey (and stupidity along it) I wanted to know what exactly was being sent when a Thread Group is started, specifically, the ThreadGroup's 'Thread Properties' values for the:
Loop Count: Infinite checkbox
Specify Thread lifetime checkbox
Is it possible to get and manipulate the values of the fields via say JSR223 or Beanshell elements as I would like to manipulate them at runtime?
As of JMeter 5.5 there is no possibility to change these parameters in the runtime
The options are in:
Use JMeter Properties to pass the values from file or command-line arguments when you start the test
Basically the same as point 1 but if you need i.e. more complex calculations you can amend the properties somewhere in setUp Thread Group using suitable JSR223 Test Element (using Beanshell is not generally recommended)
It's also possible to set the "Loop Count" to "Infinite" and "Thread lifetime" to something very big and stop the test using JSR223 Test Element or Flow Control Action sampler if it helps
I am using the following code in BeanShell postprocessor to stop the current iteration on an error and move to the next iteration. But in my case, I am handling the duration programmatically(infinite while loop and time functions) and not using the thread group loop/duration. So loop count is set to default one at thread group level.
My jmeter script would be -> 3 transaction + whilecontroller(10 transactions).
My error would occur at 5th transaction inside while loop. so i need to stop at that 5th transaction level and start the thread again.
ctx.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_CURRENT_LOOP);
So in this case where am not using thread group loop count/duration and using the above code in a Beanshell Postprocessor, it is not moving to next iteration(like stop the current wihle loop run and start from initial). Am I missing something, could someone suggest?
It won't, if the error occurs at 5th iteration - "your" code will proceed to 6th iteration without executing any Samplers which are below the Beanshell PostProcessor. According to interpretation of your description with my very limited level of English I believe you need to use START_NEXT_ITERATION_OF_THREAD action instead.
It's recommended to use JMeter's built-in test elements and avoid scripting where possible. Particular your case can be handled using If Controller and Flow Control Action Sampler combination
If you prefer or have to go for scripting - consider using the most performing language which is Groovy, it's recommended to use JSR223 Test Elements and Groovy language for scripting since JMeter 3.1
Basically this in a slightly (but meaningfully!) different context. From within a Groovy script being run in jMeter (those are all the details I have at the moment), I need functionality to tell the script what iteration of the test it is currently on.
There are 2 options:
Use vars.getIteration() function
Since JMeter 4.0 you can also get current Thread Group iteration as ${__jm__Thread Group__idx}. From the Groovy script it would be vars.get('__jm__Thread Group__idx)`
My test plan is below
!TestPlan
ThreadGroup
LoopController1
Sampler1
BeanShellPostProcessor
Listener
LoopController2
Sampler2
As part of Beanshellpostprocessor, i am putting count value to a variable
props.put("noOfRecords",vars.get("msg_#"));
Now this value i am placing on 2nd loopcontroller as ${__P(noOfRecords,0)}
This setup is failing for iterations where we don't have any records. So the previous "${__P(noOfRecords,0)}" value is considered while running the Loop2.
Is there any other way we can achieve the dynamic loop counter?
You can use variables or properties in the Loop Controller to change the loop count at run time.
If the Property/Variable is set correctly by the Beanshell postprocessor in your test, It should work. That is, you need to set the value to 0 explicitly when there is no record. Otherwise Properties (which are not destroyed until you close JMeter) might use the previous value.
In JMeter, I have a test plan with a thread group. The thread group has number of threads and a loop count which can be set in the gui.
Is there anyway I can figure out dynamically what they have being set so I can pass them to variable?
Thanks.
Use BeanShell PostProcessor with following code:
vars.put("threads", Integer.toString(prev.getAllThreads()));
Or maybe you just looking for this: http://code.google.com/p/jmeter-plugins/wiki/ActiveThreadsOverTime
You can parametrize the thread count defining a property like
${__P(users, 1)}
and if you run the test plan from command line, you can specify its value as -Jusers=XX. If, instead, you run the test from JMeter gui, to verify the text plan for example, the users property assume the default value of 1.
Don't forget to reference the property in the thread count, with ${users}.
You should parameterize your thread count at TestPlan level settings, then use that parameter (variable) both in ThreadGroup and Listener.
If the value varies and you want to pass it from command line use __P() function instead of variable.