Why You Must Use JMeter Assertions in Your Load Tests
Assertions in JMeter are used to compare actual test results to the expected results of requests. This post will explain that even though there are ways to get around using assertions, it is highly recommended to indeed utilize them.
Join the DZone community and get the full member experience.Join For Free
assertions in jmeter are used to compare actual test results to the expected results of requests. this post will explain that even though there are ways to get around using assertions , it is highly recommended to indeed utilize them.
when we plan load test workloads, assertions are generally not utilized. the primary reason is that in load or performance testing, the response status code serves as one of the main metrics in analyzing your results. there is not much of a need to verify the response data of each request. what we do need is the response time, latency, and response code. theoretically, we don't need to use jmeter assertions but, in practice, ignoring assertions might play havoc with the final results.
a 200 status code doesn't always mean success
in order to illustrate the importance of assertions in jmeter, i want to share a real case which happened to me while load testing a saas service.
before the release, i was asked to perform a load test of the saas company's website, which provides services in the tourism industry. the test scenario was not complex—it included a sign-in, some links, and a sign-out. seems pretty straightforward, and i wasn’t expecting anything to go wrong.
when the jmeter script was ready, i uploaded the jmx file into blazemeter and began to run the test. all the requests passed with the 200 status code and the average response time was less than 1000 ms, which means that the web application was super fast. increasing the number of concurrent users and performing the load test again did not change the average response time, and the application still loaded very quickly without any errors.
i began to think. on the one hand, it was good that the application is stable, but on the other, it was strange to me that the average response time remained the same.
before sending the load test report to my client, i decided to double-check the results locally. when all requests are successful, it's difficult to find the root cause, but after a long time investigating, i decided to add assertions to each request, and i was amazed at the results in the assertion results listener, which reveals the label under which all the assertions were taken. the listener displayed almost 90% of the requests as failed, but in the view results tree listener—which shows a tree of all sample responses, allowing you to view the response for any sample—everything was right.
i discovered that the root cause of the discrepancy was with the sign-in sampler. the csv data that was used for credentials was outdated, but instead of returning a 4xx status code (which would indicate it as unsuccessful), it was redirecting to a maintenance page with 2xx (successful) status code. the rest of the requests were also returning to the maintenance page content and the page was hosted on a cdn. while we were loading the company’s web servers, in reality all requests were sending us to a cdn.
as a result of this experience, we can conclude that a successful response status message by itself does not always indicate success, and that to solve this problem, jmeter assertions can help.
when to use jmeter assertions
the above case is not the only example of when jmeter assertions are important to use in load testing applications. for instance, in api performance testing, jmeter assertions are a must-have in any test script.
here are some recommendations of when to use jmeter assertions:
- with requests that use data from
"csv data set config"
- with http post/put/patch methods
- after signing in and signing out
- when 2xx status codes returned in both positive and negative testing
- in api performance testing
- in functional testing
- with soap/xml-rpc requests
word of warning: assertions consume memory
in the official jmeter documentation, it is written to "use as few assertions as possible" , and the main reason for not adding them to all samplers is resource consumption. using assertions irregularly in high-load testing will cause performance issues or even out of memory errors .
here is the list of assertions i would suggest you avoid using in your load tests:
to find the impact of the above assertions, i made the benchmarking test that of jmeter itself. i used a jmx file from a "jmeter performance evolution across versions" test.
- jmeter version: 2.13
- tomcat version: 6.0.39
- tomcat jvm: -xmx128m
- jmeter jvm: -xmx512m
- java version: “1.7.0_95"
- ubuntu: 14.04.3 lts
- jmeter and tomcat are on the same machine
- no particular os tuning
- simple test plan using tomcat examples
the test plan parameters:
- ramp-up period: 100 seconds
- number of threads: 1500
- duration: 10 minutes (so the test will run for just 10 minutes, coming to a forced stop at the 10th minute)
- startup delay: 7 seconds
1. benchmark testing without assertions
2. benchmark testing with response assertions
3. benchmark testing with xpath assertions
as you can see from charts above, the behavior of ram is stable in all tests. if we analyze the cpu results, we see that there are no big changes in the test with response assertions, but in the test with xpath assertions, the cpu consumption increased 10%. the test plan included only 4 samplers and 4 xpath assertions. adding more assertions will drastically increase cpu consumption, which will lead to performance issues.
jmeter assertions are a must-have in load and performance testing, especially when a server returns dynamic data with non-standard response codes. ignoring jmeter assertions while the server is returning static plain-text data might be ok, but extra additional verification is very recommended. a side effect of assertions is resource consumption, and you can't use each and every assertion in your load test.
i recommend balancing between performance and functionality while writing a test plan in jmeter. in distributed load testing, when you run a load test by yourself this can save significant time and money.
if you’d like to learn more, please sign up for our free online jmeter training course .
Published at DZone with permission of Bob Meliev. See the original article here.
Opinions expressed by DZone contributors are their own.