Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Java API Performance Testing

4.83/5 (4 votes)
14 Feb 2013GPL34 min read 26.8K  
Java API Performance Testing using LoadRunner

Introduction

I searched multiple places to get some knowledge on Java API Performance Testing, but the problem was I could not find any constructive step-by-step procedure. This made me think of jotting down the processes which would help others in future.

I have already implemented the same using Java API which was executed in LoadRunner for Scalability, Reliability and various other performance testing.

Prerequisite 

  • It is expected that you know a bit of development in Java. You may not be an awesome developer but should know a bit more than "Hello World!" program.
  • You should have knowledge in performance testing tool e.g. LoadRunner.
  • You should have any Java Development IDE e.g. NetBeans.

Requirement

  • You require Java API (usually in jar format) for which you will test the performance.
  • You usually require Java API Doc for the Java API provided which will make you understand about the API.
  • You require performance testing tool and in this example we will use LoadRunner. It is not limited to LoadRunner and could be used in performance testing tool.
  • You require Java Development IDE and we will use NetBeans in this example. This is not required but will help us to write error free code and later copy paste in performance testing tool. When you write the java code in NetBeans, you get few advantages over writing the same in LoadRunner.
  1. Add the API in NetBean project easily.
  2. Required packages are imported in NetBeans automatically.
  3. Debug the code in hassle-free method.
  4. Make the code error-free with any fuss.

Note:
Before you start writing the Java Code make sure that whatever you write in NetBeans have to be copied and it has to be understood by LoadRunner.

Performance Scenario

Suppose you have an API (e.g. SumitAPI.jar)  which was provided to you by development team of your project and you are responsible to provide performance metrics for that API when it is tested with 100 users concurrently running for 2 hour with a ThinkTime of 10 sec.

Transaction Details

  • Login to Web Server using the Java API
  • Get all Server Pool IDs
  • Get all Server Connection IDs
  • Logout from Web Server

Start writing the code in NetBeans 

In NetBeans, click File -> New Projects
Select Java -> Java Application and click Next
Enter Project Name as "Sumit_Api_Example", select Project Loaction, select Create Main Class checkbox and select Finish
New Project will be created.
Right click Libraries under Project and select Add Jar/Folder
Select the jar provided to you by the development team. In this example it is SumitAPI.jar.
Expand the tree for Libraries and you will find JDK and also the jar you just added.

Note:
The API (in this case SumitAPI.jar) or the code below will not match your performance test script. This blog will just help you to understand how to execute Performance Test for Java API. You will obviously have different API which will have different Classes, Methods and Properties with which you have to write Java Code to execute in LoadRunner.
The best way to understand your API is to go through the Java API Doc which should be provided by the development team at the time of delivering API.

Java
package sumit_api_example;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.net.ssl.*;
import sumit.api.computeservice.client.OvmWsClient;
import sumit.api.computeservice.client.OvmWsClientFactory;
 
public class Sumit_Api_Example
{
     static OvmWsClient objOvmWsClient;
 
