Friday 16 September 2016

LoadRunner - Selecting random value using lr_paramarr_random function


In performance testing, it is really important to simulate a realistic user path through an application. For example, randomly select an image link from a gallery or select a share from a share list. In such situations, you can use the LoadRunnerlr_paramarr_random function to select a random value from a captured parameter array. Similarly, you can also write a code to do the same.

Before you use the above function, you will need to use web_reg_save_paramfunction to capture all the ordinal values. This can be achieved by passing "ORD=ALL" into the function.

The following code demonstrates the use of lr_paramarr_random function. The code saves link Ids using  web_reg_save_param function and then useslr_paramarr_random function to randomly select an Id value. The value is passed as a parameter in web_text_link function to navigate to the appropriate webpage.

Action()
{

 //Capture all the Link IDs
 web_reg_save_param("LinkID","LB=id=\"","RB=\" href=\"default.aspx?content=business_","ORD=ALL",LAST);

 lr_start_transaction("VU01_01_Homepage");
 web_browser("website", 
  DESCRIPTION, 
  ACTION, 
  "Navigate={WebsiteUrl}", 
  LAST);

 lr_end_transaction("VU01_01_Homepage",LR_AUTO);

 lr_think_time(10);


 //Save a randomly selected ID to a Parameter
 lr_save_string(lr_paramarr_random("LinkID"),"RandomLink"); 

 //Printout the randomly selected parameter
 lr_output_message("%s",lr_eval_string("{RandomLink}"));

 //Navigate to the appropriate webpage associated to the ID
 lr_start_transaction("VU01_02_RandomLink");

 web_text_link("Links", 
  "Snapshot=t2.inf", 
  DESCRIPTION, 
  "Id={RandomLink}",
  ACTION, 
  "UserAction=Click", 
  LAST);

 lr_end_transaction("VU01_02_RandomLink",LR_AUTO);
 
 web_browser("Sync", 
  "Snapshot=t3.inf", 
  DESCRIPTION, 
  ACTION, 
  "Sync", 
  LAST);

 return 0;
}
Following is a screenshot of replay log, displaying the random id's selected for each iteration. It also displays the values captured using web_reg_save_param function. 

Wednesday 14 September 2016

MQ scripting vugen code for pushing message in Queue


            //Message Injection loop
            for(i=0;i<injectionRate;i++)
            {

                if (numberOfMsgsInjected==totalMessages)
                                break;
                try
                {
                text.delete(0,text.length());
                //appending the xml message.
                text.append(lr.eval_string("<Items MessageType=\"ADJUSTMENT\"><Item AdjustmentType=\"ADJUSTMENT\" Availability=\"TRACK\" ItemID=\"[ItemID]\" OrganizationCode=\"DEFAULT\" ProductClass=\"GOOD\" Quantity=\"10\" RemoveInventoryNodeControl=\"Y\" ShipNode=\"[BrandID]\" SupplyType=\"ONHAND\" UnitOfMeasure=\"EACH\"></Item></Items>"));

                //Convert to String 
                String str = text.toString();
                System.out.println(str);
       
                //Creating Message Queue
                MQMessage XMLMessage = new MQMessage();

                //Setting Message format
                XMLMessage.format = "MQSTR";
                //UTF encoding Supports
                XMLMessage.writeString(str)
               
                System.out.println("string" + XMLMessage);                  
                  // specify the message options...
            
                  MQPutMessageOptions pmo = new MQPutMessageOptions();    // same as MQPMO_DEFAULT
            
                 // put the message on the queue
            
                  qName.put(XMLMessage,pmo);
                  numberOfMsgsInjected = numberOfMsgsInjected + 1;
                   lr.start_transaction("MessageSend");
                  System.out.println("msg sent ...");
                  lr.end_transaction("MessageSend",lr.PASS);
                               
                }

                catch(Exception e)
                {
                System.out.println("error here");
                e.printStackTrace();
                System.exit(0);
                }

            }

            //Getting the time stamp in millisecond after message injection.
            timeInMillis2 = System.currentTimeMillis();
           
        //Calculating the message injection rate
        double timespend = (double)(timeInMillis2 - timeInMillis1);
        double injectionIntervalMillisec = multiplyingFactor * injectionInterval * 1000;
        try
        {

            if(injectionIntervalMillisec > timespend)
            {
           
            Thread.sleep((long)(injectionIntervalMillisec-timespend));
            
            }
        }
            catch (Exception e)
            {}

        if (numberOfMsgsInjected==totalMessages)
         {

            lr.exit(lr.EXIT_VUSER,lr.PASS);
         }

    return 0;
    }//end of action

 /**
 * LoadRunner method to terminate the established  connection
 * with middleware.
 */

    public int end() throws Exception
    {
        // Close the queue...
        qName.close();

        // Disconnect from the queue manager 
        qMgr.disconnect();
        return 0;
    }//end of end

}

Friday 9 September 2016

Validation check in web services protocol using lr_XML_get_values()

The function lr_xml_get_values executes queries on the string xml_input. The results of each query are stored in the parameter Result.

#include "as_web.h"

/* The XML Data */
char *xml_input=
"<acme_org>"
     " <accounts_dept>"
          "<employee>"
               " <name>Kevin Sharp</name>"
               "<cubicle>227</cubicle>"
               "<extension>2145</extension>"
          "</employee>"
     "</accounts_dept>"
     "<engineering_dept>"
          "<employee>"
               "<name>John Smith</name>"
               "<cubicle>372</cubicle>"
               "<extension>2970</extension>"
          "</employee>"
          "<employee level=\"manager\">"
               "<name>Sue Jones</name>"
               "<extension>2375</extension>"
          "</employee>"
     "</engineering_dept>"
"</acme_org>";

Action() {
    int query_number = 1;
// Save data as parameter
    lr_save_string(xml_input, "XML_Input_Param");
    /* Query 1: Find the first employee's name
        (To find the names of all employees
        see Multiple Query Matching)    */
    lr_xml_get_values("XML={XML_Input_Param}",
        "ValueParam=Result",
        "Query=/acme_org/*/employee/name",
        LAST );
    /* Query 2: Find the first extension number one
        or more levels deep */
    lr_xml_get_values("XML={XML_Input_Param}",
        "ValueParam=Result",
        "Query=//extension",
        LAST );
    /* Query 3: Find the name of the manager
        of the engineering department. */
    lr_xml_get_values("XML={XML_Input_Param}",
        "ValueParam=Result",
"Query=/acme_org/engineering_dept/employee[@level=\"manager\"]/name",
        LAST );
    /* Query 4: Find the name of an employee
    whose extension number is 2970 */
    lr_xml_get_values("XML={XML_Input_Param}",
        "ValueParam=Result",
        "Query=/acme_org/*/employee[extension=\"2970\"]/name",
        LAST );
    return 0;
}
Output from Execution Log Window:
Action.c(33): Saving Parameter "Result = Kevin Sharp"
Action.c(33): "lr_xml_get_values" was successful, 1 match processed
Action.c(37): Saving Parameter "Result = 2145"
Action.c(37): "lr_xml_get_values" was successful, 1 match processed
Action.c(41): Saving Parameter "Result = Sue Jones"
Action.c(41): "lr_xml_get_values" was successful, 1 match processed
Action.c(46): Saving Parameter "Result = John Smith"
Action.c(46): "lr_xml_get_values" was successful, 1 match processed