I have a Bean shell preprocessor which ends up setting up some global variables like host name and the path according to the value that the user passes.
The variables that the bean shell sets would be used by all the thread group.
Currently i've placed my BS pre-processor outside a thread and it runs perfectly..
The issue is that it runs for every thread which isn't performance friendly.
I just want it to run once at the start of the Test plan to improve performance.
I tried to put it into a setUp thread but it doesn't work.
Is there something other that the BS-preprocessor that i can use which would be performance effective(which runs only once for the entire plan rather than for every thread).
You can put it under If Controller and use the following condition:
${__BeanShell(vars.getIteration() == 1)} && ${__threadNum} == 1
You can use setUp Thread Group which is designed for executing pre-test actions but in that case change all occurrences of vars.put() to props.put() as JMeter variables scope is limited to current thread group only and Properties are global for the whole JVM instance. See How to Use Variables in Different Thread Groups guide for additional information
JMeter SetUp thread group and TearDown thread group are meant for exactly this. These threads run at the start and the end of test plan. So, just add Setup thread group and add beanshells into. See links for more information:
http://jmeter.apache.org/usermanual/component_reference.html#setUp_Thread_Group
In the other hand, vars are limited for each thread groups. Use props to put a value which is shared with all thread groups.
This worked for me... Just configure a User Defined Variable called firstTimeIndicator and set the value to 1
String firstTimeIndicator = "";
firstTimeIndicator = vars.get("firstTimeIndicator");
if (firstTimeIndicator.equals("1")) {
log.info("The code inside the if statement executes once per test run...");
firstTimeIndicator = "0";
vars.put("firstTimeIndicator",firstTimeIndicator);
}
log.info("The code after the if statement executes once per thread");
Related
I am currently running 10 different threads like this:
each of the thread response will provide a different id in the response and i want to save them and use it as a request in the next test case(10 threads) so there will be 10 ids and 10 threads and each thread will have a unique id as a request. This is what I am doing:
Here is the first request
This is how I am extracting the values
This is how i am using the final request however i am not able to get the desired results
UPDATE:
I tried Dmitri Answer but still no luck i am extracting the id by using this
I used __threadNum() function as the prefix or postfix for the property name like:
${__setProperty(loginassistant_${__threadNum},${loginassistant_},)}
and read it similarly:
${__P(loginassistant_${__threadNum},)}
but it is not working and it is setting the value as a static string(see screenshot below):
This is how and where i am defining my loginassistant using the simple controller:
You're using properties which are global, if you have more than one thread the next thread will overwrite the property defined by the previous thread so it could be mismatch.
If you don't need to pass values across thread groups - go for JMeter Variables instead, variables are local to the thread (virtual user) and the properties are global for the whole JMeter/JVM instance
If you do need to pass values across thread groups - either use __threadNum() function as the prefix or postfix for the property name like:
${__setProperty(loginassistant_${__threadNum},${loginassistant},)}
and read it similarly:
${__P(loginassistant_${__threadNum},)}
or go for Inter-Thread Communication Plugin
I have a Test Plan, that executes multiple Thread Groups consequtevly - But for one of these Thread Groups I want to be executed once and on next Test Plan exection it should be ommited. Is this possible?
You can implement it as follows:
Put all the requests in your Thread Group under the If Controller and use the following expression as the "Condition"
${__groovy(!new File('somefile').exists(),)}
Add a JSR223 Sampler at the end of your Thread Group and put the following code into "Script" area:
new File('somefile').createNewFile()
If Controller checks for somefile file presence in JMeter's "bin" folder (or whatever is your current working directory) and if there is no file in it - its children will be executed.
JSR223 Sampler creates this somefile when it's being executed so next time you will run your test the condition will not be met and Thread Group will skip all its samplers. If you will need to re-enable the Thread Group - just delete this somefile
Not sure if I fully understood your requirement. If you would like to ignore a specific thread group dynamically , then you could pass the 'number of threads/users' for the thread group to 0 using properties.
more information is here - http://www.testautomationguru.com/jmeter-manage-test-plan/
I have a JMeter test plan which performs a simple action once.
The problem I am having is that my test setup needs to know how many threads the thread group will have. To make things clearer, here is a simple representation of the test plan:
setUp Thread Group
needs to know the number of threads in the below thread group
Thread Group
The number of threads for this thread group is determined via the "Number of Threads (users)" Thread Property.
I can dynamically get the number of threads from within the thread group, but I can't find any way of getting this number from within the setup thread group instead.
Any ideas?
Ideally I was hoping to find something like ${__groovy(ctx.getTestPlan().getThreadGroupByIndex(1).getNumThreads())}, but I can't find anything of the sort.
To be clear, I'm looking for the number of threads as assigned directly in JMeter in the Thread Group properties. This question has absolutely nothing to do with BlazeMeter and is therefore not a duplicate of Get number of threads (defined via BlazeMeter) in a thread group from the setup thread group (jmeter)
You can try defining a User Defined Variable in the Test Plan, let's say "Users_test" and assign it the number of virtual users you want to run the test.
Then simply use that variable in the Thread Group "Number of Threads (users): ${Users_test}, and you can do the same in the setUp.
The fast that you don't know the number of threads indicates that your test is badly designed.
The fact that you have already been given the answer for the same question 6 hours before indicates that you are unwilling to learn and prefer the community to solve problems for you.
Just in case I will repeat using simpler words:
if you need to overcome a JMeter limitation you have to go for scripting
the recommended approach is using JSR223 Test Elements and Groovy language
JMeter API reference lives at https://jmeter.apache.org/api/
Check out Groovy Is the New Black article for examples of using Groovy with JMeter API
Example code to get the number of threads for all thread groups:
import org.apache.jmeter.engine.StandardJMeterEngine
import org.apache.jmeter.threads.ThreadGroup
import org.apache.jorphan.collections.HashTree
import org.apache.jorphan.collections.SearchByClass
import java.lang.reflect.Field
StandardJMeterEngine engine = ctx.getEngine()
Field test = engine.getClass().getDeclaredField("test")
test.setAccessible(true)
HashTree testPlanTree = (HashTree) test.get(engine)
SearchByClass<ThreadGroup> threadGroupSearch = new SearchByClass<>(ThreadGroup.class)
testPlanTree.traverse(threadGroupSearch)
Collection<ThreadGroup> threadGroups = threadGroupSearch.getSearchResults()
threadGroups.each {
log.info("Thread Group: " + it.getName() + "; Number of threads: " + it.getNumThreads())
}
Demo:
In JMeter, I have a requirement where I want to run a particular thread group after all the other thread groups complete their run, I know the tearDown thread group has the similar behavior but unfortunately, the logic has to be part of my regular thread.
Let's say there are 4 thread groups A,B,C & D in my test plan and I want the thread group D only to be executed after A, B & C will complete their run.
Can we achieve this without using "setup, teardown & Run groups one at a time" ??
Problem ScreenShot:
I can suggest 2 options:
Use Inter-Thread Communication Plugin. See example test plan for details.
If for some reason you are not in position to use JMeter Plugins you can achieve the same using JMeter Properties like:
When Thread Group A finishes set a JMeter Property, i.e. ThreadGroupADone=true using __setProperty() function like
${__setProperty(ThreadGroupADone,true,)}
In Thread Group D:
Add While Controller at the beginning of the Thread Group and use the following condition:
${__javaScript("${__P(ThreadGroupADone,)}"=="false",)}
Add Test Action sampler as a child of the While Controller and configure it to pause for a reasonable amount of seconds, i.e. 5 so each 5 seconds While Controller will check ThreadGroupADone property value and if it is still false - sleep for another 5 seconds. When property value will become true - Thread Group D will proceed.
I Have a setUp Thread group in which i am creating A resource with unique Id,path it will give me a url to test
In main Test group I have to Test each url created in setupthread group needs to be rigorsly tested.
testplan
In tearDown thread group i need to clear the setupgroup creation .
The problem i am facing is , the property value is getting overridden
Suppose say in SetUpthread group i have created 10 resources then its the last resouce always the mainTest thread group is getting executed
I am looking for a way foreach setupThread group resource the mainTest theadgroup must execute the no of times i specified in the TheadCount and LoopCount
eg: setUpThread group ThreadCount is 10 and Loop count is 10 then i will get 100 different unquie resources would be created then its the mainTest ThreadGroup
100 TC , 100 LC i.e., for each resource its 10000 times gets invoked .
please help me in acheiving this attached pic
You can take a look at http://jmeter-plugins.org/wiki/InterThreadCommunication/ plugin.
This will help in your case. Let me know if this works.
It is hard to suggest anything without having your test plan itself so here is just an approach, I don't guarantee that it'll work, however the idea should be fine.
You need to set up as many properties as many URLs you would like to be hit in the main thread group.
For instance, if you write some URL down in JSR223 Sampler using __counter() function output as postfix like:
This will generate the following properties:
URL_1=http://some.url
URL_2=http://some.other.url
etc.
After that in 2nd Thread Group you can add another JSR223 Sampler to convert JMeter Properties to JMeter Variables like:
Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String propertyName = e.nextElement().toString();
if (propertyName.startsWith("URL_")) {
vars.put(propertyName, props.getProperty(propertyName));
}
}
Then you should be able to use ForEach Controller to iterate the variables.
Also make sure you use Groovy as JSR223 PostProcessor and Sampler language