Thursday, 20 August 2015

Real Example for Correlation In LoadRunner


I recorded LoadRunner script for a web server, which contained two special fields - timestamp and checksum:

web_submit_data("rms.jsp", 
    "Action=http://eprumossd0010:8400/RMS/jsp/rms.jsp", 
    "Method=POST", 
    "RecContentType=text/html", 
    "Referer=http://eprumossd0010:8400/RMS/html/testFramework.html", 
    "Snapshot=t4.inf", 
    "Mode=HTML", 
    ITEMDATA
    "Name=TIMESTAMP", "Value=1192177661211", ENDITEM
    "Name=CHECKSUM", "Value=715E19300D670ED77773BBF066DAAAE2866484B8", ENDITEM
    // others parameters ...
    LAST);

Every time a client web browser connects to web server, server gets current time stamp, calculates checksum and sends them to client. These two fields are used to identify a current session. In other words, the pair of timestamp+checksum is analog of session ID.

The scheme of this interaction is the following:


Where is the problem? Let's replay the recorded LR script.

The problem occurs when I try to execute my recorded script.
Web server checks its current time with a time stamp, sent by client. If client's data is out-of-date or incorrect, then server returns an error:

The parameter "CHECKSUM" is not found or has invalid value.

There is the scheme for this interaction:

Client cannot re-use old (i.e. hard-coded) values for times tamp and checksum. It must request new data.So, instead of hard-coded values, LR script should process dynamic data, returned from server. This can be done using a correlation:


The definition of correlation is:
Correlation is the capturing of dynamic values passed from the server to the client.


Correlation can be done with 2 ways:
  1. Automatically
  2. Manually
I will describe auto-correlation in the future posts. For now, I can say that this is not ideal solution. Sometimes, it does not work, or works incorrectly.

Manual correlation is a choice of real LoadRunner engineer. It's a kind of "must have" knowledge!
Well, let's start investigating a manual correlation.

The algorithm of manual correlation is the following:
  1. Find a dynamic value to capture.
  2. Find server's response, containing the dynamic value.
  3. Capture the dynamic value.
    Special parameter will be used instead of dynamic value.
  4. Replace every occurrence of dynamic value in script with the parameter.
  5. Check changes.
