Y-lib
Loadrunner libraries
Data Structures | Macros | Functions | Variables
y_browseremulation.c File Reference

Code for supporting (more) realistic browser emulation. More...

#include "y_core.c"
#include "y_string.c"

Go to the source code of this file.

Data Structures

struct  y_browser
 Internal browser structure, describing a browser as an entity. More...
 

Macros

#define MAX_BROWSER_LIST_LENGTH   1000
 Limit the browser list to 1000 browsers, as a failsafe in case of wrong parameter settings (which may cause endless loops otherwise) More...
 

Functions

void y_log_browser (const y_browser *browser)
 Log the content of a browser object. More...
 
void y_save_browser_to_parameters (const y_browser *browser)
 Save the contents of a browser struct as a series of "browser_*" loadrunner parameters. More...
 
int y_setup_browser_emulation_from_parameters (const char *browser_name_param, const char *browser_chance_param, const char *browser_max_connections_per_host_param, const char *browser_max_connections_param, const char *browser_user_agent_string_param)
 Initialize the browser list based on a set of prepared parameters. More...
 
int y_setup_browser_emulation ()
 Initialize the browser list, using the default values for the parameter names: More...
 
int y_setup_browser_emulation_from_file (char *filename)
 Initialize the browser list based on a tab-seperated CSV file. More...
 
y_browser * y_choose_browser_from_list (y_browser *browser_list_head, int browser_list_chance_total)
 Choose a browser profile from a browser list at random using the defined weights in that list. More...
 
y_browser * y_choose_browser ()
 Choose a browser profile from the browser list at random using the defined weights. More...
 
int y_emulate_browser (y_browser *new_browser)
 Emulate a specific browser. More...
 

Variables

y_browser * y_browser_list_head = NULL
 The first element in the browser list. More...
 
int y_browser_list_chance_total = 0
 The total of all the browser weights added together. More...
 

Detailed Description

Code for supporting (more) realistic browser emulation.

Basic concept: The tester defines a list of browsers that contains:

  1. browser name
  2. browser chance - A weight determining the odds that said browser will be chosen. Can be a percentage, or a count from a production access log.
  3. max connections - How many connections said browser uses (See: www.browserscope.org for what this value should be for your browser)
  4. max connections per host - Same as above, but this setting limits the number of connections to a single hostname.
  5. User agent string - A user agent string used by said browser.

This list is read in during vuser_init() to construct a list of browsers, which can then be used during the loadtest to dynamically choose a browser from the list.

Usage:

  1. Set up the appropriate parameters
  2. Add this file to your script, plus any supporting ylib files.
  3. Add an include statement: '#include "y_browseremulation.c' to the top of vuser_init()
  4. Call y_setup_browser_emulation() (or one of the alternative variants) in vuser_init()

    Example:

    {
    y_log_turn_off(); // Suppress the logging - the setup code can create a fair amount of (harmless) log messages.
    y_log_restore(); // Resume logging.
    }
  5. Call y_choose_browser() and y_emulate_browser() at the start of each iteration.

    Example:

    Action()
    {
    // This will set up some connection options for emulating different browser versions in a realistic fashion
    {
    // Note: This will blow up if the setup code hasn't been called beforehand.
    y_browser* browser = y_choose_browser();
    }
    // .. more code ..
    }
Author
Floris Kraak

Definition in file y_browseremulation.c.

Macro Definition Documentation

#define MAX_BROWSER_LIST_LENGTH   1000

Limit the browser list to 1000 browsers, as a failsafe in case of wrong parameter settings (which may cause endless loops otherwise)

Definition at line 104 of file y_browseremulation.c.

Function Documentation

y_browser* y_choose_browser ( )

Choose a browser profile from the browser list at random using the defined weights.

This wrapper around y_choose_browser_from_list() uses the list pointed to by y_browser_list_head to choose from.

Returns
a pointer to a randomly chosen struct browser, or NULL in case of errors.

Example:

