how can we increase the string limit or size for base64Encode - jmeter

[Error](https://i.stack.imgur.com/iqUDy.png)
I tried to pass a string from csv file to ${__base64Encode()} function but in result my API request is getting failed. When I checked my jmeter logs then in that I found that base64Encode function is unable to encode the string because it is too long and limit is 65535. So, is there any way by which I can increase the string limit to encode my string or is there other way because I need to pass that string to initially encode the data to make my script work.
Please find the attached screenshot for the error in mentioned link and let me know if there is any solution to resolve this issue.

Don't inline JMeter Functions or Variables in Groovy scripts, as per JSR223 Sampler documentation:
The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:
Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
Or Use Script Text and check Cache compiled script if available property.
When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.
Amend your code to look like:
vars.put('token', vars.get('token1').bytes.encodeBase64().toString())
and the error should go away
More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?
Alternatively just use ${__base64Encode(${token1},token)} function where required, but not in the JSR223 Test Element script

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

How set a variable as a parameter value in 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.

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.

how to use jmeter to update xml to post later

I have a JMeter job that I'm taking the XML response from, updating the XML, then PUT the XML back to update the record. I have ALL the parts, except the XML update part.
I need to update XML root/Settings/HasRetry from not only "false" to "true", but also add other nodes directly under it. Order matters in this XML.
xml to update
Ideas on the best way to do this? I tried a via a simple java script:
java replace
...but the JS pukes on that b/c of the special characters in the XML string.
Ideas?
You have an error in your script, last line should look like vars.put("ResponceData", res); as your current implementation simply ignores the replacement logic.
Also be aware that it is not recommended to use Beanshell for scripting, since JMeter 3.1 users are encouraged to switch to JSR223 Test Elements and Groovy language.
Groovy is Java compliant hence in majority of cases valid Beanshell code will be a valid Groovy code, however it is not recommended to use JMeter Variables references like ${ResponceData} as it will ruin scripts compilation feature and performance will be a big question mark so switch to vars.get('ResponceData') instead.
Example Groovy code for values replacement will look something like:
def str = vars.get('ResponceData')
def res = str.replace(vars.get('findSearch'), vars.get('findReplaceWith'))
vars.put('ResponceData', res)
Demo:
See Apache Groovy - Why and How You Should Use It to learn more about Groovy scripting in JMeter.

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