.NET Core Performance Testing using Apache JMeter – Part 4

Introduction

In previous posts on this topic, we learned how JMeter can help us in performance testing our applications. I am using .NET Core for the demos, but you can use APIs written in totally different languages as well.

In this post, we will learn various other JMeter components and configurations and will also see couple of demos. I believe that with all this information, you’ll have good level of information to start setting up performance tests for your APIs and you will be able to explore different JMeter options easily on your own.

JMeter Plugins

jemeter-plugins.org has a lot of plugins useful for various purposes. Those are typically JAR files and are copied to lib/ext folder. However there is a very handy plugin called Plugins Manger which you can install to easy manage all other plugins for you.

JMeter Items

We saw few items e.g. Test Plan, Thread Groups etc in earlier posts. There are many more items and/or configurations in JMeter which you can use as per your particular testing needs. I will describe few of those in this post, however we will not go into much details. You can find good example and description of those on the official website, if you want to explore more:

Setup Thread Group

This is a special thread group that can be used to perform actions before executing regular thread groups.

Tear-down Thread Group

This is opposite to setup thread group. It executes after excuting regular thread groups. You can use it for disposing objects or clean up purposes.

Ramp-Up Period

Time, JMeter should take to create the Users.

Configuration Elements

Used to setup default configurations and variables for later use by other components. You can place it under any component. Few common are:

  • CSV Data Set Config
    • Use to read the content of a CSV file and turn it into variables.
  • Header Manager
    • Allow us to add/override HTTP Request Header.
  • User Defined Variables
    • Allow us to define a set of variables. These variables are processed once at the start of the test and are shared b/w thread group.

Demo (HTTP Request Defaults)

Let’s add serverName and port as default, so we don’t have to repeat this info for every request. We will place this element (HTTP Request Defaults) under TestPlan element, to all apply it to all thread groups we can add:

Here is how it looks after adding the serverName and port info:

Now, we don’t have to add this info for individual HTTP Requests. It will be automatically applied to all thread groups.

Logic Controllers

This component let us customize the logic to decide when to send requests e.g. when a condition becomes true.

Built-in Logic Controllers

  • If-Controller
    • Allow to control whether its children run or not.
  • Transaction-Controller
    • Allow to measure the overall time taken to perform its nested elements.

We can combine many Logic Controllers to simulate complex scenarios.

Timers

JMeter executes requests in sequence and without pauses. Timers allow you to introduce delay (pauses) b/w requests. Specifically they introduce delay before each sampler in their scope, simulating the time user taken to perform an action on web page and to avoid overwhelming the server with unrealistic load.

It can added on any level of the tree. Usually they are added as children of samplers or controllers to which they apply.

Assertion

  • Once server response is received, you can test if it contains some particular text.
  • Assertion allows you to validate a a response as expected.
  • Can be added at the thread group controller or sampler level.
  • JMeter offers different type of assertions, you can use as per your requirements

Let’s see a demo of this next:

Demo (JSON/YAML Path Assertion)

If you remember from previous posts the following REST endpoint:

We will setup an assertion that our response contains the specific data e.g. userName in response has value of jon. Lets add an assertion to our test as shown below:

Next, we will configure the assertion as shown below:

Execute the Test Plan and if everything looks green, then our assertion is successful. However, just to confirm, I will change the expected value to jon1 and following screen shows that now we have failing assertion:

I will change it back to correct expected value (i.e. jon in this case).

This is a very simple assertion example, feel free to explore other assertion types as well.

Here is few other example for your reference (notice that Assert JSON Path setup):

Execution Order and Rules

Here are the general execution orders of various JMeter components:

  • Ordered
    • Controllers and Samplers
  • Hierarchical (Scoped)
    • Everything else (e.g. config elements, assertions, timers, etc.)

Execution Order

  • Config Elements
  • Pre processors
  • Timers
  • Logic Controllers / Sampler
  • Post Processor
  • Assertion
  • Listeners

Config Elements Rules

  • The user defined variables config element is processed at the start of test, no matter where it is placed.
  • A configuration element inside a tree branch has a higher precendene than another element of the same type in an outer branch, but this also depends on types.

Generating Reports

So far, we have been running our tests from the GUI, however it is recommended to run tests from command line. Command line can also help us generating various test reports and this process can be automated as per your requirements.

To generate report, open the command line and execute following command:

jmeter -n -t apipart4.jmx -l resultFile.csv -j logfile.log -e -o c:/testReport

Here is the details about various command switches:

Now, if we check the output directory, we can see the generated report files:

Open the index.html file and you can see a very detailed report of our JMeter tests:

Summary

In this post, we learned different JMeter components and how they can help us in setting up complex test scenarios and same time help us simplifying management of those. We learned about Assertions and also how to generate detailed test results report using command line.

You can download the sample JMeter Test Files from this git repo.

There are a lot more components and configurations you can use for performance testing of your application. Let me know of you comments or questions. Till next time, Happy coding.

My Recent Books


Discover more from Hex Quote

Subscribe to get the latest posts sent to your email.

Discover more from Hex Quote

Subscribe now to keep reading and get access to the full archive.

Continue reading