Creating Regular Expressions in JMeter

Creating Regular Expressions

Regular expressions can be a bit daunting at first, so here is a suggested procedure to help create them.

Getting Started
Ensure you have an exact copy of the source document that you want to operate on.

One way to do this is to attach a Save Responses to a file Listener to the Sampler, and run the test to create a copy of the resource.

You can then use the HTTP Sampler with the “file:” protocol to retrieve the page at any time.

Extract the section you are interested in
Find the variable part that you want to extract (in the file, or in the Tree View Listener), and start by using that as the regular expression.

For example, suppose you want to find the value from the following snippet:

input type=”hidden” name=”secret” value=”CAFEBABE.12345(3)”

Start with exactly that as the regular expression, and check that it works, for example in the Tree View Listener Regex tester panel.

If not, examine the expression for any meta-characters (characters that have a special meaning in regexes).In this case, the only special characters are the “.” and the parentheses “(” and “)”.These need to be escaped, by being prefixed with “\”. We now have:

input type=”hidden” name=”secret” value=”CAFEBABE\.12345\(3\)”

This should now match the whole phrase.

The next stage is to tell the regex processor which part of the section you want to use. This is easy, just enclose the characters in parentheses.

So assume you want to match just

CAFEBABE.12345

Your regular expression then becomes:

input type=”hidden” name=”secret” value=”(CAFEBABE\.12345)\(3\)”

Fix the expression so it matches variable text

Of course, the previous expression is not much use, as the text it matches is already known. We want to match variable text, so we have to replace the fixed characters of the target text with meta-character expressions that will match all possible variations of the target.

This requires knowledge (or a good guess) as to what possible characters can be used in the target.

In this case, it looks as if there is a string of hex characters followed by a number, followed by a digit in parentheses.

A digit is easy, that’s “\d”, so a number is “\d+”, where the “+” means one or more of the previous item.

A hex character can be represented by “[0-9A-Za-z]“. The enclosing “[ ]” create a ”character class” which matches one of the specified characters.
The “-” here means a range.

So putting that together, we get:

input type=”hidden” name=”secret” value=”([0-9A-Za-z]+\.\d+)\(d\)”

Now suppose we wanted to match the whole of the value. We could move the closing capture parenthesis to the end of the value.

This would be suitable if there were other values with different patterns that we did not want to match.

However, if we just wanted to capture the quoted value, then we could use:

