Load testing with JUnit using Zerocode framework

Igor Vlahek
5 min readAug 19, 2019

--

Photo by M. B. M. on Unsplash

Recently, I have tried to find a load testing tool that supports reusing your existing tests written in JUnit. I did some research, and I stumbled across the Zerocode framework. And I must admit, I am thrilled how easy it is to write a load test using Zerocode framework.

Zerocode

Zerocode is a community-developed, free, open-source API automation and load testing framework built using Java JUnit core runners for Http REST, SOAP, Kafka, RDBMS and much more. It enables to create and maintain test-cases with absolute ease. In this blog post, we will focus on the load testing part of the framework.

Zerocode is very simple and it is easy to learn load testing framework. To write and run your load test all you need is your IDE. Tests are written as JUnit tests and tests are run as JUnit tests. That said, you can run your load test in your IDE, as a part of your build process or as a separate Java application. The notion of writing and running in your IDE is the thing that attracted me the most. I am a developer. I write my code in IDE. I write my test code in IDE and run in it in IDE. And the notion that I can write and run my load test in IDE is what thrilled me the most. Also, as said before, you can reuse your JUnit test when defining a load test scenario using Zerocode framework.

In this post, we will focus on the load testing feature of the framework. We will show how to write a load test using Zerocode on a simple example. We will use maven as a build tool. You can achieve the same with the Gradle. All of the code snippets we will show are located in the src/test folder.

The text is accompanied with the test project hosted on the GitHub.

Writing your load scenario consists of the following:

  • Start with JUnit test,
  • Define the load test scenario (Java class + properties file),
  • Enrich your JUnit test with Zerocode annotations,
  • Run load test,
  • Collect data,
  • Extract useful information from the data.

Start with JUnit test

Let’s say we have written some functionality and a test accompanied with the functionality. UatTest class will act as our UAT test for the functionality. In this test, we send GET to the www.google.com and assert response status code in the end.

Define load test scenario (Java class + properties file)

The next step is to define the load test scenario. To do so we need to create properties file with the following content and put it in the test/resources folder:

//my_load_config.properties//number of users
number.of.threads
=30
//that will send request distributed in the ramp-up period
ramp.up.period.in.seconds
=2
//and we will repeat this test 10 times
loop.count
=10
//inform library not to generate Html report (time-consuming)
interactive.html.report.disabled
=true

In the application properties, we have defined how many users will we spawn in a certain time period that will send some request to the certain endpoint. The only thing left is to define what those users are going to execute in that period. We will define that in the Java test class as follows:

Enrich your JUnit test with Zerocode annotations

At this moment everything is set for the load test. If you run LoadTest class, ZeroCodeLoadRunner will spawn 30 threads that will each, in a 2 seconds time frame, execute request specified in the UatTest class and that scenario will be repeated 10 times. If you run it like this, you won’t get any results. ZeroCodeLoadRunner only spawns users and executes the request. We must enrich our JUnit test with ZerocodeCodeUnitRunner that will capture the request response time.

Current status of our test project:

Run load test

Running load test is easy as running JUnit Test. Open LoadTest class, right-click and click Run “Load Test”. Sit back and wait will the test is finished.

Collect data

Load test data is saved in the target/zerocode-junit-granular-report.csv file. The report is in the CSV file format and for each request, it collects the following data:

Let’s be honest. This file does not contain any useful information. You would expect you would get something like this:

  • Total number of requests
  • Average response time (ms)
  • Total time taken to execute
  • Requests per seconds
  • Requests per minutes
  • Requests per hour
  • Requests per 24 hour
  • Max duration (ms)
  • Min duration (ms)
  • Standard deviation (ms)
  • 95th percentile (ms)
  • 99th percentile (ms)
  • percentage of tests failed
  • percentage of tests passed

You don’t have any statistics, but you do have data containing requestTimestamp, responseDelay, responseTimeStamp and the result of each request. From this data, you can extract all the statistics you need.

Extract useful information from the data

On the Zerocode site, you can find a sample report generated in Excel. I used that and upgraded it a bit. You can find example of the report here. You need to import CSV file generated by the Zerocode in the Excel and you will get the following information and graph:

Response times graph
Statistic data extracted from the response times

Conclusion

I will not compare this tool with the other tools on the market. I am sure that this is not the best tool in the market, but for most of the firms, it will be enough. With this tool, you can quickly see whether you have implemented something wrong, or you forgot to put some index on the database or something else. And for that this tool is perfect. Tests are written quickly and in an environment (IDE) where you would ordinarily write your application logic. There is no learning curve. Everything is known and set for the development of your load tests.

--

--