JSR223 Post Processor: ClassNotFoundException: org.apache.bsf.engines.java.JavaEngine - jmeter

I'm trying to use a JSR223 post processor in jmeter 2.11 with some java code and I keep getting this error:
org.apache.bsf.BSFManager:Exception:java.lang.ClassNotFoundException:org.apache.bsf.engines.java.JavaEngine
I've checked the bsf/bsh jar files, not sure what else I need to do to make this work. I've read that groovy is recommended but I'd really like this to work with java.

As per the very first lines from BSF Sampler documentation:
See the Apache Bean Scripting Framework website for details of the languages supported. You may need to download the appropriate jars for the language; they should be put in the JMeter lib directory.
And at the same place:
The BSF API has been largely superseded by JSR-223, which is included in Java 1.6 onwards. Most scripting languages now include support for JSR-223; please use the JSR223 Sampler instead. The BSF Sampler should only be needed for supporting legacy languages/test scripts.
So I would recommend considering the following (in the order from worst to best performance provided)
Beanshell Sampler (if your Java code is doing something "light")
JSR223 Sampler + groovy language - for "heavy" operations (again you'll need to have groovy jar in your JMeter CLASSPATH)
Write your own implementation of Java Request Sampler
Both Beanshell and Groovy support Java syntax with some limitations which can be easily worked around.
See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! guide which might help you to make right selection.

Related

Jmeter project documentation

We have been using the Jmeter for the API's functional and performance test activities.
Is there a possibility to generate the documentation (similar java docs, API docs) for the JMX projects that we have been developing?
Each JMeter's Test Element has "Comments" section which can be used for explanation of what this element is for:
It's possible to create a JMeter Test Plan using Java language and JMeter API, see JMeter Command Line Overview: 5 Ways To Launch a Test for more details. There is also jmeter-from-code sample project you can use as a reference, normal JavaDoc will be available out of the box
There is JMeter DSL project which provides easy and handy way of creating a JMeter test plan, however it doesn't fully support all the features. For basic test plans it should be faster and easier than point 2
There is Taurus tool which provides possibility to create JMeter scripts in a declarative manner using YAML and YAML naturally supports comments

how to declare global function in jmeter and use anywhere in beanshell

I'm facing with problem don't know how to create one Java function and call it to use anywhere because I don't want to write this function many times. Could anyone give me the best solution to do it?
If you still want to proceed with Beanshell (however since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language) - take a look at Beanshell configuration, there are properties like:
beanshell.init.file
beanshell.sampler.init
etc.
where you can place the "common" code which can be reused in the Beanshell test elements.
For Groovy as of JMeter 5.2.1 there are no equivalent properties, however you should be able to compile your Groovy code into a .jar file, put the file into JMeter Classpath and invoke the functions defined in the .jar from any JSR223 Test element. Check out How to Reuse Your JMeter Code with JAR Files and Save Time article for more details.
This approach can be applied to Beanshell as well if you still want to use it and find the properties not very convenient.

Jmeter cant read custom JAR depedencies

I wrote a Maven application that I want to use in my Jmeter BeanShell script.
The Maven application is using Google Guice 4.2.2 for dependecy injection and it is calling an API at the end (Code needs to perform some other operations before calling the API and that's why I am not using the JMeter Plugin). I am creating the uber JAR (Fat JAR) with maven-shade-plugin. When I run the Jar from the command line the application run successfully!
However, When I load the JAR in my Jmeter test plan and call my application main method in the Jmeter BeanShell I am getting the following error:
java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V
at com.google.inject.Key.ensureRetainedAtRuntime(Key.java:341) ~[load-testing.jar:?]
.
.
.
Now lots of other threads mentioned that this can be because of older Google Guava version (20.x older) but from the dependency tree I see the Guava version is 25.1-android and I can run my JAR successfully from the command line!!
Also, I list the classes in the Uber JAR by running jar -tf command and I can see that the com.google.common.base.Preconditions class is there.
I would appreciate it if someone can shed some light on this matter and help me to resolve this?
It seems that your fat-jar contains multiple versions of Guava (which may be caused by the shade-plugin).
Make sure to lock it to single version by using Mavens dependency management in each maven module. (https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management)
The Beanshell itself might be a problem, since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting, the reasons are in:
Groovy is more "modern" language which supports all modern Java features while with Beanshell you're stuck at Java 1.5 language level (no lambdas, no generics, no multi-catch etc.)
Groovy performance is much better comparing to Beanshell cause Grooovy engine implements Compilable interface and is capable of caching compiled scripts providing performance very close to native Java code
Groovy provides a lot of enhancements on top of standard Java SDK
Check out Apache Groovy - Why and How You Should Use It article for more details.
I have finally cofirmed my comment. The problem was my JAR Guava version was conflicting with Jmeter Guava version (which is 17.0) and Jmeter for some reason is picking up its own Guava version, after downgrading Guice version to 3.0 which does not depend on guava, I successfully ran my JAR through Jmeter.
There is still a mystery on why Jmeter is overriding my JAR Guava version with its own. I will create a ticket with Jmeter in order to find more about this mystery but this change solved my problem

JMeter - Why use JDBC PostProecssor over sampler

In JMeter test plan, why do I need JDBC post (/pre)proecssor when I can just use JDBC sampler?
If the relevant sampler will be before/after the request it will be same as JDBC Pre/Post processor
Also it seems more reusable option
By default JMeter does not include the time required to execute any Pre Processors into sample result. Same applies to Post-Processors and Timers (unless you are using Transaction Controller configured to do so)
So I would use Pre Processor if it assumes some pre-requisities setup and I am not interested in its results timings.
If I need to measure the time required to execute a request - I would go for Sampler
Check out A Quick Guide to JMeter PreProcessors article for comprehensive information on using Pre Processors in JMeter tests.

Are Java Request samplers as accurate as SOAP or HTTP samplers in jmeter?

Are Java Request samplers as accurate as SOAP or HTTP samplers in jmeter? For example, I run a java request sampler which reads from a csv file and handles entries in the file through code. This causes 0 throughput in the results.
However, when I use HTTP or SOAP samplers I get higher throughput since the entries of the csv file are handled one at a time through Jmeter not through the java code.
Any clues on how to solve this "inaccuracy" in the Java sampler results?
Related to this question is: if I use the Java Request Sampler Test class to merely call a static method in another java class. Is that a good practice or the whole logic should be inside the runTest method?
Thank you.
Java Request Sampler is pretty much alike any other sampler.
Make sure that you invoke sampleStart() and sampleEnd() methods at the beginning and in the end of your test.
See Java Request section of Beanshell vs JSR223 vs Java JMeter Scripting for example Java Request Sampler source code.

Resources