1 // This will set up some connection options for emulating different browser versions in a realistic fashion
2 {
3  // Note: This will blow up if the setup code hasn't been called beforehand.
4  y_browser* browser = y_choose_browser();
5  y_emulate_browser(browser);
6 }
See also
y_choose_browser_from_list(), y_setup_browser_emulation_from_file(), y_setup_browser_emulation_from_parameters(), y_browseremulation.c, y_browser_list_head
Author
Floris Kraak

Definition at line 558 of file y_browseremulation.c.

y_browser* y_choose_browser_from_list ( y_browser *  browser_list_head,
int  browser_list_chance_total 
)

Choose a browser profile from a browser list at random using the defined weights in that list.

Parameters
[in]browser_list_headThe first entry of a previously constructed single-linked browser list.
[in]browser_list_chance_totalThe total of the browser weights in the list.
Returns
a pointer to a randomly chosen struct browser, or NULL in case of errors.

Example:

1 // This will set up some connection options for emulating different browser versions in a realistic fashion
2 {
3  // Note: This will blow up if the setup code hasn't been called beforehand.
4  y_browser* browser = y_choose_browser_from_list(y_browser_list_head, y_browser_list_chance_total);
5  y_emulate_browser(browser);
6 }
Note
In most cases a call to the y_choose_browser() wrapper should suffice, instead.
See also
y_choose_browser(), y_setup_browser_emulation_from_file(), y_setup_browser_emulation_from_parameters(), y_browseremulation.c, y_browser_list_head
Author
Floris Kraak

Definition at line 484 of file y_browseremulation.c.

int y_emulate_browser ( y_browser *  new_browser)

Emulate a specific browser.

This will set up the MAX_TOTAL_CONNECTIONS, MAX_CONNECTIONS_PER_HOST socket options and user_agent_string settings for the browser listed.

Example:

1 // This will set up some connection options for emulating different browser versions in a realistic fashion
2 {
3  // Note: This will blow up if the setup code hasn't been called beforehand.
4  y_browser* browser = y_choose_browser();
5  y_emulate_browser(browser);
6 }
See also
y_choose_browser, y_choose_browser_from_list, y_browser_emulation.c, web_set_sockets_option(), web_add_auto_header()
Parameters
[in]new_browserPointer to a browser struct for the browser to be emulated.
Returns
0 if no errors occurred, -1 in case of an error.
Author
Floris Kraak

Definition at line 583 of file y_browseremulation.c.

void y_log_browser ( const y_browser *  browser)

Log the content of a browser object.

Given a pointer to a browser struct, log what said browser struct contains. This is useful for debugging.

Note
Passing in NULL will result in a call to lr_abort().
Parameters
[in]browserA pointer to the browser struct to log.
Author
Floris Kraak

Definition at line 122 of file y_browseremulation.c.

void y_save_browser_to_parameters ( const y_browser *  browser)

Save the contents of a browser struct as a series of "browser_*" loadrunner parameters.

This will create the following set of parameters from the browser object:

  • browser_name
  • browser_chance
  • browser_max_connections_per_host
  • browser_max_connections
  • browser_user_agent_string
Parameters
[in]browserA pointer to the browser to create parameters for.
Note
y_emulate_browser will use this, so these parameters will contain details about what browser the script is currently emulating after that call.
Passing in NULL will result in a call to lr_abort().
Author
Floris Kraak

Definition at line 154 of file y_browseremulation.c.

int y_setup_browser_emulation ( )

Initialize the browser list, using the default values for the parameter names:

  • browser_name_param = browser_name
  • browser_chance_param = browser_chance
  • browser_max_connections_per_host_param = browser_max_connections_per_host
  • browser_max_connections_param = browser_max_connections
  • browser_user_agent_string_param = browser_user_agent_string

Example:

1 vuser_init()
2 {
3  y_log_turn_off(); // Suppress the logging - the setup code can create a fair amount of (harmless) log messages.
4  y_setup_browser_emulation_from_parameters("browser_name", "browser_chance", "browser_max_connections_per_host", "browser_max_connections", "browser_user_agent_string");
5  y_log_restore(); // Resume logging.
6 }
Note
We may remove this in favor of a call to y_setup_browser_emulation_from_file() at some point.
See also
y_setup_browser_emulation_from_parameters(), y_setup_browser_emulation_from_file(), y_browseremulation.c
Author
Floris Kraak

