Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Java Classes with JMeter

Tags:

jmeter

I'd like to test a Java Service Call. My first attempt was to use the "Java Request Sampler" The docu says

This sampler lets you control a java class that implements the JavaSamplerClient interface.

I'm not sure how to get the org.apache.jmeter.protocol.java.sampler.JavaSamplerClient Interface as there is no Maven Artifact nor provided binaries on the JMeter side. Just a JMeter Maven Plugin (which is not what I'm looking for). I could install the needed binaries in my local Maven Repository, I Just don't know where they are available.

Wondering if anybody at all is using the "Java Request Sampler" ?

P.S. Maybe I should try the JUnit Sampler

like image 955
Martin Dürrmeier Avatar asked Mar 04 '10 13:03

Martin Dürrmeier


People also ask

Can we use Java in JMeter?

Platform independent: JMeter is written and developed using java, so it can run on any environment / workstation that accepts a Java virtual machine, for example - Windows, Linux, Mac, etc. Mail: POP3. Support multi-protocol: JMeter supports protocols such as HTTP, JDBC, LDAP, SOAP, JMS, and FTP.


1 Answers

We are building with ANT and are using the JARs, which are located in the binary file from JMeter under \lib\ext\. The AbstractJavaSamplerClient which is used for the Java Request Sampler is located in the file \lib\ext\ApacheJMeter_java.jar. For working with this abstract class, you also have to import the JAR file \lib\ext\ApacheJMeter_core.jar, which is (for example) holding the class SampleResult.

After building our Java class we put the resulting JAR file also in the folder \lib\ext\. If our class uses any dependency (3rd part JAR), we put it in '\lib' folder. After that, you can start JMeter and you're able to select your Java class in a Java Request Sampler.

Here is an example of such a Java Request Sampler:

public class JavaRequestSamplerDemo extends AbstractJavaSamplerClient {

  @Override
  public SampleResult runTest(JavaSamplerContext ctx) {
    JMeterVariables vars = JMeterContextService.getContext().getVariables();
    vars.put("demo", "demoVariableContent");

    SampleResult sampleResult = new SampleResult();
    sampleResult.setSuccessful(true);
    sampleResult.setResponseCodeOK();
    sampleResult.setResponseMessageOK();
    return sampleResult;
  }  
}
like image 88
Steve Avatar answered Oct 29 '22 08:10

Steve