This beginner-friendly subproject creates a custom JMeter "Sampler" to exercise some custom Java code.
Beginner Friendly
This subproject is friendly to Java developers who are new to JMeter. However, this project is somewhat contrived
because it does not require additional Java library dependencies. A real-world Java project would likely require additional
library dependencies. Venture over to the with-dependencies/
subproject for a more sophisticated JMeter project that
shows how to incorporate additional library dependencies and some other more advanced features.
This project creates a custom JMeter "Sampler" to exercise some custom Java code. Why is this useful? JMeter is a featureful load testing tool and is equipped with out-of-the-box controls to load test HTTP servers and databases via JDBC connections. But it doesn't have to stop there! With a custom sampler, you can instrument JMeter to load test any arbitrary thing that you can invoke using Java code. Want to load test a sorting algorithm you wrote in Java? You can do that!
Follow these instructions to build our example project and make its .jar
file available to jmeter
:
- Use Java 17
- Install JMeter
- Download it at the offical site or use SDKMAN!.
- Build the project into a
.jar
file:-
./gradlew jar
-
- Move the
.jar
into your JMeter installation'slib/ext/
directory- For example, on my computer (it will be a different path on your computer!):
cp build/libs/custom-sampler.jar ~/.sdkman/candidates/jmeter/5.6/lib/ext
- For example, on my computer (it will be a different path on your computer!):
- Open the JMeter GUI:
-
jmeter
-
- Next, follow these instructions to create a simple JMeter "Test Plan"
- Right-click "Test Plan" in the left-hand menu
- On the menu that appears, navigate through and select "Add" > "Threads (Users)" > "Thread Group"
- Right-click the "Thread Group" element that appears in the tree on the left
- On the menu that appears, navigate through and select "Add" > "Sampler" > "Java Request"
- On the "Java Request" screen that appears, click the dropdown menu
- You should see our custom sampler, "dgroomes.EchoSampler" (like the screenshot below)
Run the test plan using JMeter's CLI mode with jmeter -n -t echo-sampler-test-plan.jmx -l log.jtl -e -o test-report
.
JMeter recommends using CLI mode to execute test plans and GUI to define and edit test plans.
Open the test report in the browser to view the results of the test run! For example, on macOS the open
command will
open a .html
in your default browser. Use open test-report/index.html
.
Before executing another run after the first run, you will have to delete the old log.jtl
and test-report/
directory. Do so with rm log.jtl; rm -rf test-report