Definition at line 328 of file y_browseremulation.c.

int y_setup_browser_emulation_from_file ( char *  filename)

Initialize the browser list based on a tab-seperated CSV file.

This will initialize the single-linked list of browsers, using a text file containing on each line one browser with the following fields, seperated by tabs:

  1. browser name
  2. chance (weight)
  3. max_connections
  4. max_connections_per_host
  5. user_agent

The order of the fields is important; They should be listed on each line in the above order.

Note
The file should not contain headers of any kind, nor the string "END" as y_setup_browser_emulation_from_parameters() requires.

Example:

1 vuser_init()
2 {
3  y_log_turn_off(); // Suppress the logging - the setup code can create a fair amount of (harmless) log messages.
4  y_setup_browser_emulation_from_file("browser.dat");
5  y_log_restore(); // Resume logging.
6 }
Note
Empty lines and comments (text starting with "#") will be ignored.

Call this during vuser_init().

Parameters
[in]filenameThe name of the file to read.
Returns
0 if setup was successful, -1 in case of an error.
See also
y_setup_browser_emulation_from_parameters()
y_browser_list_head
y_browser_list_chance_total
y_browseremulation.c
Note
You may wish to temporarily disable logging for this call using y_log_turn_off() and y_log_restore().
Author
Floris Kraak

Definition at line 371 of file y_browseremulation.c.

int y_setup_browser_emulation_from_parameters ( const char *  browser_name_param,
const char *  browser_chance_param,
const char *  browser_max_connections_per_host_param,
const char *  browser_max_connections_param,
const char *  browser_user_agent_string_param 
)

Initialize the browser list based on a set of prepared parameters.

This will initialize the single-linked list of browsers, using the given set of parameters. The 'browser_name' parameter named here is expected to be set to "Select Next Row: Sequential", and "Update value on: Each iteration". The -last- browser name in the list has to contain the value "END" to mark the end of the list. All other parameters should be set to "Select Next Row: Same line as {browser_name_param}".

Call this during vuser_init().

Parameters
[in]browser_name_paramBrowser name. Set this parameter to "Select Next Row: Sequential", and "Update value on: Each iteration". The last browser name should be "END".
[in]browser_chance_paramBrowser weight. Can be any number as long as the total of chances does not exceed Y_RAND_MAX. Set this parameter to "Select Next Row: Same line as {browser_name_param}".
[in]browser_max_connections_per_host_paramMaximum number of connections this browser allows per host. See www.browserscope.org for possible values. Set this parameter to "Select Next Row: Same line as {browser_name_param}".
[in]browser_max_connections_paramMaximum number of connections this browser allows. See www.browserscope.org for possible values. Set this parameter to "Select Next Row: Same line as {browser_name_param}".
[in]browser_user_agent_string_paramThe user agent string this browser reports to the server. See your production HTTP access logs for examples. Set this parameter to "Select Next Row: Same line as {browser_name_param}".
Returns
0 if successful, -1 if errors occurred during setup.

Example:

1 vuser_init()
2 {
3  y_log_turn_off(); // Suppress the logging - the setup code can create a fair amount of (harmless) log messages.
4  y_setup_browser_emulation_from_parameters("browser_name", "browser_chance", "browser_max_connections_per_host", "browser_max_connections", "browser_user_agent_string");
5  y_log_restore(); // Resume logging.
6 }
See also
y_setup_browser_emulation_from_file()
y_browser_list_head
y_browser_list_chance_total
y_browseremulation.c
Note
You may wish to temporarily disable logging here using y_log_turn_off() and y_log_restore().
Author
Floris Kraak

Definition at line 210 of file y_browseremulation.c.

Variable Documentation

int y_browser_list_chance_total = 0

The total of all the browser weights added together.

Definition at line 110 of file y_browseremulation.c.

y_browser* y_browser_list_head = NULL

The first element in the browser list.

Definition at line 107 of file y_browseremulation.c.