How set a variable as a parameter value in JMeter - jmeter

I'm trying to set a variable as a parameter value in a Backend Listener. I tried to add the variable as
${"testname"}
and also as
$["testname"]
but both options didn't work. JMeter consider them as a string and not as a variable. Any ideas how I can do that ?

The correct syntax for JMeter Variables is ${variable_name_here} so you need to change it to ${testname} and it should start working as expected.
If there will still be problems make sure that the variable is defined and has its respective value, it can be done using Debug Sampler and View Results Tree listener combination
P.S. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting instead of Beanshell (and in fact any other languages) so while your script is relatively small maybe it's a good time for migration.

Related

JMeter timeshift issue

I've been working with timeshift, but it seems that timeshift does not do what I expect it should do. When I input a DateTime into TimeShift to work with it always uses the Current DateTime, but when I only input the Date it works?
long timeStamp = ${__timeShift(,,P2D,,)};
${__timeShift(yyyy-MM-dd'T'hh:mm:ss'Z',,P2D,,timeStampFrom)};
log.info("timeStamp: " + timeStamp);
log.info("timeStampFrom: " + ${timeStampFrom});
long timeStampTo = ${__timeShift(,${timeStampFrom},P2H,,)};
log.info("timeStamp: " + timeStampTo);
The function is called timeShift, but why doesn't let it me work with the datetime I input in?
Thanks
UPDATE:
I've been playing around with it... and yes when you are using it in the header of the JSR223 Sampler it works great. The moment you use the same code inside Sampler it doesn't... It always goes wrong with the following message:
Another update:
So when using this in the User Defined Variables:
This works without any issues, but using it in Groovy itself it doesn't work at all... getting errors. Using it in the name of a sampler works also outstanding. From my point of view it is an issue in the way how jMeter works.
Don't inline JMeter Functions or Variables into Groovy scripts, either use "Parameters" section for this.
There is one more thing connected with using functions and variables in Groovy scripts: they're being compiled and cached hence if you run your test with 2 or more iterations only first value will be used everywhere.
Correct expression for adding 2 hours is PT2H
And last but not the least, your code is not a valid Groovy code so it won't compile, you can check jmeter.log file yourself and you will see the error.
Assuming all above I don't think you need any Groovy scripting at all, you can do everything using JMeter's built-in test elements as functions can be used anywhere in the script:
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction

Not able to update the user-defined variable value in Jmeter

I have defined one variable in Jmeter with some default value. I want to update the value of this variable based on certain conditions. I am using bean shell processor to update the value of this variable.
So the scenario which I am trying is, I am executing my test plan for some fixed number of times(say 2 minutes), and I am also counting(using counter) the number of request jmeter is making to the endpoint. So based on the one specific counter's value, I want to update the value of this user-defined variable. So to update this, I am using below line in bean shell processor -
vars.put("variableName","value");
So the above statement is only changing the value of the variable for that specific count, after that, it is again referring to previous value. Like,
if(count==100){
vars.put("variableName","newvalue");
}
So it is updating this value only at the 100th request, and when 101th request comes up, it again having the previous value.
Can someone guide me on, how can I update this value for the variable. Also I am using one thread group with 10 threads.
I cannot reproduce your issue, refer the test plan below for demo:
So I would recommend double-checking your if(count==100) condition, JMeter Variables are normally being stored as Strings so my expectation is that the condition should be amended to look like:
if (vars.get("count").equals("100"))
Also since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so consider migrating on next available opportunity.

How to handle escape characters (backslash) in jmeter

while executing the API's using jmeter tool in the response i get some value as '1K\/vyEh'.
while storing this value in the variable it is observed that the backslash does not get stored in the variable resulting in saving the value as '1K/vyEh'
I need guidance of how to save the value as is in the variable
There are too many "unknowns" in your question so unfortunately it is not possible to provide a comprehensive answer without seeing:
at least partial (better full) response
and the way you're storing the value into a JMeter Variable
Here is a quick demo that in theory it is possible:
The demo assumes:
Dummy Sampler
JSR223 PostProcessor
Groovy language and regular expressions

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

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.

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

Resources