10

I need to do load testing on in memory databases.

I want to use JMeter and am under the impression I need to write a class that implements JavaSamplerClient.

I simply have no idea where to start. What the JMeter website has to offer helps me very little. This is my first time doing something like this and I have been lost for days!

So maybe you can help by explaining like the basics of how my class should be set out? Packages I might need to import? Because whenever I try to tell it to implement JavaSamplerClient I get an error.

Also maybe a brief summary on how it all works? Like is a method run as many times as specified in JMeter? Or what is actually happening here?

Ree
  • 863
  • 2
  • 18
  • 38

3 Answers3

16

To use Java Request in JMeter you must create a Java class that inherits from JavaSamplerClient. To do that, you must download two jar files and add them to classpath if you are working with Eclipse. This two jar files are ApacheJMeter_core.jar and ApacheJMeter_java.jar An your class will look like that:

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
public class javaRequest extends AbstractJavaSamplerClient {

        @Override
        public void setupTest(JavaSamplerContext context){
        // TODO Auto-generated method stub

        super.setupTest(context);
        }
        @Override
        public Arguments getDefaultParameters() {
        // TODO Auto-generated method stub  


        }
        @Override
        public SampleResult runTest(JavaSamplerContext arg0) {
            // TODO Auto-generated method stub

            SampleResult result = new SampleResult();

                boolean success = true;

                result.sampleStart();

                // Write your test code here.

                //


                result.sampleEnd();

                result.setSuccessful(success);

                return result;

        }
        @Override
        public void teardownTest(JavaSamplerContext context){
            // TODO Auto-generated method stub
            driver.quit();
            String verificationErrorString = verificationErrors.toString();
            if (!"".equals(verificationErrorString)) {
                fail(verificationErrorString);
                System.out.println(verificationErrorString); 
            }
            super.teardownTest(context);
        }
}

For more informations you can visit this link http://www.javacodegeeks.com/2012/05/apache-jmeter-load-test-whatever-you.html/comment-page-1/#comment-8288 and this page too How to use CSV Data Set with junit request test in jmeter

Giancarlo Romeo
  • 663
  • 1
  • 9
  • 24
Imen CHOK
  • 930
  • 1
  • 12
  • 20
  • Nice. How one can access the input and set value to output variables, so that other steps can access this data set inside the code? – Rao Aug 18 '16 at 01:54
13

Your custom class needs either implement JavaSamplerClient or to extend AbstractSamplerClient.

The absolute minimum is runTest() method implementation.

I'd recommend to look into sources for existing JavaTest and SleepTest Java Request samplers:

  • /src/protocol/java/org/apache/jmeter/protocol/java/test/JavaTest.java
  • /src/protocol/java/org/apache/jmeter/protocol/java/test/SleepTest.java

Sources are available from JMeter download page

Or there is a couple of guides which have example of simple working Java Requests.

See

  1. Beanshell vs JSR223 vs Java JMeter Scripting comparison benchmark - for something very basic like generating large random string
  2. WebSocket Testing With Apache JMeter - for fully functional Websocket client implementation via Java Request

After compiling your classes package it to .jar and drop to /lib/ext folder of your JMeter installation. Your class should be available from Java Request drop down.

Hope this helps.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Saw your comparison article and it very useful. In the java code same that is available there does not address how to access the input variables or set data to output variables, so that these can accessed in other samplers. Can you please shed some light on it? – Rao Aug 18 '16 at 01:58
  • For JSR223 way you have `vars` shorthand which stands for [JMeterVariables](https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html) class instance which provides read/write access to JMeter Variables. See [How to Use BeanShell: JMeter's Favorite Built-in Component](https://www.blazemeter.com/blog/queen-jmeters-built-componentshow-use-beanshell) guide for explanation of pre-defined vars For JavaRequest Sampler you can use [JMeterContextService.getContext().getVariables();](https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContextService.html#getContext()) – Dmitri T Aug 18 '16 at 14:44
2

Dmitri and Imen's answers are correct.

Here is a working example project.

It uses Gradle and includes instructions on how to build and execute it. It should be a good reference point.

https://github.com/dgroomes/jmeter-playground

Also, here is the official JMeter documentation on making your custom plugin available in the JMeter tool: https://jmeter.apache.org/usermanual/get-started.html#classpath.

David Groomes
  • 2,303
  • 1
  • 21
  • 23