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).


1 comment: