I am new to Jmeter. create a Jsr223 postprocessor and select language as Bsh.
import java.util.Date;
import java.text.SimpleDateFormat;
long mill = System.currentTimeMillis();
log.info(mill);
output:
ERROR - jmeter.extractor.JSR223PostProcessor: Problem in JSR223 script JSR223 PostProcessor javax.script.ScriptException: Sourced file: inline evaluation of: ``import java.util.Date; import java.text.SimpleDateFormat; long mill = System.c . . . '' : Error in method invocation: Method info( long ) not found in class'org.apache.log.Logger' : at Line: 5 : in file: inline evaluation of: ``import java.util.Date; import java.text.SimpleDateFormat; long mill = System.c . . . '' : log .info ( mill )
in inline evaluation of: ``import java.util.Date; import java.text.SimpleDateFormat; long mill = System.c . . . '' at line number 5
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:92)
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:206)
at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor.java:42)
at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:776)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:489)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:410)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:241)
at java.lang.Thread.run(Unknown Source)
please advise a fix. thank you!
First of all, are you aware of __time() JMeter function which can output current time in the different formats?
If you still want to do it in Beanshell be informed that you cannot print a Long value directly to jmeter.log file, you need to cast it to String first using one of the following approaches
log.info(String.valueOf(mill));
log.info(Long.toString(mill));
log.info("Current time is: " + mill);
You can get a "good" Beanshell error by putting your code inside the try block like:
Related
I use BeanShell code loading 100s of sql files in jmeter:
import org.apache.commons.io.FileUtils;
File folder = new File("D:\\sql99");
File[] sqlFiles = folder.listFiles();
for (int i = 0; i < sqlFiles.length; i++) {
File sqlFile = sqlFiles[i];
if (sqlFile.isFile()) {
vars.put("query_" + i,sqlFile.getName(),
FileUtils.readFileToString(sqlFiles[i]));
}
}
but get error info :
17:42:03,301 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.commons.io.FileUtils; File folder = new File("D:\sql99"); Fi . . . '' : Error in method invocation: Method put( java.lang.String, java.lang.String, java.lang.String ) not found in class'org.apache.jmeter.threads.JMeterVariables'
I want to get each sql execute time in jmeter results tree. How to fix code?
Thanks!
You're trying to call JMeterVariables.put() function which accepts 2 Strings as the parameters passing 3 Strings
The correct syntax is vars.put("variable-name", "variable-value"); so you need to decide how to amend this line:
vars.put("query_" + i, sqlFile.getName(), FileUtils.readFileToString(sqlFiles[i]));
so it would contain only 2 parameters instead of 3.
Also since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting mainly for performance reasons so it might be a good option for switching (the same code will work in Groovy without changes assuming you fix the issue with vars.put() function call)
I'm getting error while running the bellow java code in Jmeter JSR223 sampler
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import net.minidev.json.JSONArray;
String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";
String jsonPath = "$..book[?(#.author == 'Nigel Rees')].title";
Configuration config = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
JSONArray authorsArr = JsonPath.using(config).parse(json).read(jsonPath);
System.out.println(authorsArr.get(0).toString());
json-path-2.4.0.jar is added under ..\lib\ext and assume it is automatically loaded on Jmeter start.
The above code is checked in IDE and is running fine.
JSR223 Error
2021-02-02 17:19:10,580 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : Typed variable declaration : Error in method invocation: Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.Configuration' : at Line: 11 : in file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : .addOptions ( Option .DEFAULT_PATH_LEAF_TO_NULL )
in inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' at line number 11
javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : Typed variable declaration : Error in method invocation: Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.Configuration' : at Line: 11 : in file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : .addOptions ( Option .DEFAULT_PATH_LEAF_TO_NULL )
in inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' at line number 11
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_241]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:69) [ApacheJMeter_java.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) [ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
Change "Language" to groovy, if you choose java it isn't really Java, it's Beanshell which
is not fully Java-compliant
has much worse performance comparing to Groovy
So according to JMeter Best Practices you should switch to Groovy since JMeter 3.1. More information: Apache Groovy - Why and How You Should Use It
Change this line:
String jsonPath = "$..book[?(#.author == 'Nigel Rees')].title";
to this one
String jsonPath = '$..book[?(#.author == \'Nigel Rees\')].title'
as your Java syntax slightly conflicts with Groovy GString Template Engine
You should not require any extra .jars, the code will work on vanilla JMeter instance
Also be aware that Groovy has built-in JSON support
I am using the below script in a Beanshell Postprocessor
import java.io.*;
File f =new File ("C:\Users\xxxxx\Desktop\testresults.csv");
FileWriter fw=new FileWriter(f,true);
BufferedWriter bw=new BufferedWriter(fw);
var r=prev.getResponseCode();
if (r.equals("200"))
{
bw.write("Test Passed");
}
else
{
bw.write("Test failed");
}
bw.close();
fw.close();
But I am getting the below error
BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.io.*; File f =new File ("C:\Users\xxxxx\Desktop\testresults.csv") . . . '' Token Parsing Error: Lexical error at line 2, column 23. Encountered: "U" (85), after : ""C:\".
What could cause the above error.
You need to escape a backslash with a backslash like:
C:\\Users\\xxxxx\\Desktop\\testresults.csv
or use a forward slash instead:
C:/Users/xxxxx/Desktop/testresults.csv
A couple more hints:
Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting
If you run your test with 2 or more concurrent threads they will be writing into the same file resulting in data corruption due to a race condition so maybe it worth considering switching to Flexible File Writer instead
Change to JSR223 Post Processor and write as one line (groovy default)
new File("C:\\Users\\xxxx\\Desktop\\\testresults.csv") << (prev.getResponseCode().equals("200") ? "Test Passed" : "Test failed")
For encrypting the input I am using the below code but facing the error
import java.util.Base64;
String plainPassword=vars.get("PW");
log.info(plainPassword);
String encodedPassword = new String(Base64.encodeBase64(plainPassword.getBytes()));
vars.put("encodedpassword", encodedPassword);
log.info("encodedpassword");
ctx.getCurrentSampler().getArguments().getArgument(0).setValue(encryptedpassword);
Error says:
Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval
Sourced file: inline evaluation of: import java.util.Base64;
String plainPassword=vars.get("PW");
log.info(plainPa . . . : Typed variable dec
I fail to see encodeBase64() function in java.util.Base64 JavaDoc so double check the source from which you copied and pasted it.
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so you can change 2nd line of your "script" to something like:
String encodedPassword = plainPassword.bytes.encodeBase64().toString()
Full correct code just in case:
String plainPassword=vars.get("PW");
log.info(plainPassword);
String encodedPassword = plainPassword.bytes.encodeBase64().toString();
vars.put("encodedpassword", encodedPassword);
log.info(encodedPassword);
sampler.addNonEncodedArgument('', encodedPassword, '');
Demo:
More information: Apache Groovy - Why and How You Should Use It
I am getting the following error when I use a BeanshellPostProcessor to regex some data and write to file:
2015/06/11 12:11:19 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: `` import java.io.FileOutputStream; import java.util.Map; import java.u . . . '' Token Parsing Error: Lexical error at line 10, column 45. Encountered: "d" (100), after : "\"c:\\"
2015/06/11 12:11:19 WARN - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: `` import java.io.FileOutputStream; import java.util.Map; import java.u . . . '' Token Parsing Error: Lexical error at line 10, column 45. Encountered: "d" (100), after : "\"c:\\"
My code:
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;
String entirePage = new String(data);
FileWriter fstream = new FileWriter("c:\\downloads\\results.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
Pattern totalNetValue = Pattern.compile("totalNetValue\">([^\"]+)</span>");
Matcher mTotalNetValue = totalNetValue.matcher(entirePage);
mTotalNetValue.find();
//out.write(${date});
out.write(mTotalNetValue.group(1));
out.write("\n");
out.close();
Any ideas what's wrong with my code? Thanks :-)
How do you read the code into the bsh interpreter? It sure looks like the double backslashes are reduced to single ones.
As a quick fix, just use forward slashes instead of backslashes, e.g.
FileWriter fstream = new FileWriter("c:/downloads/results.txt",true);