Jmeter beanshell script - Polish first and last name - jmeter

i have a little problem with my jmeter test plan.
i have a jdbc request to extract customers in sql databases
from this sql query i retrieve first name and last name only
after that i have a beanshell little script to write all my customers in a csv file :
f = new FileOutputStream(vars.get("InputFilePath"));
p = new PrintStream(f);
nb_customer=Integer.parseInt(vars.get("NOM_#"));
for (i=1;i<=nb_customer;i++) {
p.println(vars.get("NOM_"+i) + ";" + vars.get("PRENOM_"+i));
}
p.close();
f.close();
my problem is that it is an application for our subsidiary company in polska
so, all customers first and last name are in polish language with different symbol, characters.
in the csv file, they appears with a ? in spite of the real character.
can you help me please ?
thanks a lot
Ludo

Try explicitly setting encoding to UTF-8, initialize PrintStream as:
p = new PrintStream(f, true, "UTF-8");
Check out JMeter default encoding as:
log.info(System.getProperty("file.encoding"));
If output is different from UTF-8 add the next line to system.properties file (lives under /bin folder of your JMeter installation:
file.encoding=UTF-8
and restart JMeter. Alternatively you can pass it via -D command line argument like:
jmeter -Dfile.encoding=UTF-8
See Apache JMeter Properties Customization Guide for more information on Jmeter properties and ways of setting and overriding them.
Check file contents in JMeter itself by using FileUtils.readFileToString() method like:

Related

Azure Load test preview unable to read data from parameterized CSV file in JSR223 Sampler

I used JSR223 Preprocessor to create the request body for a POST JSON request. I used Groovy language for it The code has some parameterization so, I mentioned the path for CSV file in the script as below and attached the Order.csv file to the test plan in load test preview.
CSV file path in the script:
"List lines = new File("Order.csv").readLines()"
So whenever I run the test in Azure load test preview, the is the error message im encountering:
javax.script.ScriptException: java.io.FileNotFoundException: Order.csv (No such file or directory)
How can I fix this. Please Help.
I tried just mentioning the CSV file name in the code and attached the CSV file to the Load test preview along with .jmx file.
This is throwing error saying, File not found
How do you know where guys from Microsoft are placing the CSV file and why do you expect them to copy it to the same place where the .jmx script lives?
I would suggest amending your Groovy script to get the file location dynamically using FileServer class, i.e. change this line:
List lines = new File("Order.csv").readLines()
to something like:
List lines = new File(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + File.separator + 'Order.csv').readLines()
More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

Read multiple CSV files automatically from a directory inside a ForEach Controller with JMeter

Im trying to read multiple CSV files from a special directory inside a While Controller to convert the data in the files to a specific JMeter property/variable. But I always get an error:
ERROR - jmeter.threads.JMeterThread: Test failed! java.lang.IllegalArgumentException: File ${CURRENTACTIONATTRIBUTEFILE} must exist and be readable
Is it possible that the variable isn't evaluated at this moment? (__V doesn't change anything).I really have no idea why this isn't working.
When I place a Debug Sampler between the ForEach and the While Controller it shows me the JMeterVariable CURRENTACTIONATTRIBUTEFILE has the correct path and file name (when I paste this string hardcoded in the CSV data set config filename it works):
CURRENTACTIONATTRIBUTEFILE=C:/#JMeter/Plans/PRODUCT/61_RISK2VALUE/61_RISK2VALUE - Resources/ModelingData/ActionAttributes/ActionAttributes_AM2.csv
This is what my Testplan Setup looks like:
CREATE file variables: groovy sampler, which checks a special directory and creates numbered vars with the file path + name + extension and a var with the count of the files found in the directory
ForEach Controller
While Controller
CSV Data Set Config (Filename: ${CURRENTACTIONATTRIBUTEFILE)
CREATE variables: groovy sampler, which creates properties/vars to a special convention I need
"CREATE file variables" - sampler code:
String actionAttributeFilePath = "${PATHPLAN}${PATHMODELINGDATA}ActionAttributes/";
File modelingDataDirectory = new File(actionAttributeFilePath);
File[] files = modelingDataDirectory.listFiles();
int fileCounter = 1;
for (File file : files) {
if(file)
{
String actionAttributeFile = actionAttributeFilePath + file.getName();
vars.put("ACTIONATTRIBUTEFILE_" + fileCounter, actionAttributeFile);
fileCounter++;
}
}
vars.put("ACTIONATTRIBUTEFILE_COUNT", Integer.toString(fileCounter-1))
"CREATE variables" - sampler code (not relevant for my problem):
All variables names used below, come from the CSV data set config.
if ("${ATTRIBUTEDEFINITIONID}" != "<EOF>")
{
def variableName = sprintf('ATTRIBUTE%2$sDEFINITIONID_%1$s',["${ATTRIBUTEFORMULANAME}", "${ATTRIBUTETYPE}"])
props.put(variableName,"${ATTRIBUTEDEFINITIONID}");
}
You won't be able to use a JMeter Variable as a CSV Data Set Config filename as CSV Data Set Config is being initialized before any variables.
The only way to make this dynamic is using JMeter Property instead of JMeter Variable
Substitute your ${CURRENTACTIONATTRIBUTEFILE} variable reference with JMeter Property using __P() function as ${__P(CURRENTACTIONATTRIBUTEFILE,)}
Provide the property value via -J command line argument like:
jmeter -JCURRENTACTIONATTRIBUTEFILE="C:/#JMeter/Plans/PRODUCT/61_RISK2VALUE/61_RISK2VALUE - Resources/ModelingData/ActionAttributes/ActionAttributes_AM2.csv"
Another way of setting the property is defining it in user.properties file like:
CURRENTACTIONATTRIBUTEFILE="C:/#JMeter/Plans/PRODUCT/61_RISK2VALUE/61_RISK2VALUE - Resources/ModelingData/ActionAttributes/ActionAttributes_AM2.csv"
References:
Configuring JMeter
Apache JMeter Properties Customization Guide
The CSV Data Set Config element has a field called Filename where you need to specify the file name of your CSV along with its' path.
By default JMeter starts to look for data files in the JMeter root/home folder (\apache-jmeter-x. xx\bin) or the same folder where the JMeter script (.jmx file) resides.
So you can have your CSV files together in the same directory than the .jmx.

An Error message occurs on command prompt while generating Dashboard from Jmeter

I have started using JMeter 3.1 recently for load testing, all I wanted to do was generate a report dashboard from a csv file.
When I run the following command from Command Prompt:
jmeter -g (csv file location) -o (Destination folder to save HTML Dashboard)
I get the error shown below:
Could not parse timestamp<1.487+12> using format defined by property.saveservice.timestamp+format=ms on sample 1.487+12 .........
I have also attached the screenshot of the error message kindly refer below:
Below is my saveservice properties that I copied into user properties file:
jmeter.save.saveservice.bytes = true
jmeter.save.saveservice.label = true
jmeter.save.saveservice.latency = true
jmeter.save.saveservice.response_code = true
jmeter.save.saveservice.response_message = true
jmeter.save.saveservice.successful = true
jmeter.save.saveservice.thread_counts = true
jmeter.save.saveservice.thread_name = true
jmeter.save.saveservice.time = true
jmeter.save.saveservice.print_field_names=true
# the timestamp format must include the time and should include the date.
# For example the default, which is milliseconds since the epoch:
jmeter.save.saveservice.timestamp_format = ms
# Or the following would also be suitable
#jmeter.save.saveservice.timestamp_format = dd/MM/yyyy HH:mm
#save service assertion
jmeter.save.saveservice.assertion_results_failure_message = true
I am not able to figure out the resaon, any help in this regard will be much appreciated.
please help, also please let me know if any addition information is required.
I have followed the below link to generate Dashboard:
http://jmeter.apache.org/usermanual/generating-dashboard.html
The answer is in your question itself:
Could not parse timestamp<1.487+12>
According to your configuration, JMeter expects first column to be in Unix timestamp format like 1487047932355 (time since beginning of Unix epoch in milliseconds)
Another supported format is yyyy/MM/dd HH:mm:ss.SSS like 2017/02/14 05:52:12.355
So there are several constraints:
The value of jmeter.save.saveservice.timestamp_format = ms should be the same during test execution and dashboard generation
You need to restart JMeter to pick the properties up. For example if you ran the test, then amended properties and then tried to generate dashboard - it might fail
There are no duplicate properties
You don't do anything with the .jtl results file between test execution and dashboard generation
My expectation is that you opened .jtl results file with MS Excel which converted timestamps into scientific notation and saved so most probably you will be able to do the opposite.
Just in case I would also recommend getting familiarised with Apache JMeter Properties Customization Guide
The default timestamp format in JMeter csv and logs is given in a Unix style format , but you can change it .
Go to (jmeterDirectory)/bin .
open jmeter.properties file .
Search for the following :-
jmeter.save.saveservice.timestamp_format
You will find it commented (Start with #) . Uncomment it and restart the Jmeter .
You can update this above property with the format you need

Saving a exact response in file in Jmeter

How to save exact response from Jmeter in a file?
e.g. - I have a response as email only so I want to save this email address only in a file with a success and failure but If I am saving a file as xml its giving a whole bunch of code otherwise if I save it as a csv file it's giving all sort of infomation other than a email address. Pls help me to solve it.
If you need to save the whole response - Save Responses to a file listener is what you're looking for.
If you need to store a part of the response - first of all get the necessary part with i.e. Regular Expression Extractor. Once you have JMeter Variable holding required value i.e. email you can add the next line to user.properties file (lives under /bin folder of your JMeter installation)
sample_variables=email
and ${email} variable value will be stored along with other results.
NB
you need to restart JMeter to pick the property up
you can also pass it via -J command line argument like:
jmeter -Jsample_variables=email -n -t /path/to/testplan.jmx -l /path/to/results.jtl
References:
Sample Variables
Apache JMeter Properties Customization Guide

how to write data to csv in jmeter using beanshell scripting

I am new to jmeter,and in my company we are doing webservices testing using jmeter.My requirement is i am using csv data config file for web services testing and here i want to update each row of csv file with test results and i am using beanshell postprocessor,please help
Ex:
csv contains:
Test Case,Dates,Numbers,Results
Total test cases are 16 and i want to update as below for each test case
TC_001,9-03-2016,001,PASS
TC_002,9-03-2016,0002,FAIL
and so one...
Result = "FAIL";
Response = prev.Get....();
If(Response.Contains("Valid"));
Results="PASS";
f = new FileOutputStream("/tmp/oders.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(Results + "," + Result);
f.close();
P.Close();
import java.io.File;
import org.apache.jmeter.services.FileServer; //jmeter spelling corrected
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.Writer;
File file = new File("csvFilePath");
FileWriter fstream = new FileWriter(file, true);
// true, will append the file if already exist
BufferedWriter out = new BufferedWriter(fstream);
out.write("Save your data which you want to save");
out.write("Save your data which you want to save");
out.close();
fstream.close();
It might not be the best idea as:
You may have problems with re-using this file
You may run into a situation when 2 or more threads will be concurrently writing to the file and it will result in data loss, file corruption or IO Exception
I would recommend adding your "Test Case", "Dates" and "Numbers" variables to JMeter's .jtl results file instead using Sample Variables property like:
Given you have 3 variables: TestCase, Date and Number
Add the following line to user.properties file (it is located under /bin folder of your JMeter installation)
sample_variables=TestCase,Date,Number
On next JMeter restart you will see "TestCase", "Date" and "Number" variable values appended to .jtl results file.
It is also possible to pass the setting as a command-line parameter like:
jmeter -Jsample_variables=TestCase,Date,Number -n -t /path/to/testplan.jmx -l /path/to/results.jtl
References:
Sample Variables
Apache JMeter Properties Customization Guide
I would like to share my experience using BeanShell PostProcessor to write variables to a file.
I captured few responses from the server using "Regular Expression Extractor" and saved it to few variables Var1 & Var2 and when I wrote these variables to a file using "BeanShell PostProcessor", I have observed that it works fine when we run a test with single thread but with multi thread test, the data was being lost.
Say When I inject 100 transactions, I was able to see only 97 records in the output file.
So the best option is to set sample_variables in user.properties file so that you can get the variables out in your .jtl results file.

Resources