How to set up the number of threads in a thread group by using the length of an array - jmeter

I have a user defined variable that contains several elements:
a,b,c,d,e,f
Is it possible to use the lenght of the above 'array' to pass as Number of Threads?
I tried using a beanshell function like this:
${__BeanShell(vars.get("users_username").split(",").length,)} but it does not seem to work.

As per JMeter Functions documentation
If a function parameter contains a comma, then be sure to escape this with "\", otherwise JMeter will treat it as a parameter delimiter
Since JMeter 3.1 it is recommended to use __groovy() function instead of other scripting options/languages mainly because Groovy performance is much better than alternatives
Assuming all above you can dynamically define the number of threads as:
${__groovy(vars.get("users_username").split("\,").length,)}

Related

How do I use multiple values of all matches returned by regular expression in jmeter in a single HTTP request

I'm capturing multiple values with a regular expression, and that expression returns 20 matches as shown in image RegExMatches,
My regular expression is something like this RegEx.
How do I use multiple values in post data of my HTTP request which is something like PostData
I tried with MatchNR and -1 and call with ${candidateGUID_gN} N being the match number but this didn't workout.
How do I use the Extracted values in Post data ? or will I have to make different Regular Expressions for each value ?
I think the only way of achieving this is building your request using JSR223 PreProcessor and Groovy language, take a look at the following JMeter API shorthands:
Arguments
vars aka JMeterVariables
sampler aka HTTPSamplerBase
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy
And please stop posting code as images it's not very polite / disrespect to the community.

Jmeter random variable function for specific alpha numeric pattern

I am new to Jmeter. I am testing rest API using Jmeter. In one scenario need to pass dynamic random variable in request. The pattern is starting character should be an alphabet and remaining 9 characters numbers (0-9) example "J123456789". Please suggest me how to do it using Jmeter random functions.
You can go for __RandomString() and __Random() functions combination like:
${__RandomString(1,ABCDEFGHIJKLMNOPQRSTUVWXYZ,)}${__Random(111111111,999999999,)}
Demo:
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction

Jmeter beanshell Encounter ";"

I am trying to execute beanshell script in jmeter for a URL parameter value. I have the following:
${__BeanShell(vars.get("query").replaceAll(" ","%20"))}
The jmeter console outputs this:
Caused by: bsh.ParseException: In file: inline evaluation of: ``vars.get("query").replaceAll(" ";'' Encountered ";" at line 1, column 33.
I can't figure out what the problem is as the character there is a , not a ;.
You're doing something ridiculous. Encoding an URL is not only about escaping spaces, looking into URLEncoder documentation you will need to handle:
All non-Latin non-alphanumeric characters
All characters apart from ., -, *, and _
Which might be very tricky.
So you basically have 2 options:
Use JavaScript encodeURIComponent() function via JMeter's __javaScript function like:
${__javaScript(encodeURIComponent("${query}"),)}
Or use aforementioned URLEncoder from __groovy() function like:
${__groovy(URLEncoder.encode(vars.get('query')\, 'UTF-8').replaceAll('\\\+'\,'%20'),)}
Performance-wise in cases of high loads Groovy is preferred option, check out Apache Groovy - Why and How You Should Use It article for more details.
See JMetrer's functions tutorial, you need to escape every comma:
If a function parameter contains a comma, then be sure to escape this with "\", otherwise JMeter will treat it as a parameter delimiter.
In your case
${__BeanShell(vars.get("query").replaceAll(" "\,"%20"))}
Also consider using __groovy function instead of __BeanShell for better performance.
Please use below code in Beanshell PreProcessor or BeanShell PostProcessor in order to replace single space character to '%20':
String myString = vars.get("query");
String new_var = myString.replaceAll(" ", "%20");
vars.put("updated_value", new_var);
You can further use 'updated_value' variable having space replaced by '%20' in next requests.
Please refer to the JMeter Knowledge Base for more information on JMeter elements.

Jmeter- How to pass Comma separated String as 1 value through parametrization

From a csv file, I need to pass
224,329,429
as a single value to one of the parameter in HTTP request.
I have parameterized using CSV data config. But, only 224 is getting passed.
I want 224,329,429 to be treated as a single value.
Please let me know how do I achieve this. Should I change anything in CSV config or CSV file to make this work?
Just use __StringFromFile() function instead of using CSV Data Set Config.
The __StringFromFile() function reads next line from the file each time it's being called so it seems to be a lot easier to stick to it for particular your scenario.
The syntax is as simple as ${__StringFromFile(/path/to/your/file.csv,,,)} and the function can be used anywhere in the script, i.e. directly in the request parameter section.
See Apache JMeter Functions - An Introduction to get started with the JMeter Functions concept and comprehensive information on the above and other JMeter functions.
You should change your delimiter to a not used character e.g. #
In that way you will be able to get full line for every request
Use ${__FileToString(dummy.csv,,payloadvar)} function. It makes the file independent that mean you can use any file extension example: .txt, .csv, .excel etc..
Just keep the string in dummy.csv and it will fetch the whole string.
benefit of using this function is, it will not consider comma's so in case your string has comma separated values then this is the best option.
Just use %2C in the place of comma.

Will jmeter function FileToString(path) takes dynamic values

Will jmeter function FileToString(path) takes dynamic values.
I need to use around 400 json files as a input. so planning to use FileToString($fileName}) in body data. By providing the filename column in csv. But it seems Jmeter is checking filename as file instead of getting value from csv.
You need to wrap your ${fileName} into __eval() function. As per documentation:
The eval function returns the result of evaluating a string expression.
This allows one to interpolate variable and function references in a string which is stored in a variable.
So you need to change your expression to look like:
${__FileToString(${__eval(${fileName})},,)}
For more information on JMeter functions see How to Use JMeter Functions post series.

Resources