Monday, December 14, 2015

[JMeter] Invoke API chain using JMeter

I have created two ESB APIs. You can download these two files and copy to wso2esb-4.9.0/repository/deployment/server/synapse-configs/default/api directory.

1. First API (API-1) will  return the TV channels (tvstations.xml).

GET http://localhost:8280/channels

{
  "details":{
    "totalChannelCount":4,
    "channelDetails":{
      "channelDetail":[
        {
          "name":"TV-01",
          "owner":"ajith",
          "location":"Colombo 05"
        },
        {
          "name":"TV-02",
          "owner":"vajira",
          "location":"Colombo 05"
        },
        {
          "name":"TV-03",
          "owner":"ranjith",
          "location":"Colombo 05"
        },
        {
          "name":"TV-04",
          "owner":"lakmali",
          "location":"Colombo 05"
        }
      ]
    }
  }
}


2. The second API (API-2) will return the programs available in each channel (tvprograms.xml).

GET http://localhost:8280/programs?channel=TV-01

{
  "details": {
    "totalPrograms": 4,
    "channelName": "TV-01",
    "programsDetails": {
      "program": [
        {
          "name": "Morning Show",
          "presenter": "ajith",
          "duration": "1h"        },
        {
          "name": "News",
          "presenter": "vajira",
          "duration": "2h"        },
        {
          "name": "Lunch Time Music",
          "presenter": "nishatha",
          "duration": "3h"        },
        {
          "name": "movie",
          "presenter": "lakmali",
          "duration": "4h"        }
      ]
    }
  }
}
Now I'm going to create a JMeter test plan. First invoke  API-1 to get  TV channels, then iterate through each channel to get TV programs invoking  API-2.

Steps:

1. Download and install the JMeter plugins from http://jmeter-plugins.org/downloads/all/

(Download the "Extras with Libs Set")

2. This plugin provides "Json Xpath extractor" feature, which can be used to filter the TV channel names  from first API response.

3. Create a new "Thread Group".

"Test Plan" ----> "Add" ---> "Threads (users)" -----> "Thread Group"

4. Add "HTTP Header Manager", this is  useful to set the HTTP headers which is required to invoke the API.

 "Thread Group" ---> "Add" ---->  "Config Element" --->  "HTTP Header Manager"

5.  Add HTTP Request, then define a host , protocol, path ..etc.

"Thread Group ----> "Add"  ---> "Sampler" -----> HTTP Request"


5. Add JSON path extractor to filter the channel names from the first response.

"HTTP Request" ---> "Add" ---> "Post Processors" ---> "Json path extractor"

variable name : channelNames
Json path expression: $.details.channelDetails.channelDetail[*].name


6.  Add another JSON path extractor to assign channel count to a variable.

variable name : channelCount
Json path expression:$.details.totalChannelCount


7.  Add "Loop Controller" -  set the Loop Count as ${channelCount}

"Thread Group" ---> "Add" ---> "Logic Controller" ---> "Loop Controller"


8. Add counter to construct the for loop.

"Loop Controller" ---> "Add" ----> "Config Element" ----> "Counter"

Start : 0
Increment : 1
Maximum : ${channelCount}
Reference Name : loopCounter


9.  Add "BeanShell Processor" to set channel name as a variable.

"Loop Controller" --> "Add" ---> "Post Processors" ---> "BeanShell Processor"



// channelNames is the Json array as string.

String tvChannels = vars.get("channelNames");

// Create channle list as array

tvChannels = tvChannels.replace("[","").replace("]","").replace("\"","");

String[] channelArray = tvChannels.split(",");

// "loopCounter" is reference variable used in Counter

idx = Integer.parseInt(vars.get("loopCounter"));

// Set the chanle name to a variable

vars.put("channelName",channelArray[idx]);

10. Add the second API as HTTP Request.

The channelName  is variable defined in the BeanShell processor.
Path : /programs?channel=${channelName}

11. Add a listener to monitor the results.

Eg:

"HTTP Request"  ---> "Add" ---> "Listeners" ---> "Summary Report"


11. You can download the .jmx file  here and import to JMeter.