Is there any diffrence between hmacSha1Hex and hmacSha1? - jmeter

I am using org.apache.commons.codec.digest.HmacUtils.hmacSha1Hex("secretkey", "message");
and getting a long string in output.
i tried executing org.apache.commons.codec.digest.HmacUtils.hmacSha1("secretkey", "message"); but facing an error
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``String hmac_Sha1 = org.apache.commons.codec.digest.HmacUtils.hmacSha1("secretkey . . . '' : Typed variable declaration
2016/11/29 17:09:07 WARN - jmeter.modifiers.BeanShellPreProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: String hmac_Sha1 = org.apache.commons.codec.
Basically i want to know the length of output for both functions
for hmacSha1Hex output is like HMAC SHA1 HASH: 0ff4e6a0b47baebe19c392e706fffaa13664a1df
I am expecting output like btuU9CPfMQMswNgxPIMjRkTjfks%3D difference is of length

The answer you're looking for is in HmacUtils JavaDoc:
HmacUtils.hmacSha1Hex - is a String
HmacUtils.hmacSha1 - is a byte[]
You can convert byte array to string like:
String s = new String (your byte array here);
I would also recommend using JSR223 Sampler and Groovy language instead of Beanshell, it is compliant with modern Java features and has better performance.

Related

Jmeter BeanShellSampler error: Error invoking bsh method: eval import org.apache.commons.io.FileUtils

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)

Error on accessing Output Variable in beanshell preprocessor in Jmeter

I am unable to print 'Output Variable' value of foreach Controller in Beanshell Pre/Post-processor in Jmeter.
log.info("inside hash"+ ${current_file} ); //current_file is the Output variable name defined in foreach controller and has the value of current file path.
File file=new File(${current_file});
byte[] content = FileUtils.readFileToByteArray(file);
Whenever I execute the tests, I get this error:
2021-12-15 19:58:25,208 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: ``import org.apache.commons.io.FileUtils; import org.apache.jmeter.services.FileSe . . . '' Encountered "( "inside hash" + C :" at line 4, column 9.
Can anyone help me fix this error?
Don't inline JMeter functions or variables in form of ${current_file}, use vars shorthand for JMeterVariables class instance instead
Something like:
String current_file = vars.get("current_file");
log.info("inside hash"+ current_file );
File file=new File(current_file);
Don't use Beanshell, since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting, there is a chance that your code will just start working after switching to Groovy or at least you will get more informative errors.

Beanshell Preprocessor in jmeter getting an error

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

jmeter - Beanshell post processor

Getting below error in log:
2020-06-24 13:23:51,091 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: //to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler 2020-06-24 13:23:51,091 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: //to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler 2020-06-24 13:23:51,154 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: //to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler 2020-06-24 13:23:51,154 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: //to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler 2020-06-24 13:23:51,236 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: //to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler 2020-06-24 13:23:51,236 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of://to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler
Below is the code used in Beanshell postprocessor.
import java.text.DecimalFormat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
int tdNumber = ctx.getThreadNum();
int noThreads = ctx.getThreadGroup().getNumThreads();
long tdstart = ctx.getThread().getStartTime();
long tdEnd = ctx.getThread().getEndTime();
String respCode = ctx.getPreviousResult().getResponseCode();
String respCode = ctx.getPreviousResult().getResponseMessage();
String samplerName = sampler.getName();
Date date = new Date();
date.setDate(date.getDate());
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
String formattedDate = df.format(date);
log.info("sampler::"+ sampleName +"....ThreadNo::"+ (tdNumber + 1) +" ....Starttime::"+ formattedDate +
"....StatusCode::" + respCode +"....TotalThreads::"+ noThreads +" ....Iterationno::"+vars.getIteration());
undefined variable or class name: sampler
There is no sampler shorthand in the Beanshell PostProcessor, you should use one of the following alternatives:
prev.getSampleLabel(false)
ctx.getCurrentSampler().getName()
Also be aware that starting from JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating to Groovy on next available opportunity.
More information on this ctx, prev and friends: Top 8 JMeter Java Classes You Should Be Using with Groovy
Move to JSR223_PostProcessor which is the best practice and include also sampler
Migration to JSR223 PostProcessor+Groovy is highly recommended for performance, support of new Java features and limited maintenance of the BeanShell library.

BeanShell PostProcessor error Jmeter GUI works, command line doesn't

The following code is in a BeanShell PostProcessor works fine when I run a test using Jmeter on GUI mode. If I run it from the command line I get the following exception:
015/09/10 18:25:13 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``notFound="NOT_FOUND"; vars.put("AUDIT_CODE",notFound . . . '' : Typed variable declaration
2015/09/10 18:25:13 WARN - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``notFound="NOT_FOUND"; vars.put("AUDIT_CODE",notFound . . . '' : Typed variable declaration
Any idea why this could happen?
String notFound="NOT_FOUND";
vars.put("AUDIT_CODE",notFound);
String auditCode=vars.get("AUDIT_CODE");
if(auditCode.equals("NOT_FOUND")){
if (vars.get("AUDITS_RESULT_0")!=null){
String audit = vars.get("AUDITS_RESULT_0");
if(audit.contains("WAITING")){
String[] auditLineWithCode = audit.split("auditCode=");
String[] auditLineJustWithCode=auditLineWithCode[1].split(",");
vars.put("AUDIT_CODE",auditLineJustWithCode[0]);
log.info("AUDIT_CODE:"+vars.get("AUDIT_CODE"));
}
}
}
Change this line:
audit = vars.get("AUDITS_RESULT_0");
to
String audit = vars.get("AUDITS_RESULT_0");

Resources