How can I embed a test data set in my JMeter test plan? - jmeter

At the moment, my JMeter test uses a CSV Data Set Config to iterate through a limited set of input data for each HTTP request that I do.
But I don't want to deal with the hassle of an external file (uploading it to my test runner, etc.) - I'd like to just embed the data into the jmx file itself.
I was hoping for something like a "test data" node, that would work similarly to a CSV data set (with Recycle on EOF especially) and I'd just copy/paste the data into the test plan instead of working with an external file.
I'm thinking I might be able to work around it with a JSR223 preprocessor - but is there a better built-in way?
Edit: As per comment: the data cannot be generated.

If you want to do this via JSR223 Test Elements and Groovy language correct syntax would be
vars.put("messageId", "wibble");
vars is a shorthand for JMeterVariables class instance, see the JavaDoc for available functions and properties.
Easier way would be going for User Defined Variables or User Parameters or even better Set Variables Action

You can create a text contains keys and values separated with tab, copy all text
Notice if you have property file you can replace = with tab
Add to JMeter GUI User Defined Variables and click Add from Clipboard
It'll load all your variables to JMeter without "do that by hand using JMeter's GUI"
.

This is my first go at a script based approach using a JSR223 preprocessor node:
// This is where the data is embedded. Up to a couple of hundred entries
// is probably fine, more than that will likely be a bad idea.
def messageIdList = ["graffle", "wibble", "wobble", "flobble", "gibble", ...]
def messageIndex = (vars.getIteration() -1) % (messageIdList.size() -1)
println "iteration ${vars.iteration}, size ${messageIdList.size()}, index: ${messageIndex}"
vars.put("messageId", messageIdList[messageIndex]);
messageIndex++
This appears to do what I want, even when run in a Thread Group with multiple threads.
I'm not sure exactly what the vars.getIteration() represents, and I'm not clear about the precise lifetime / scope of the variables. But it'll do for now.
Any better answers will cheerfully accepted, marked and upvoted.

Related

Using flexible file writer in Jmeter produces unwanted results in the output csv

I am using the flexible file writer in Jmeter to write the four variables that I have captured and defined in the properties file. I have used the debug sampler to verify and view results of the values of each of the 4 variables that I have used.
But when I am trying to use number of threads as more than 3 or 5, I am getting garbled and multiple values of the variables in the same column of the output csv as:
But ideally I am expecting the result to be like:
Timestamp_Queued|Timestamp_Download_Started|Timestamp_Download_Completed|Timestamp_Completed
1623596826050 1623596829514 1623596829868 1623596830045
1623596826195 1623596826434 1623596826840 1623596827071
1623596826427 1623596827736 1623596828138 1623596828319
1623596826629 1623596830096 1623596830417 1623596830600
1623596826809 1623596827113 1623596827514 1623596827692
Can someone help please. I have tried using other JS to capture the data one below the other but I am not able to capture the data in the above way. I am sure Jmeter must have an easy way to accomplish this.
Thanks!
We cannot help you by looking at the screenshot of the result file, we need to see your configuration of the flexible file writer, know how do you launch your JMeter script, is there any suspicious entries in jmeter.log file, etc.
Given that:
You declare your variables as Sample Variables
Configure the Flexible File Writer to save these variables as variable#0,variable#1,etc
Run JMeter in command-line non-GUI mode
You should get the anticipated results
More information: Using the JMeter Flexible File Writer Plugin

Jmeter JSONs comparison

Currently I am working on moving some API DDT (data from CSV) tests from RobotFramework to Jmeter and what troubles me is the lack of proper JSONs assertion which is able to ignore some keys during comparison. I am totally new to jmeter so I am not sure if there's no such option available.
I am pretty sure we are using the wrong tool for this job, especially because functional testers would take the job of writing new tests. However, my approach (to make it as easy as possible for functionals) is to create jmeter plugin which takes response and compare it to baseline (excluding ignored keys defined in its GUI). What do you think? Is there any builtin I can use instead? Or do you know anything about some existing plugin?
Thanks in advance
The "proper" assertion is JSON Assertion available since JMeter 4.0
You can use arbitrary JSON Path queries to filter response according to your expected result
Example:
If it is not enough - you can always go for JSR223 Assertion, Groovy language has built-in JSON support so it will be way more flexible than any existing or future plugin.
Please find below the approach that I can think of:-
Take the response/HTML/json source code dump for the base line using "save response to a file".
Take the response dump for the AUT that needs to be compare or simply 2nd run dump.
Use 2 FTP sampler's to make calls for the locally saved response dump's.
Use compare assertion to compare the 2 FTP call response's. In the compare assertion, you can use RegEx String and Substitution to mask the timestamps or userID to something common for both so that it will be ignored in comparison.
Below I have shown just an image for my thought's for help.
You need to take care on how to save and fetch the response's.
Hope this help.

Jmeter - query, store in variable. Now use that variable in post-processing