Now, I will describe each step in details:
  1. Find a dynamic value to capture
    I recommend to record and save two equal VuGen scripts. After that, open menu item "Tools / Compare with Scripts..." and you can compare both recorded scripts in WDiff:
    The differences are highlighted by yellow. This highlighting means that lines (parameters values) change from run to run. So, most probably, these values should be correlated.

    Tips: Sometimes, comparing of two scripts cannot detect dynamic values. Imagine, that you recorded this script:
        "Name=SessionID", "Value=A38E9002A41", ENDITEM
        "Name=CurrentMonthID", "Value=4", ENDITEM,    ...
    It's obvious, that SessionID should be correlated. What about CurrentMonthID parameter? Second recorded script can contain "Value=4" too. And it's possible, that your script will work correctly during the April (4th month is April), and will not work from 1st May! So, be careful, my dear reader :)

    Tips: 
    Look through the source code of recorded script. TimestampCheckSumSessionID, and different IDs - all of they are potential candidates to be correlated.

    Tips: Check Replay (Execution) log carefully. Errors can be there. The widespread reason of script's errors is an absence of correlations.

    Tips: Execute your script with enabled run-time viewer (menu "Tools / General Options.. / Display"). Any shown errors ("Page not found", "Session timeout", etc) indicate about potential correlations.
  2. Find server's response, containing the dynamic value
    Before script executing, please enable extended logging from "Vuser / Run-Time Settings...":
    Then execute script.
    Open Replay (Execution) log and find server's response, which contains dynamic values of TIMESTAMP and CHECKSUM:
    Great! Now we know, where server sends both dynamic values.
    And we found the step, that returns these values. This is 13th line - Action.c (13). Double click the line, containing timstamp's and checksum's values, 13th line of script will be opened:
    web_submit_data("generateChecksum.jsp",     "Action=http://eprumossd0010:8400/RMS/jsp/generateChecksum.jsp", 
        "Method=POST",
        "RecContentType=text/html",
        ...
    This means that server's response for generateChecksum.jsp page contains dynamic values which should be correlated.
  3. Capture the dynamic value
    I will show two ways how to capture a dynamic value:
    • Automatic capturing from Tree-view
    • Manual from Script-view
    These ways are similar enough. Also, they use the same function - web_reg_save_param.

    I start from:
    • Automatic capturing from Tree-view.
      Open Tree view (menu "View / Tree view"):
      Then:
      • click "View recording snapshot only" btn (2);
      • select generateChecksum.jsp page from tree view (3);
      • select "Body" to view body of server's response (4).

      As result, you will see recorded values of CHECKSUM and TIMESTAMP (5).

      Now, select value of first dynamic value (checksum), right-click, and select "Create parameter":
      After that you will see the message box:
      You can create parameter for dynamic value.
      If you want to replace all occurrences of dynamic value ("715E19...") in script, press "Yes" btn.
      To not replace all occurrences, press "No" btn.

      Tips: I recommend to not replace all occurrences of dynamic value. It can lead to incorrect results. It's more preferable to replace occurrences one by one with "Search and Replace" dlg.

      OK, I click "No" btn.

      Return to Script-view ("View / Script View") and see changes. There are new lines, inserted before generateChecksum.jsp page:

      // [WCSPARAM WCSParam_Text1 40 715E19300D670ED77773BBF066DAAAE2866484B8] Parameter {WCSParam_Text1} created by Correlation Studio 
      web_reg_save_param
      ("WCSParam_Text1",
          "LB=window.parent.setChecksum(\"",
          "RB=\"",
          "Ord=1",
          "RelFrameId=1",
          "Search=Body",
          "IgnoreRedirections=Yes",
          LAST);

      web_reg_save_param function finds and saves a text string from the next server's response. In other words, it captures a dynamic value.

      In this example, web_reg_save_param function will save the captured value into WCSParam_Text1 parameter. The function finds the left boundary (window.parent.setChecksum(") and after that it finds the right boundary ("). The string, found between left and right boundaries, will be saved to WCSParam_Text1 parameter.
      Ord attribute indicates the ordinal position of captured value. In the example (Ord=1), we capture the value between first left boundary and left one.
      Easy to guess, that "Search=Body" means search in a body of server's response.
      I recommend to study Help on web_reg_save_param function. It's very interesting :)

      Note: the capturing of TIMESTAMP parameter is similar. It generates the following code:

      // [WCSPARAM WCSParam_Text2 13 1192177661211] Parameter {WCSParam_Text2} created by Correlation Studio
      web_reg_save_param("WCSParam_Text2",
          "LB=, ",
          "RB=)",
          "Ord=1",
          "RelFrameId=1",
          "Search=Body",
          "IgnoreRedirections=Yes",
          LAST);
    • Manual capturing from Script-view.Actually, this method consists in a manual writing of web_reg_save_param function. It requires strong knowledge on this function and its parameters. There are many attributes of web_reg_save_param function, and I do not want to duplicate HP's Help :)

      Tips: I recommend to rename default parameter (WCSParam_Text1, 2, 3, etc) to something sensible. For example, in my example - I would prefer prmCheckSum and prmTimeStamp to WCSParam_Text1 and WCSParam_Text2.
  4. Replace every occurrence of dynamic value in script with the parameter
    This is not difficult step.
    Open "Search and Replace" dlg ("Edit / Replace..."). And replace one-by-one hard-coded values with a parameter.

    Why it is important?
    Imagine, that you have the following code:

    web_submit_data("somepage", 
        ...
        "Name=OrderNumber", "Value=125", ENDITEM
        "Name=UserID", "Value=125", 

    If you create parameter for UserID, and perform replacing of all occurrences of its value ("125"), then it will produce the code:

    web_submit_data("somepage", 
        ...
        "Name=OrderNumber", "Value={WCSParam_Text1}", ENDITEM
        "Name=UserID", "Value={WCSParam_Text1}", 

    It may be wrong! OrderNumber can be static value and be equal to 125, while UserID may change.

    Now, I assume that you replace all needed occurrences of hard-coded values. We have to perform last step:
  5. Check changes
    After above manipulations, our script will look like:

    web_submit_data("rms.jsp", 
        "Action=http://eprumossd0010:8400/RMS/jsp/rms.jsp", 
        "Method=POST", 
        "RecContentType=text/html", 
        "Referer=http://eprumossd0010:8400/RMS/html/testFramework.html", 
        "Snapshot=t4.inf", 
        "Mode=HTML", 
        ITEMDATA
        "Name=TIMESTAMP", "Value={WCSParam_Text2}", ENDITEM
        "Name=CHECKSUM", "Value={WCSParam_Text1}", ENDITEM
        // others parameters ...
        LAST);

    The statement "{WCSParam_Text1}means "get value of WCSParam_Text1 parameter".
    So, current algorithm is:
    • when server returns different values of CheckSum and TimeStamp
    • then web_submit_data captures and places them into WCSParam_Text1 and WCSParam_Text2 parameters
    • after that we use {WCSParam_Text1} and {WCSParam_Text2} get current values of parameters and use them in scripts

    Let's run our modified script and see results of capturing from server's response:
    These are values, which are sent to a server by a client:

    You can see that dynamic values are saved to parameters and their values are substituted instead of parameters in scripts. Great! We have just correlated our script and it is working now!

    Tips: To get/debug a captured value of parameter, use the following statements:
    lr_output_message("Value of WCSParam_Text1: %s"lr_eval_string("{WCSParam_Text1}"));
    lr_output_message("Value of WCSParam_Text2: %s"lr_eval_string("{WCSParam_Text2}"));

    Execute script again, he result is:


Epilogue:
I hope, I provided simple and understandable explanation of LoadRunner correlation.
Please, share your comments and thoughts, dear readers.

Do you have ideas for further topics - welcome :)




Related articles:

No comments:

Post a Comment