     public static void main(String[] args)
    {
          try
         {
            //Login
            System.out.println("Login...");
            objOvmWsClient = OvmWsClientFactory.getOvmWsClient("SOAP");
            objOvmWsClient.setDebug(false);
            objOvmWsClient.setDialect("XML");
            objOvmWsClient.initialize("http://xxxxxxxx.com", "7001", true);

            objOvmWsClient.login("UserName", "Password", Locale.getDefault());
 
            //Get All Server Pool IDs
            System.out.println("Get All Server Pool IDs...");
            List<ServerPool> serpools = objOvmWsClient.serverPoolGetAll();
            for (ServerPool serpool : serpools)
            {
                System.out.println(serpool.getId().toString();
            }

            //Get All Server Connection IDs
            System.out.println("Get All Server Connection IDs...");
            List<ServerConnection> sercons = objOvmWsClient.serverConnectionGetAll();
            for (ServerConnection sercon : sercons)
            {
                System.out.println(sercon.getId().toString();
            }

            //Logout
            System.out.println("Logout...");
            objOvmWsClient.logout();
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
    }
}

Run the project and it should run successfully. It should connect to the Web Server, display all Server Pool IDs and Server Connection IDs and then logout. Once you are satisfied with the output and desired result, its time to copy paste in LoadRunner.

Note:
import sumit.api.computeservice.client.OvmWsClient; and import sumit.api.computeservice.client.OvmWsClientFactory; has been taken from SumitAPI.jar. OvmWsClient or serverPoolGetAll() are the various classes and methods inside the SumtAPI.jar.

Copy Paste above code in LoadRunner

In this section we will copy paste the working code from NetBeans in LoadRunner using Java Vuser protocol. We will add the SumitAPI.jar and then modify the code which will be recognizable by LoadRunner. We will also add parameters and other features so that we can load test the API for 100 users which will be running for 2 hours concurrently.

Click Insert -> Insert Java Function in VuGen
Click Location button
Click Browse -> File to open the jar file (in this case SumitAPI.jar)
Select the checkbox for the jar file and click OK button
Click Close button and with that SumitAPI.jar has been added in the script.

Java
import lrapi.lr;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.net.ssl.*;
import sumit.api.computeservice.client.OvmWsClient;
import sumit.api.computeservice.client.OvmWsClientFactory;

public class Actions
{
    OvmWsClient objOvmWsClient;

    public int init() throws Throwable
    {
    try
    {
        lr.start_transaction("SumitAPI_01_Login");
        //Login
        objOvmWsClient = OvmWsClientFactory.getOvmWsClient("SOAP");
        objOvmWsClient.setDebug(false);
        objOvmWsClient.setDialect("XML");
        objOvmWsClient.initialize("http://xxxxxxxx.com", "7001", true);
   
        objOvmWsClient.login(<UserNames>, "Password", Locale.getDefault());
        lr.end_transaction("SumitAPI_01_Login", lr.AUTO);

        lr.think_time(10);
    }
    catch(Exception ex)
    {
        System.out.println(ex);
    }
    return 0;
    }


    public int action() throws Throwable
    {
    try
    {
        lr.think_time(10);

            lr.start_transaction("SumitAPI_02_ShowAllServerPoolIDs");
        //Show All Server Pool IDs
        lr.message("Show All Server Pool IDs...");
        List<ServerPool> serpools = objOvmWsClient.serverPoolGetAll();
        for (ServerPool serpool : serpools)
        {
            lr.message(serpool.getId().toString());
   
        }
        lr.end_transaction("SumitAPI_02_ShowAllServerPoolIDs", lr.AUTO);

        lr.think_time(10);

        lr.start_transaction("SumitAPI_03_ShowAllServerConnectionIDs");
        //Show All Server Connection IDs
        lr.message("Show All Server Connection IDs...");
        List<ServerConnection> sercons = objOvmWsClient.serverConnectionGetAll();
        for (ServerConnection sercon : sercons)
        {
            lr.message(sercon.getId().toString();
   
        }
        lr.end_transaction("SumitAPI_03_ShowAllServerConnectionIDs", lr.AUTO);
    }
    catch(Exception ex)
    {
        System.out.println(ex);
    }
    return 0;
    }


    public int end() throws Throwable
    {
    try
    {
            lr.think_time(10);

        lr.start_transaction("SumitAPI_04_Logout");
        //Logout
        lr.message("Logout...");
        objOvmWsClient.logout();
        lr.end_transaction("SumitAPI_04_Logout", lr.AUTO);
    }
    catch(Exception ex)
    {
        System.out.println(ex);
    }
    return 0;
    }
}

Note:
Check that static keyword has been removed in LoadRunner because it is not called from static main method. Transaction Name has been defined. Parameter has been provided for UserNames. Normally the parameter is separated by {}, but here it has been set as <>. This can be configured from VuGen in Tools -> General Options -> Parameterization. Think Time has been provided and last but not the least transaction has been categorized as init(), action() and end().

Run the LoadRunner script for 2 iterations and check whether it is successfully doing the operations. With this our LoadRunner script is ready and to be executed in LoadRunner controller as per the scenario defined.

Points of Interest 

Always make sure that whatever code you write in NetBeans should be convertable in LoadRunner.

History

This is the first version.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)