Showing posts with label REST. Show all posts
Showing posts with label REST. Show all posts

Thursday, March 22, 2012

How to Test REST Web Service Using The Grinder

A simple and easy method of load testing a REST service using The Grinder open source load testing tool is as follows:
  • use the HttpRequest object
  • use the appropriate REST method on the HttpRequest object: 
    • Method=PUT
    • Method=GET
    • Method=DELETE


















The following script gives an example of this method in the case of REST PUT request using a JSON request message.  (XML request message would be similar).

Script

# The Grinder 3.7.1

from net.grinder.script import Test
from net.grinder.script.Grinder import grinder
from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest
from HTTPClient import NVPair
connectionDefaults = HTTPPluginControl.getConnectionDefaults()
httpUtilities = HTTPPluginControl.getHTTPUtilities()

# headers
headers0= \
  [ ]

# Http URL
url0 = 'http://servername:8080'

# Create an HTTPRequest for each request, then replace the
request101 = HTTPRequest(url=url0, headers=headers0)
request101 = Test(101, 'PUT Test').wrap(request101)

request102 = Test(102, 'PUT Test 2').wrap(request101)

json1 = '''{
              \"key1\"        :  \"BD6632F8569D4D609BB8ED902E9F6585\",    
              \"key2\"        :  \"65EE35B00655100625F85498A6092F80\",    
              \"key3\"        :  \"984921672\",\r\n    
              \"id1\"         :  \"6\",\r\n    
              \"id2\"         :  \"1\",\r\n    
              \"descr\"       :  \"4724711\",\r\n    
        }'''
json2 = '''{   
              \"key4\"        :  \"6\",\r\n    
              \"key5\"        :  \"1\",\r\n    
              \"key6\"        :  \"0\",\r\n    
              \"key7\"        :  \"2\",\r\n    
              \"name1\"       :  \"Green Bay\",\r\n    
              \"name2\"    :  \"GRBay\",\r\n    
        }'''


class TestRunner:
  """A TestRunner instance is created for each worker thread."""

  # A method for each recorded page.
  def page1(self):
    """PUT Test (request 101)."""
    result = request101.PUT('/Capture/Test', json1,   ( NVPair('Content-Type', 'application/json'), ))

    return result
# A method for each recorded page.
  def page2(self):
    """PUT Test (request 101)."""
    result = request102.PUT('/Capture/Test', json2,   ( NVPair('Content-Type', 'application/json'), ))

    return result

  def __call__(self):
    """Called for every run performed by the worker thread."""
    self.page1()      # PUT Test (request 101)
    self.page2()      # PUT Test (request 102)

Output

Monday, January 9, 2012

How to Test REST Web Service Using LoadRunner

A simple and easy method of load testing a REST service using LoadRunner is as follows:

  • use the http protocol
  • use web_custom_request
  • specify in web_custom_request the appropriate REST method: 
    • Method=PUT
    • Method=GET
    • Method=DELETE
The following script gives an example of this method in the case of REST PUT request using a JSON request message.  (XML request message would be similar).  One of the fields in the message is parameterized using a random number parameter.  The parameter is given a format of "0000000000000000000000%09lu" to achieve a 32 character-long number string.

Script

Action()
{
    char *request_json_base;
    char *request_json;


    // save web service url to param {URL}
    char *URL = "http://SERVER:8080/Path";
    lr_save_string(URL, "URL_Param");


    // save json request to param {REQUEST_JSON_PARAM}, parameterize "SomeID" as random number
    request_json_base=
     "{" 
     "    \"Field1\"       : \"ValueOfField1\"," 
     "    \"SomeID\"           : \"{SomeID}\","   
     "    \"Field2\"       : \"ValueOfField2\"," 
     "    \"Field3\"       : \"ValueOfField3\"," 
     "}";

    request_json = lr_eval_string(request_json_base);
    lr_save_string(request_json, "REQUEST_JSON_PARAM");
  
    // set http headers
    web_add_header("Content-Type", "application/json; charset=utf-8");


    // validate response
    web_reg_find("Text=success", LAST);


    // send JSON request
    lr_start_transaction("rest_put");


    web_custom_request("post_to_http_jms_provider",
    "URL={URL_Param}",
    "Method=PUT",
    "TargetFrame=",
    "Resource=0",
    "Referer=",
    "Mode=HTTP",
    "Body={REQUEST_JSON_PARAM}",
    LAST); 


    lr_end_transaction("rest_put", LR_AUTO);
}


Console Output

Action.c(49): Notify: Transaction "rest_put" started.
Action.c(51): Notify: Parameter Substitution: parameter "URL_Param" =  "http://SERVER:8080/Path"
Action.c(51): Notify: Parameter Substitution: parameter "REQUEST_JSON_PARAM" =  "{    "Field1"       : "ValueOfField1",    "SomeID"          : "00000000000000000000001244464508",    "Field2"       : "ValueOfField2",    "Field3"       : "ValueOfField3"}"
Action.c(51): t=258ms: 147-byte response headers for "http://SERVER:8080/Path" (RelFrameId=1, Internal ID=1)
Action.c(51):     HTTP/1.1 200 OK\r\n
Action.c(51):     Content-Type: application/octet-stream\r\n
Action.c(51):     Date: Mon, 09 Jan 2012 18:43:07 GMT\r\n
Action.c(51):     Content-Length: 7\r\n
Action.c(51):     \r\n
Action.c(51): t=271ms: 7-byte response body for "http://SERVER:8080/Path" (RelFrameId=1, Internal ID=1)
Action.c(51):     success
Action.c(51): Registered web_reg_find successful for "Text=success" (count=1)   [MsgId: MMSG-26364]
Action.c(51): web_custom_request("post_to_http_jms_provider") was successful, 7 body bytes, 147 header bytes   [MsgId: MMSG-26386]
Action.c(61): Notify: Transaction "rest_put" ended with "Pass" status (Duration: 0.1255 Wasted Time: 0.0000).