0

I was testing to use Apache Jmeter to load test a java class. I set up a Maven project and added Jmeter dependencies. I was using v5.4.1 JMeter.

 <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_java -->
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_java</artifactId>
            <version>5.4.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_core -->
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_core</artifactId>
            <version>5.4.1</version>
        </dependency>
    </dependencies>

And below is my simple java class for testing:

package jmeter.testing;

import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

public class ClassTester extends AbstractJavaSamplerClient {
    @Override
    public SampleResult runTest(JavaSamplerContext context) {
        SampleResult result = new SampleResult();
        result.sampleStart();
        for (int i = 0; i < 100; i++) {
            System.out.println("Testing Jmeter code...");
        }
        result.setResponseCode("200");
        result.setResponseMessage("Jmeter Test");
        result.setSampleLabel("test");
        result.sampleEnd();

        return result;
    }
}

I had Maven build this jar file and copied it to the /lib/ext directory. (My Jmeter was installed via brew..) And I didn't see this class in the Java Request Sampler somehow after I restarted JMeter. Could anyone guide me where was wrong? Thanks.

[INFO] Building jar: /Users/alex/Documents/IntelliJ/jmeter-util/target/jmeter-util-1.0-SNAPSHOT.jar

/usr/local/Cellar/jmeter/5.4.1/libexec/lib/ext
❯ ls -ltr
-rw-r--r--@ 1 alex  staff     2726 Nov 11 17:16 jmeter-util-1.0-SNAPSHOT.jar
Zhang
  • 39
  • 1
  • 6

3 Answers3

0

Another solution: You will have to add your jar into the local repository and add the dependency to your pom.xml.

Reference : How to add jar file to a maven project

Janesh Kodikara
  • 1,670
  • 1
  • 12
  • 23
0

Your code and your approach should be working, at least it does for me:

enter image description here

So check where your jmeter is pointing using which and ls -la commands, it might be the case you have some other JMeter in your PATH which is different from ../Cellar/jmeter/5.4.1/bin/jmeter

You can check out Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! article for more information on creating custom Java Request sampler and other scripting approaches.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Hi Dmitri, thank you for verifying it for me. I found the issue. I was using a different version of JDK (11) when I was building the jar. It worked after I re-built the jar using v17. – Zhang Nov 12 '21 at 18:59
-1

Can you copy the jar file to ${JMETER_HOME}/bin and check if it work?

lib/ext folder is for the Jmeter plugins. It's not for the utility jars. You can place the utility jars into the lib folder.

Your jar file need to be in the classpath. Please see more details on the classpath configuration

More details

Janesh Kodikara
  • 1,670
  • 1
  • 12
  • 23
  • I did uncommented user.classpath=../classes;../lib in the jemeter.properties file. I've tried to put the jar in lib/ext, lib, even /bin, none worked.. – Zhang Nov 12 '21 at 05:12
  • The solution will work when you start JMeter with jmeter.sh/jmeter.bat. Please check the other solution. – Janesh Kodikara Nov 12 '21 at 07:09