JMeter 101

Posted by ChenRiang on June 13, 2021

Performance testing is a test technique that focuses on how the system behaves and performs. In real life, many software development teams tense to ignore this and only carry it out when it is requested. As a result, they always get “surprise” (e.g. system malfunction when under heavy traffic) in production. Thus, IMHO performance testing is a very critical and essential test that should be conducted on every enterprise application.

Over the year, I have carried few performance tests and in this article, I’m going to share with you the tool that I used to generate load.

JMeter

Apache JMeter is a very famous open-source tool that has been around for 20+ years. It is written in pure Java and this allows it to be run on any platform. It was originally designed for testing Web Applications but has since expanded to allows testing almost any application and protocol, enabling users to create tests by using a desktop application that is compatible with any OS platform.

Download: here

Springboot Application

I created a simple Springboot application which expose an endpoint /load.

1
2
3
4
5
6
7
8
9
10
11
@PostMapping(value = "/load")
public ResponseEntity response(@RequestBody Map<String, String> values){
    for (Map.Entry<String, String> entry : values.entrySet()) {
        if (values.containsKey("error")) {
            System.out.println("Bad Request with error");
            return ResponseEntity.badRequest().build();
        }
        System.out.println("Key : " + entry.getKey() + " | Value : "+ entry.getValue());
    }
    return ResponseEntity.ok().build();
}

Source code : here

Create Test Plan

In this article, we going to create a JMeter test plan that:

  1. Load data from CSV data file.
  2. Send request to Springboot application.
  3. Generate report.

Test Plan


JMeter provides a GUI that eases us to create the test plan(.jxm) without writing any line of code. However, it is not designed for running real performance tests.

  1. Lunch JMeter GUI by execute jmeter.bat(window) / jmeter.sh (linux).

  2. Right click on Test Plan and select Add > Threads (Users) > Thread Group.

    Add Thread Group

    • Thread group is the first element that must be created in any test plan.

    • All controllers and samplers must be created under a thread group.

    • Thread group element controls the number of threads(users) that JMeter will create to execute the test.

  3. Below is the default value for Thread Group. We will maintain the value as it is at the moment.

    Thread Group

    • number of threads - how many thread that required to run the test plan. Each thread is completely independently of other test threads and this could used to stimulate the concurrent connection to the application.

    • ramp-up period - how long should JMeter spent to create the full number of threads that specified.

      • number pf thread = 10
      • ramp-up period = 100
      • JMeter will start 1 thread every 10 second (100/10).
    • loop count - how many time thread will execute the test plan.

      • number of thread = 10

      • loop count = 20

      • JMeter will execute the test plan 200 times(10 x 20).


  4. Create a data ingestion element by right click on Thread Group and select Add > Config Element > CSV Data Set Config.

    Add CSV File element

  5. Click Browse to select the data file.

    Configure Data File location

    • If the data file does not have any header, you will need to specify header name on the Variable Names column.

    • In this example, our data has two headers : name and greeting. (See the sample CSV data here)

  6. Then, we will 9need to create the HTTP Request element. Right click on Thread Group and select Add > Sampler > HTTP Request.

    Add HTTP Request

  7. We need to configure the HTTP Request and point it to the Springboot application that created.

    Configure HTTP Request

  8. As we are sending JSON payload, we specify it in HTTP Header. Right click on HTTP Request and select Add > Config Element > HTTP Header Manager.

    Add HTTP Header Manager

  9. Set Content-Type to application/json.

    ConfigureHTTP Header Manager

  10. We also want to assert all the request getting a success response. To do that, right click on HTTP Request and select Add > Assertion > Response Assertion.

    Add Response Assertion

  11. Set Response Code is Equals to 200.

    Configure Response Assertion

  12. To view the response of each request. Right click on Thread Group and select Add > Listner > View Result Tree.

    View Result (Detail)

  13. We can try validate the work we done so far by clicking the green color Start button. Below is the sample screenshot.

    View Result Sample

  14. After validate the test plan, we going to run a end to end test. Before that, we need to add a report element to generate the statistic summary of our result.

    Generate report

  15. Edit the test plan with more threads and loop.

    Generate report

  16. Hit Start button and view the result.

    Sample GUI Report

  17. (Optional) We can parameterize some of the configuration with ${__P(<variable name> , <default value>)} and pass as argument in non-gui mode. Normally, I will parameterize the threads group as below :

    Parameterize variable

  18. Hit Save and done :)

Execute Test

Once the test plan is ready. We can run the real performance test in non-gui mode (CLI mode).

  1. Open Command Prompt (Window) / Terminal (Linux).

  2. Go to JMeter’s bin folder.

  3. Execute the test.

    jmeter -n -t <test plan jxm file> -Jthread=10 -Jloops=100 -l log.jtl -e -o result

  4. The command above will generate a HTML report in result folder.

Some useful option when running jmeter in non-gui mode.

Option Explaination
-n This specifies JMeter is to run in cli mode.
-t This specify test plan location.
-l This specify the location to generate the JTL log file.
-e Generate HTML report after load test
-o This specify the location to generate output folder

Conclusion

In this article, we only cover a very simple usage of JMeter. It has lots more useful functions that will make your life easier during the performance test. I will write more about it future :)