JMETER Write and Read from the same CSV file between threadgroups - jmeter

In JMeter, I have a thread group which creates order, I capature the relavant details of the order and write to a csv file.
I have another thread group which searches these orders, the data for which I am currently creating as part of the above thread group as a prerequsite for this step.
Rather I wanted to check if there is an option to write and read from the same csv file, just to avoid the data preparation ahead. Any suggestions on how I can achieve this? Any constraints forseen on running with multiple user writing and reading from the same file. Thanks

With 1 thread - yes.
With > 1 thread - still yes, but it will be dangerous due to race condition, writing into the same file by multiple threads will result in data corruption.
If you need to pass values between Thread Groups there are following options:
Inter-Thread Communication Plugin
__setProperty() function in 1st Thread Group and __P() function in the 2nd Thread Group if you don't want or cannot use the plugin

You can use Inter Thread Communication plugin to share data between thread groups.
You can put a string value into a queue from one thread, and then get that value from another thread, even in another Thread Group. This allows you having separate Thread Groups with some synchronization and parameter passing between them. There may be any number of FIFOs and they differ by "queue name".
Use the Inter-thread communication Post-processor to write the order information
User the Inter-thread communication Pre-processor to read the order information from another thread group
Sample Test Plan 1 Test Plan 2

Related

Passing property between threads in Jmeter

In J meter, I am unable pass property from thread 1 to thread 2 if there is JDBC request in thread 1. If there is no JDBC request, property is getting passed from thread 1 to thread 2. Any specific reason for this, Any changes i need to do? kindly help me on this.
Passing values between Thread Groups is not something you should be normally doing, I would recommend reconsidering the design of your JMeter script so it would not be required
There are several ways of passing values between different thread groups starting from using __setProperty() and __P() functions combination, using interim CSV files, and ending up with Inter-Thread Communication Plugin, they all should work irrespective of presence/absence of the JDBC test elements in this or that Thread Group
When anything goes wrong in the absolute majority of cases JMeter will print something to jmeter.log file, first of all check it for any warnings/errors and if they are not there - check the variables/properties values using Debug Sampler

Jmeter wont read csv file with line feed properly

I have a thread which writes data to a csv file and data looks like this in that file, as you see below each data is separated by a , then line feed.
ApiKey
20a145260241463194bf84f43952da9c:dut8ghdt+iQrsmYEgKzHzF1It79aWRIjb/d1wM8U3WE=,
93,
d0e29bb7-476d-4a52-9527-e5d9bb0ac34a
In another thread I am trying to read the data so I have done the following. Added a "CSV Data Set Config" with correct filename variables X,Y,Z and my Delimiter I have tried all these A=,n or B=n or C=n, I am not able to get Y and Z properly. What am I doing wrong?
Synchronization of data between thread groups using CSV files is not the best idea as you will have to implement a form or read/write lock to ensure that only one thread is writing data to the CSV file at a time and that will cause throughput degradation.
So I would recommend going for one of the following solutions:
Use __setProperty() function to convert the data from JMeter Variables into JMeter Properties. JMeter Properties are global for the whole JVM therefore they can be accessed from another Thread Group(s). See Knit One Pearl Two: How to Use Variables in Different Thread Groups article for more information.
Use Inter-Thread Communication plugin which implements FIFO queue so you can block threads in 2nd thread group unless data from 1st thread group is available. You can install Inter-Thread Communication Plugin using JMeter Plugins Manager
Both approaches are in-memory therefore you will not need to store data in interim files.

JMeter - reusing random variables in another thread group

I currently have a test plan where I use a random variable to submit a post request to a given URL (/app/${app_id}).
I want to also re-use the random variable app_id to poll the status of that app (/app/${app_id}/status). Note there would be multiple requests to the status URL.
My current idea is to:
have one thread group that submits the posts
save a list of the randomly generated app_ids
in another thread group, read the list of app_ids and for each app_id, loop the status request
Is this a sensible approach? If so, how can I go about saving the randomly generated app_ids and then reading them?
Also, if there is a better approach to this situation, I'm all ears :)
You can pass the values from one thread group to another one using __setProperty() function like:
${__setProperty(appid-${__counter(FALSE)},${your_variable_holding_appid},)}
Each time the function will be called it will generate a JMeter Property in form of
appid-1=foo
appid-2=bar
appid-3=baz
etc.
Where numbers like 1, 2, etc. are coming from __counter() function
In another Thread Group you will be able to access the generated values using __P() function like:
${__P(appid-${__counter(FALSE)},)}
Demo:
See Apache JMeter Functions - An Introduction for more information on JMeter Functions concept.
There is more "advanced" way of passing JMeter variables between threads and even thread groups, moreover you will be able to synchronise your threads, i.e. don't start thread in 2nd thread group until variable is not set using Inter-Thread Communication plugin.
Solution with Thread Groups is feasible, although you also can do all that in one thread group, by configuring one of the threads to be "monitor", while remaining threads submit posts. Something like this:
Thread Group
If [${__threadNum} == 1]
Samplers to check status
If [${__threadNum} != 1]
Samplers to submit posts
One reason to use 1 thread group for both types of users is if you need any sort of synchronization between writing and reading app_id list - that is easier to achieve in one thread group. Or if you already have many thread groups.
As for providing app_id to various threads/thread groups, you can use one of the approaches:
To pass IDs from one thread group to the other without a file, you can use one of the methods described here (using properties is the most popular way).
In your case, probably the easiest is to save them in one property with some delimiter, e.g. ID1,ID2,.... The "status" thread then can get this property, split it using either script or __split function to convert property into serialized variables: app_id_1, app_id_2, etc. Those variables are a natural choice for ForEach Controller.
Also first thread group could be saving app_ids into file, while the other reads from the same file with CSV Data Set Config. A bit of caution here though, if they are doing it at the same time.
If app-ids can be pre-generated, a more economical approach would be to have SetUp Thread, which generates them and saves them to CSV file (you will only need one script, e.g. BeanShell to do that, see example here). And then both thread groups read from that file using CSV Data Set Config