Looking to utilize jmeter for some automated testing but facing a problem. preliminary to my tests I want to run a query on my DB and then store the result in a text file.
I thought I'd do it via a JDBC request as such:
Then immediately after I want to do some post-processing that writes the result to our file:
I've tried, too, putting the paramater passed to vars.get in quotation marks, but I get no such luck. Jmeter does write a file, but that file is empty, and if I run the query independently, it does return results.
Does anybody know how to get this desired behavior?
If you look into jmeter.log file you should see a Beanshell Related error.
This is due to "Result Variable Name" being an ArrayList, not String, hence
You need to use vars.getObject() method instead of vars.get();
Ensure you quote the variable name
Remove the ";" at end of SQL Query
You need to iterate the ArrayList somehow or serialize it to file.
If result set is large it's better to consider doing it via JSR223 Sampler using "groovy" as a language as Beanshell has some performance limitations. See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! guide for benchmarking results and instructions on how to setup groovy scripting engine support.
To write the output to a file , see :
Append data into a file using Apache Commons I/O
If you decide to use Groovy it will be even easier :
http://grails.asia/groovy-file-examples

How to control variable substitution in JMeter?

I have some user defined variables:
SERVER_NAME myhost.com
THE_HTTP_PROTOCOL HTTP
LOOP_COUNT 2
Then, I use a Recording Controller to capture my browser actions. Unfortunately, JMeter does more variable substitution than I'd like it to. For example, it substitutes ${LOOP_COUNT} into a web site name that contains "2", e.g., /host${LOOP_COUNT}/somePage. My favorite example of over-zealous variable substitution is my ${THE_${THE_HTTP_PROTOCOL}_PROTOCOL} Header Manager.
Is it possible to make the recording more selective about what variables can be used for substitution? For example, of the variables I've listed, I'd really only like SERVER_NAME to be substituted in.
I'm using JMeter 2.11.
For now you must disable User Defined Properties during recording.
Maybe you could open a bugzilla request here:
http://jmeter.apache.org/issues.html
Note that variable substitution works better with Regex Matching checked (HTTP(S) Test Script Recorder component) as it will be much more intelligent.
But ensure if you use it you do not use values (usually short ones) that can be frequently in responses/requests.
I wouldn't recommend to substitute variables during recording as the behavior may be flaky.
If you want to parametrize your recorded test it's better to go for
HTTP Request Defaults - for host, protocol and any other http-related stuff
JMeter Properties and functions like _P() or _property() to retrieve them .
Actually I would set host and protocol as properties too as they can be set via command line, via Ant or Maven plugin or any other way of invoking JMeter. Besides properties can be shared across thread groups and variables have local scope.
I was able to work around the problem by adding a User Defined Variables (UDV) element to my workbench.
It defined:
THE_HTTP_PROTOCOL someStringThatShouldNeverBeMatched
USER_COUNT someStringThatShouldNeverBeMatched
Then, when I recorded, I did not get the unwanted variable substitutions.
Another option is to break up a monolithic collection of variables into smaller groups using User Defined Variables. Then, you can disable certain of those smaller groupings when you need to record. For example, you could put USER_COUNT into a "Flow of Control" UDV, and then disable it at recording time as shown below.

Variable input for JMeter Load testing?

I need to load test a service I've developed but I need the data that I post to the web service to have some variance.
I've set up Thread with an Http Request and I've the parameter I need to set but I can't see how I'd go about changing the contents of the HTTP parameter from request to request.
Ideally I'd like to feed in a list of data Items and have JMeter iterate through them.
Prepare kind of csv-file with list of your test-params and use it to parametrize your test-samplers, using at least the following:
CSV Data Set Config
Explained example here, simple example here.
Jmeter functions: __CSVRead, __StringFromFile.
Variables From CSV sampler from jmeter-plugins.
One way would be to prepare a CSV file with all the values that you will need. There are a multitude of different ways to use it afterwards. Alies Belik's answer listed most of them. The drawback of the CSV approach, however, is that you need to generate the list of values, and in some tests you can't simply reuse it without cleaning up/reinitializing the back-end database.
Another option are the functions for generating random values, usually paired with "User Defined Variables" controller.
__Random for generating numbers in a given range.
__RandomString for generating random strings of a given length and containing a set of characters.
This is a powerful mechanism, but I find it somewhat cumbersome and clunky.
For simple variables, like generating username/password/e-mail combinations, I prefer and find it easier to use the Random Variable config element. It's available since Jmeter 2.3.3. You add it to your thread group and specify a variable to store the random value for each thread. You can later reference this variable in your HTTP sampler, in the GET/POST parameters of the request, by specifying the Value of the parameter to be testuser-${rnd} for username, testpass-${rnd} for password. Each thread will get a different value of ${rnd} so there is a small chance (but there is still a chance) that you will get duplicate values (users).
Besides the functions mentioned in #zorlem answer, You can also use:
__UUID for generating a pseudo random type 4 Universally Unique IDentifier, if you need to generate random & unique strings.

Resources