input type=”hidden” name=”secret” value=”([^"]+)”

The character class in this case is ”[^"]” which means any character except double-quote. The “+” suffix means we want as many as there are in succession.

This will take us up to the end of the value.

If the expression matches more than once in the source, you have a choice:

* specify which match to use
* extend the expression to include more context at the beginning or end so the match is unique

JMETER TIPS AND TRICKS

1. How to add JMeter requests manually and 
2.Need to capture the traffic for a web front end that’s heavy in AJAX. So there’s a lot going on in the background that I don’t get to see by just making a request to the page. I could use a third party app or an addon like Firebug to grab all the traffic but when that functionality is built into JMeter I’d just be making work for myself.

I have the bare bones of my test plan in place. In the test plan node I create a User Defined Variable called serverName with the relevant target server name as the value. I’m ready to record the transactions for a short user story. I create a blank transaction controller to group the page requests (this relates to another tip I’ll post later) then in the Proxy Server config I set the Target Controllerto this empty one.

Changing the proxy server and port in the browser, I point the browser at the required location and record the transaction. For the next step I create another empty Transaction Controller, update theTarget Controller of the Proxy Server, and continue with the user story, repeating this process for each step.

This can seem cumbersome but it lightens the work load. It also allows you to go back and edit the request, adding whatever assertions you feel may be warranted while the step is clerar in your mind. An alternative would be to set the Grouping on the Proxy Server to Put each group in a new controller and later copy and paste these grouped requests into transaction controllers seperately. Either way would work. Just don’t get too far ahead of yourself and record a large slice of your user story without analysing it.

Now if I go back and look at an HTTP request created during the user story, the Server Name or IPfield has been automatically filled with the appropriate variable i.e. ${serverName}. Any other variables I set are also automagically entered into their appropriate fields.

Troubleshooting the Astra Load Test Monitor

How the Monitor Works

The monitor will run the script in the Astra LoadTest (ALT) application. It is running through perfex, and will spawn an mdrv process to execute the script. Perfex is required so that the mdrv process will be created with the credentials supplied in the ALT Recorder page.

The script is run in exactly the same way as it is run under Astra LoadTest in load mode. Astra Load Test will use the credentials defined in the Recorder page.

The Astra LoadTest log files are kept in the SiteScope\cache\tempbysize directory. The monitor parses this log file to get the transaction times and errors/warnings. By default, SiteScope saves only the log files of failed runs. If the run finished successfully, the log is not saved. However If you want to keep all the log files, you have can open the SiteScope/groups/master.config file and set to "true" the following entry:

_astraKeepLogFiles=

How to Troubleshoot

1. Make sure you have Astra LoadTest version 5.4.3 or later. It must be installed on the same machine where SiteScope is running.

2. When setting up the script initially in ALT, you must define at least one Transaction while recording (with Start and Stop points), or the monitor will fail in SiteScope.

3. The credentials (in Recorder page) have to be the same as the user that was logged in when Astra LoadTest was installed.

4. Check that you can run the script in Astra LoadTest directly. If the script runs in Astra Load Test but times out in SiteScope, then use WinInet mode to execute the script instead. In order to switch between WinInet and Turbo replay you have to go to the RTS – Browser Configuration tab and check/uncheck the Use Turboload Technology option.

5. If you are running SiteScope service as a LocalSystem Account, add the user credentials located in the Astra LoadTest Runners Preference page. This is in the help manual at:

http://your SiteScope server name and port/SiteScope/docs/recorderPrefs.htm

SiteScope should run as an interactive service in the context of the LocalSystem account. It can be changed in Control Panel -> Services -> Sitescope -> Properties -> Log on. Select the Allow interaction with desktop checkbox.

6. Every time SiteScope restarts, it cleans up the oldest Astra LoadTest log files in the SiteScope/cache/tempbysize directory according to the max size limit defined for this directory in the SiteScope/groups/master.config file. The entry is:

_tempDirMaxSize=10000 (the size is in KB)

In SiteScope 7.8.1.0, there is no default value for that entry, and that will cause all the files in the directory to be deleted. There is a default of 10000 in SiteScope 7.8.1.2 and later. If the entry is not there, then add the above entry to the master.config file, save it, and restart SiteScope. After that, the log files should be kept until the directory gets to the limit defined.

Note – Astra Load Test has been deprecated as of SiteScope 7.9.0.0 or later. It is only available in these versions if SiteScope has been upgraded from a pre-7.9.0.0 version.

How to save test results in jmeter

There is one more way about how to do it - using the command line options.

So, if you want to run the test plan from file test.jmx and save the testing results to the file log.jtl, use the following command:

jmeter -n -t test.jmx -l log.jtlYou can configure how the result data will be saved by editing the file jmeter.properties (section "Results file configuration"). For example, if you want to save the results in CSV format you should edit the following property:

jmeter.save.saveservice.output_format=csvThis way is more powerful than the one described in the previous part (Simple Data Writer) because it allows you to set some additional options like CSV delimiter, timestamp format, etc. Sometimes it could be very helpful.

Capturing the response data size limit in JMeter

View Results Tree is very useful listener especially when you are debugging your test plan. You can review the response data and ensure that your test plan works good. But sometimes you you can see a message in the beginning of the response data like "Response too large to be displayed. Size: 313674 > Max: 204800, Start of message:".



And unfortunately you are not able to see the whole response data. By default JMeter shows only first 200 Kb of response data. To resolve this problem you should edit the file jmeter.properties and uncomment the line:

view.results.tree.max_size=0 This will disable response data size check at all.


After you have saved jmeter.properties you will never see this message again.


Creating Custom Request in VuGen LoadRunner

There are some situations where you need to create custom request so that you can manipulate the request according to your need. Here we will see how to create custom request and tamper with it to get our job done.

Basic:When we record the script for any web application, we generally choose HTML-based recording because it is easy and minimizes the correlations you have to handle in the script.LoadRunner do generate custom request when some request does not fall under category of lr_web_submit_data() or lr_web_submit_form() requests category.Challenge:Suppose there is one request where you are sending some data items with it and every time there will be random number of data items like, first time there will be four data items second time there will be five data items in the same request. Here you cannot use same recorded request as number of data items are going to be different every time.In below example you can see that there are only four data items in the request next time there may be five data item.
"Name=SearchResultsGrid_ctl04_ViewPackageDialogResult", "Value=291570", ENDITEM,"Name=SearchResultsGrid_ctl08_ViewPackageDialogResult", "Value=291566", ENDITEM,"Name=SearchResultsGrid_ctl11_ViewPackageDialogResult", "Value=291563", ENDITEM,"Name=SearchResultsGrid_ctl12_ViewPackageDialogResult", "Value=291562", ENDITEM,

Solution:Here you can create a custom request and use a parameter which will hold the manipulated string of data items. In that string you can save different number of data items and you can change it as required.Procedure:First record the script with HTML-based recording and with proper transaction names for every action.Save that script and then click on Tools> Regenerate Script…





Below popup will come, click on Options…



It will show you the Regenerate options, Click on General> Recoding



    There are two options available, HTML-based script and URL-based script. Choose URL-based script.
    For URL-based script there is a button for advanced settings named URL Advanced, click on it.
    From the new popup select Use web_custom_request only.
    Save the settings and regenerate the script.
    In the regenerated script you can see that new script is generated which contain only web_custom_request.
    Now find the particular request which you want to manipulate with the help of transaction names which you have given while recording the script.

    Below is the HTML-based recorded request, You can see that there are four data items of grid results,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
web_submit_data("managepackages.aspx_4",
"Action=http://{p_Url}/site/secure/reportcenter/managepackages.aspx",
"Method=POST",
"TargetFrame=",
"RecContentType=text/html",
"Referer=http://{p_Url}/site/secure/reportcenter/managepackages.aspx",
"Snapshot=t50.inf",
"Mode=HTML",
ITEMDATA,
"Name=SearchResultsGrid_ctl03_ViewPackageDialogResult", "Value={c_PackageID}", ENDITEM,
"Name=SearchResultsGrid_ctl04_ViewPackageDialogResult", "Value=291570", ENDITEM,
"Name=SearchResultsGrid_ctl08_ViewPackageDialogResult", "Value=291566", ENDITEM,
"Name=SearchResultsGrid_ctl11_ViewPackageDialogResult", "Value=291563", ENDITEM,
"Name=SearchResultsGrid_ctl12_ViewPackageDialogResult", "Value=291562", ENDITEM,
LAST);
Below is the custom request which is URL-based,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
web_custom_request("managepackages.aspx_4",
"URL=http:// {p_Url}/site/secure/reportcenter/managepackages.aspx",
"Method=POST",
"Resource=0",
"RecContentType=text/html",
"Referer= http://{p_Url}/site/secure/reportcenter/managepackages.aspx",
"Snapshot=t135.inf",
"Mode=HTTP",
"Body=SearchResultsGrid_ctl03_ViewPackageDialogResult={c_PackageID}"
"{final_string}",
/*//below string is going to vary every time so we are going to manipulate it every time using string manipulation functions//

"&SearchResultsGrid_ctl04_ViewPackageDialogResult=291570"
"&SearchResultsGrid_ctl08_ViewPackageDialogResult=291566"
"&SearchResultsGrid_ctl11_ViewPackageDialogResult=291563"
"&SearchResultsGrid_ctl12_ViewPackageDialogResult=291562"
*/

LAST);


In custom request you can manipulate final string every time using string manipulation functions available in VuGen.

final_sting= “&SearchResultsGrid_ctl04_ViewPackageDialogResult=291570&SearchResultsGrid_ctl08_ViewPackageDialogResult=291566&SearchResultsGrid_ctl11_ViewPackageDialogResult=291563&SearchResultsGrid_ctl12_ViewPackageDialogResult=291562″

Scripting for Microsoft ASP.NET VIEWSTATE in VuGen LoadRunner

ASP.NET web applications maintain state of data by passing hidden _VIEWSTATE field encoded using base64 algorithm. The state of the page is restored based on the VIEWSTATE. If on the client side, the value of textbox has been changed, VIEWSTATE will now have the new value. (Obviously, this stage does not happen when the page is requested for the first time).

Let’s see how Web Applications developer store objects in VIEWSTATE,

// Storing a Customer object in view state.
Customer cust = new Customer ("Dilip", "Kutarmare");
ViewState["CurrentCustomer"] = cust;

// Retrieve a student from view state.
Customer cust = (Customer) ViewState["CurrentCustomer"];

Now we will see how to capture and replace VIEWSTATE in a recorded script,

1. First set the html parameter length to maximum so that it can store VIEWSTATE, You can adjust this number after examining the captured value.

web_set_max_html_param_len("50000");

2. Capture the VIEWSTATE value when it is returned from the server using web_reg_save_param() function

web_reg_save_param("MyViewState","LB=\"__VIEWSTATE\" value=\"","RB=\"","ORD=ALL",LAST);

3. Substitute the captured value with the parameter name in which you captured the VIEWSTATE:

"Name=__VIEWSTATE", "value={MyViewState}", ENDITEM,

4. Run the script (with enabled advanced log) in VuGen and verify that the correlation function is capturing the right value.

Removing Zero from the Date in VuGen LoadRunner

Enter start date and end date and date should not include zero before actual value of month and day.

for example: Invalid date format: 04/27/2011

                       Valid date format: 4/27/2011

Script:

//variable declaration
char month[10], day[10], year[10], startDate[10], endDate[10];
int nonZeroDay, nonZeroMonth;

/*if you want to run script for multiple interactions then you have to nullify
the concatenated string before starting new iteration otherwise it
will keep the old value*/
strcpy(startDate, "");
strcpy(endDate, "");

//original date which contain zero in the date
lr_save_datetime("Today’s Date is: %m/%d/%Y", DATE_NOW, "normalDate");
lr_output_message(lr_eval_string("{normalDate}"));

//for generating non zero start date

//to assign the current date parts to a parameter
lr_save_datetime("%m", DATE_NOW, "month");
lr_save_datetime("%d", DATE_NOW, "day");
lr_save_datetime("%Y", DATE_NOW, "year");

//to remove zero from the string. e.g.getting '4' from '04'

nonZeroMonth = atoi(lr_eval_string("{month}"));
lr_save_int(nonZeroMonth, "month");

nonZeroDay = atoi(lr_eval_string("{day}"));
lr_save_int(nonZeroDay, "day");

//creating a date string using concatenation of nonzero date parts and separator '/'
strcat(startDate, lr_eval_string("{month}"));
strcat(startDate, "/");
strcat(startDate, lr_eval_string("{day}"));
strcat(startDate, "/");
strcat(startDate, lr_eval_string("{year}"));

lr_save_string(startDate, "p_StartDate");
lr_output_message("Start Date is: %s", lr_eval_string("{p_StartDate}"));

//for generating non zero end date by incrementing date by one day offset
lr_save_datetime("%m", DATE_NOW + ONE_DAY, "month");
lr_save_datetime("%d", DATE_NOW + ONE_DAY, "day");

lr_save_datetime("%Y", DATE_NOW + ONE_DAY, "year");
nonZeroMonth = atoi(lr_eval_string("{month}"));
lr_save_int(nonZeroMonth, "month");

nonZeroDay = atoi(lr_eval_string("{day}"));
lr_save_int(nonZeroDay, "day");

strcat(endDate, lr_eval_string("{month}"));
strcat(endDate, "/");
strcat(endDate, lr_eval_string("{day}"));
strcat(endDate, "/");
strcat(endDate, lr_eval_string("{year}"));

lr_save_string(endDate, "p_endDate");
lr_output_message("End Date is: %s", lr_eval_string("{p_endDate}"));


Output:


Today’s Date is: 04/27/2011
Start Date is: 4/27/2011
End Date is: 4/28/2011

lr_eval_string in load Runner

To correlate statements for protocols that do not have specific functions, you can use the C Vuser
correlation functions. These functions can be used for all C-type Vusers, to save a string to a
parameter and retrieve it when required.

lr_eval_string :Replaces all occurrences of a parameter with its current value.
lr_save_string :Saves a null-terminated string to a parameter.
lr_save_var :Saves a variable length string to a parameter.

Using lr_eval_string:

In the following example, lr_eval_string replaces the parameter row_cnt with its current value. This value is sent to the Output window using lr_output_message.

lrd_stmt(Csr1, "select count(*) from employee", -1, 1 /*Deferred*/,
...);
lrd_bind_col(Csr1, 1, =;COUNT_D1, 0, 0);
lrd_exec(Csr1, 0, 0, 0, 0, 0);
lrd_save_col(Csr1, 1, 1, 0, "row_cnt");
lrd_fetch(Csr1, 1, 1, 0, PrintRow2, 0);
lr_output_message("value: %s" , lr_eval_string("The row count is:
"));

Using Vuser Functions in GUI Vuser Scripts

This section lists the Vuser functions that you can use to enhance your GUI user scripts.

declare_rendezvous: Declares a rendezvous.

declare_transaction :Declares a transaction.

end_transaction :Marks the end of a transaction for performance analysis.

error_message: Sends an error message to the Controller.

get_host_name: Returns the name of a load generator.

get_master_host_name :Returns the name of the Controller load generator.

lr_whoami :Returns information about the Vuser executing the script.

output_message: Sends a message to the Controller.

rendezvous: Sets a rendezvous point in a Vuser script.

start_transaction: Marks the beginning of a transaction for performance analysis.

user_data_point: Records a user-defined data sample