Dependency among multiple thread groups in JMeter

I have set up a load test plan with multiple thread group, i.e. -
Registration (50% of the threads)
Place Order (10% of the threads)
Some more operations (remaining threads)
Herein if Registration thread does not succeeds than I don't want to execute remaining thread groups. In case of a single thread group I can use if controller and discard samples if one sample fails but how do I achieve it when I am using multiple thread groups.
JMeter Variables scope is limited to current Thread Group only, if you want to use If Controller basing on the condition which is set in another Thread Group - you should be using JMeter Properties instead (JMeter Properties scope is global for the whole JVM). See How to Use Variables in Different Thread Groups article for details on converting JMeter Variables into JMeter Properties.
You may also find InterThread Communication plugins useful when it comes to passing data between thread groups and setting up dependencies.
However, given your scenario you either need to pass the whole thread context (cookies, cache, whatever) which might be tricky so it would be much better putting all the samplers under the same Thread Group and use Throughput Controller, Switch Controller or Weighted Switch Controller, whatever matches your scenario the closest way. See Running JMeter Samplers with Defined Percentage Probability guide for more information.

load test with varying number of threads in JMeter

I am have been exploring JMeter for the last couple of days. And I am try to achieve the following:
I am trying to create a load test for a service such that, in the first loop 'n' threads are created and in the second loop 'n+m' threads are created and in the third loop 'n+2m' threads, in the fourth 'n+3m' threads and so on. I did find many solutions here and I tried replicating them, but somehow it is not working for me. The test plan I created looks something like this:
Step 1: Add a user defined variable called USERCOUNT = 0
Step 2: Create a thread group. I have used __intSum to increment USERCOUNT by one and storing the result back in USERCOUNT for future reference.
Ideally what I am expecting here is, in the first loop one thread is created, in second loop 2 thread are created and in the third loop 3 threads are created. So in total there should be 6 thread created. However the results show up something like this:
Not sure if I am missing something silly or my approach itself is completely wrong. Any help from the community would be appreciated. :)
I believe what you are trying to do is impossible. When JMeter starts a thread group for the first time it initializes the number of threads.
What you are essentially trying to do is re-run the same test three times with different numbers of users each time. JMeter cannot re-run a thread group once started, it can only re-run ("loop") a thread in a thread group. It is not possible to add threads to a running thread group once the ramp up and threads are initialized. (Let me know if I should try to explain that further).
If you want one thread group to populate the run-time settings of another thread group, you must have two thread groups in the test plan. At the test plan level, check off the option "Run Thread Groups Sequentially."
With that option, JMeter will not initialize the number of threads on the second thread group until the first one finishes. The first thread group can set a global property, and the second thread group can reference it in the number of threads.
How to set a property in beanshell: (this would be in Thread Group 1)
props.put("threadgroup_2_num_users","15");
How to reference that value in Thread Group 2:
${__P(threadgroup_2_num_users)}
If this does not help, maybe try to describe your end goal. What are you trying to learn about the thing you are testing?
Edit: Perhaps using the plugin package "Stepping Thread Group" will allow you to achieve the scenario you are trying in a single thread group. Check this out:
http://jmeter-plugins.org/wiki/SteppingThreadGroup/
JMeter currently doesn't provide possibility to change number of threads on-the-fly during test execution so I would suggest to go with the following alternatives:
Use i.e. Ultimate Thread Group available via JMeter Plugins which adds some extra features to JMeter's Thread Group so you will be able to implement your scenario
Consider so called "goal oriented scenario" when load is based on requests per second rather than on number of virtual users. You can precisely set desired throughput rate and even change it during test run using Constant Throughput Timer

Resources