Based on this thread: Jmeter - Using varible: from JDBC request into another JDBC query under loop
I tried to get variable from DB query and use it.
If i use into parameter value like: ${__V(id_${__intSum(${__jm__Loop Controller__idx},1,)},)} inside loop controller works perfectly fine.
But, if i want to use in JSR223 PostProcessor like:
def dbID = '${__V(id_${__intSum(${__jm__Loop Controller__idx},1,)},)}'
log.warn(dbID);
or
def dbID = prop.getObject("id").get(i).get("id")
log.warn(dbID);
My script fails.
What is the equivalent usage of ${__V(id_${__intSum(${__jm__Loop Controller__idx},1,)},)} into JSR223 PostProcessor?
My script:
Use vars:
int id = Integer.parseInt(vars.get("__jm__Loop Controller__idx"))+1;
vars.get("id_"+ id);
Related
I am new to jmeter and I am trying run http request 10 times to create 10 categories in a loop
Creating loop to run http request within it
http request
Every time, I store the response(category id) in Json Extractor in a loop.
extract value from response
At the end of the loop, how can I store all the responses (i.e. category IDs) in an array object?
Since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting
Here is some form of Groovy solution:
def ids = vars.get('categoryIDs') ?: []
if (ids instanceof String) {
ids = new groovy.json.JsonSlurper().parseText(ids)
}
ids.add(new groovy.json.JsonSlurper().parse(prev.getResponseData()).id)
vars.put('categoryIDs', new groovy.json.JsonBuilder(ids).toPrettyString())
Demo:
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
I need to use variable from beanshell in my while loop, but I am not able to see the value of variable coming through.
output variable of my JDBC select count(*) query is "count_num"
using following code in beanshell:
int id = Integer.parseInt(vars.get("count_num_1").trim());
Following is the structure of my test plan:
-JDBC request (o/p variable is count_num)
-beanshell sampler (parse string count_num to integer)
-While Controller: vars.get(${i})>0)
-http request1
-http request2
- (beanshell code to decrease "i" by 5 - not sure how??)
What am i doing wrong in usage of "i" and also how to decrease count of "i" by 5 towards the end of while loop.
Put variable string value after adding 5
vars.put("count_num_1", String.valueOf(id +5));
I'm trying to get today's date using time function of jmeter with the format "${__time(yyyy-MM-dd)}" in BeanShell postprocessor. But after executing the Beanshell postprocessor the result shows as "1996". Basically the "time" function is displaying result by subtracting the values like "2018-03-19"=1996.
Please help me to resolve this issue so that i can get current date and time.
Beanshell code as below
import org.apache.jmeter.gui.GuiPackage;
import org.apache.commons.io.FilenameUtils;
String testPlanFile = GuiPackage.getInstance().getTestPlanFile();
String testPlanFileDir = FilenameUtils.getFullPathNoEndSeparator(testPlanFile);
vars.put("testPlanFileDir", testPlanFileDir);
//create the file in test plan file dir and print current time and Date
FileOutputStream f = new FileOutputStream(testPlanFileDir+"/CurrentDate.txt", true);
PrintStream p = new PrintStream(f);
this.interpreter.setOut(p);
//current date and time;
print("Current date is:"+${__time(yyyy-MM-dd)});
f.close();
Set time function call as parameter of your PreProcessor (by the way migrate to JSR223 + Groovy for performances)
And use it then with variable Parameters:
Try this with JSR223 PostProcessor and language Groovy and put this into script area:
def a = new Date().format('yyyy-MM-dd')
new File('Example.txt').withWriter('utf-8') {
writer -> writer.writeLine 'Current date is : ' + a.toString() }
(It works on JMeter 4.0)
You should move to JSR223 postprocessor according to JMeter Best Practices:
Since JMeter 3.1, we advise switching from BeanShell to JSR223 Test Elements
Until then you can fix it by quoting the function call as
print("Current date is:"+"${__time(yyyy-MM-dd)})";
This fix will treat the function's return value as string.
Currenty it treat it as a numeric substraction: 2018-3-19=1996 and then convert it to string
Performance anti-pattern #1: given you use GuiPackage class you won't be able to execute your test in non-GUI mode, consider migrating to FileServer instead
Performance anti-pattern #2: using Beanshell is not recommended as in case of high loads it might become the bottleneck. Since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for any form of scripting
Performance anti-pattern #3: don't inline JMeter Variables or Functions in script body they break Groovy compiled scripts caching function and might resolve into something which will cause unexpected behaviour (like in your case), compilation failure or even data loss.
So:
Add JSR223 PostProcessor instead of the Beanshell one
Put the following code into "Script" area:
String testPlanDir = org.apache.jmeter.services.FileServer.getFileServer().getBaseDir()
File currentDate = new File(testPlanDir + File.separator + "CurrentDate.txt")
currentDate << new Date().format("yyyy-MM-dd")
Having the following request:
From this I extract using the Regular Expression Extractor the following string:
%5B1172%2C63%2C61%2C66%2C69%2C68%5D
I decode this using the urldecode function: ${__urldecode(${Groups_g2})}
Decoded: [1172,63,61,66,69,68]
On the following request I want to extract the values using the BeanShell PreProcessor to obtain a list of parameters like this one:
I know that I have to use sampler.addArgument but i can't figure how to extract data from the list and add the values as parameters.
Try the following:
Put ${__urldecode(${Groups_g2})} into Beanshell PreProcessor's Parameters input field
Enter the following code into Script area
String params = Parameters.substring(1, Parameters.length() - 1); // remove square brackets
int counter = 1;
for (String param : params.split(",")) {
sampler.addArgument("parameter" + counter, param);
counter++;
}
I have no idea what parameter names need to look like, hopefully above information will be helpful.
HTTP Request with no parameters:
Beanshell PreProcessor
Parameters in View Results Tree Listener
For more information on Beanshell scripting in Apache JMeter check out How to use BeanShell: JMeter's favorite built-in component guide.
I am using JMeter to test a web application. The application returns JSON that looks like the following:
{"type":"8","id":"2093638401"}
{"type":"9","id":"20843301"}
{"type":"14","id":"20564501"}
I need to get a count based on type.
I have tried adding foreach controller with a regular expression extractor, but Im not sure I have done it correctly:
Apply to: Main sample only
Response field to check: Body
Reference name: match_type
Regular Expression: "type":"(\d)"
Template: $1$
Match no.: -1
Im new to JMeter so Im not sure if Im doing any of this correctly.
Thanks
If you want to operate on both type and id in single sampler, I think simple regex and ForEach controller won't be sufficient. You will have to write two regex extractor followed by while controller with BSF processor (javascript or beanshell) to extract both the values and export them to jmeter variable. Something of following type
- First Request
- Regex extractor for type
- Regex extractor for id
- BSF processor (to initialize the loopcount=0 and the total_matches of matches that you found)
- while controller (loopcount < total_matches)
- BSF processor
- export/set current_type = type_$loopcount
- export/set current_id = id_$loopcount
- increment loopcount
- USE current_type and current_id in whatever sampler you like
== Update ==
This http://goo.gl/w3u1r tutorial depicts exactly how to go about it.