Tuesday, February 9, 2016

How to run existing unit test cases in parallel as part of early performance testing

If you already have junit test cases in place you can run them in parallel and create a concurrent load to identify the potential performance issues early in the development phase itself. This will save the effort, time and cost of fixing the performance issues later in the life cycle. At the same time this could be considered as one the ways where DevOps concept can easily be adopted where developers do their performance testing by themselves.

By doing this you can uncover;
  1. High response time taken to execute a method under load
  2. Synchronization issues including Deadlocks 
  3. Database issues if the method under test has DB calls
  4. Server resource health issues
  5. Violations of already defined performance SLAs

Tools you can use;

Contiperf with Eclipse - ContiPerf is a lightweight testing utility that enables the user to easily leverage JUnit 4 test cases as performance tests.
Contiperf can be downloaded or added to the Maven POM file from;

How to run junit tests concurrently

Add the following external jar files to your Java Build Path;
contiperf-2.2.0.jar and junit-dataprovider-1.5.0.jar

Then define a contiperf rule;

Defining an attribute of type ContiPerfRule with the annotation @Rule activates ContiPerf
@Rule
public ContiPerfRule i = new ContiPerfRule();

Define a junit DataProvider to pass multiple test data
@DataProvider
public static Object[][] data() {
return new Object[][] {
{ 1, 2,2 },
{ 3, 4,12 },
{ 5, 5,25 },
};
       }

Then in your test method use the corresponding contiperf annotations along side with necessary juni t annotations.
@Test
@UseDataProvider("data")
@PerfTest(invocations = 100,threads = 20,rampUp = 1000)
@Required(max = 1200, average = 250)
       public void SimpleTest(int valOne,int valTwo, int valAns) {
              System.out.println("Executing Test Method Using DataProvider - SimpleTest");
              int y = valOne;
              int z = valTwo;
              int a = valAns;
             
              System.out.println("Values are" +y +" and " +z);
              MyMath math = new MyMath();
              assertEquals(a, math.multiply(y,z));
       }


@PerfTest and @Required are contiperf annotations and they will define the test conditions and performance requirements/SLAs respectively. In the above example the test is configured to be executed 100 times with 20 concurrent threads, so each thread does 5 invocations. All the threads will not be spawned once, but each thread will be started with a delay of 1 second. A maximum execution time of 1.2 seconds and and an average below or equals 250 milliseconds are tolerated as performance SLAs.

When you run the above test as junit test then the contiperf report will be generated and saved in the 'target/contiperf-report/index.html'. Average time, Throughput and total number of executions can be found from this detailed report.


No comments:

Post a Comment