Y-lib
Loadrunner libraries
Macros | Functions | Variables
Core of Ylib

Macros

#define RAND_MAX   32767
 RAND_MAX constant for use with rand() - 15 bits integer. More...
 
#define Y_RAND_MAX   1073741823
 Alternate RAND_MAX constant for use with y_rand. More...
 
#define vuser_init()   vuser_init() { y_setup(); return y_vuser_init(); } y_vuser_init()
 Hook to ensure that y_setup() is called at start-up. This allows many performance improvements in the library. More...
 
#define y_is_vugen_run()   y_is_vugen_run_bool
 Test if this script is running in vugen (debug mode) More...
 

Functions

void y_setup ()
 Ylib setup - determines and stores the identity of the virtual user. More...
 
long y_rand (void)
 Generate a random (integer) number between 0 and Y_RAND_MAX (30 bit maxint). More...
 
double y_drand (void)
 Generate a random number between 0 <= y_drand() < 1. This supersedes y_rand().
Equal to Math.random in Java and JavaScript, Random.NextDouble in C#, etc. Better distribution of the random numbers over the range than by using y_rand() with modulo (%) – thus no skewed results. More...
 
char * y_mem_alloc (size_t size)
 Ylib wrapper for malloc() More...
 
char * y_array_alloc (size_t length, size_t size)
 Allocates a character array and initializes all elements to zero As y_mem_alloc(), but using the 'calloc' function, rather than 'malloc()'. More...
 
char * y_strdup (char *source)
 Copy a string into a malloc'd piece of memory using strdup(), and lr_abort() if the allocation fails. See the strdup() C documentation for what it does. This is just a simple wrapper around it that catches the strdup return value and handles any errors by aborting the script. More...
 
char * y_get_parameter_eval_string (const char *param_name)
 Obtain the string required to fetch the contents of a parameter through lr_eval_string(). More...
 
int y_is_empty_parameter (const char *param_name)
 Test if the given parameter is empty or not yet set. (These are two different things..) It would be nice if loadrunner had a builtin for this. More...
 
char * y_get_parameter (const char *param_name)
 Get the content of a parameter and return it as a char *. More...
 
char * y_get_parameter_or_null (const char *param_name)
 Get the content of a parameter and return it as a char *, or return NULL if it wasn't set. More...
 
char * y_get_parameter_with_malloc_or_null (const char *src_param)
 Get the content of a parameter and return it as a char * (malloc version) More...
 
char * y_get_parameter_ext (const char *source_param)
 Get the content of a parameter and return it as a char * (lr_eval_string_ext() version) More...
 

Variables

int y_virtual_user_id = 0
 The virtual user id, as reported by lr_whoami(). More...
 
char * y_virtual_user_group = NULL
 The virtual user group, as reported by lr_whoami(). More...
 
int y_scid
 The virtual user scid, as reported by lr_whoami(). More...
 
int y_is_vugen_run_bool = 0
 Boolean, true when running in Vugen. Not able to do this in pre-compile phase. More...
 

Detailed Description

Macro Definition Documentation

#define RAND_MAX   32767

RAND_MAX constant for use with rand() - 15 bits integer.

Loadrunner does not give you full C headers, so the 'RAND_MAX' #define from <stdlib.h> is missing. We define it here mostly for documentation, as we do not have access to the header files themselves and therefore cannot change this.

Author
Floris Kraak

Definition at line 57 of file y_core.c.

#define vuser_init ( )    vuser_init() { y_setup(); return y_vuser_init(); } y_vuser_init()

Hook to ensure that y_setup() is called at start-up. This allows many performance improvements in the library.

Definition at line 106 of file y_core.c.

#define y_is_vugen_run ( )    y_is_vugen_run_bool

Test if this script is running in vugen (debug mode)

Returns
1 (true) if running in vugen, zero (false) otherwise.

Recommended practice: Use this to create script debugging code that will hit all of the functional code inside the script when run in Vugen, but the full (semi-randomized) realistic scenario when it runs as part of a load test.

Note
This relies on the y_virtual_user_id_bool variable as an indication of where the script is running. Inside Vugen that variable should be -1; Otherwise, it contains a non-negative number. This can be manipulated to your advantage, but may break if HP ever changes that convention.

Example:

1 debug_clickflow()
2 {
3  do_stuff();
4 }
5 
6 loadtest_clickflow()
7 {
8  do_more_complicated_stuff();
9 }
10 
11 Action()
12 {
13  if( y_is_vugen_run() )
14  debug_clickflow();
15  else
16  loadtest_clickflow();
17 }
See also
y_setup()

Definition at line 142 of file y_core.c.

#define Y_RAND_MAX   1073741823

Alternate RAND_MAX constant for use with y_rand.

y_rand() provides for a far bigger ceiling to the random number generator: 31 bits on Windows and 30 bits otherwise, instead of 15.

Author
Floris Kraak

Definition at line 70 of file y_core.c.

Function Documentation

char* y_array_alloc ( size_t  length,
size_t  size 
)

Allocates a character array and initializes all elements to zero As y_mem_alloc(), but using the 'calloc' function, rather than 'malloc()'.

Parameters
[in]lengthExpected number of characters.
[in]sizeHow much space a single character requires. Usually this should contain "sizeof char".
Returns
A pre-zeroed block of memory of the requisite size allocated using calloc().
Warning
The memory resulting from this call will need to be freed using free().
See also
y_mem_alloc(), calloc()

Definition at line 245 of file y_core.c.

double y_drand ( void  )

Generate a random number between 0 <= y_drand() < 1. This supersedes y_rand().
Equal to Math.random in Java and JavaScript, Random.NextDouble in C#, etc. Better distribution of the random numbers over the range than by using y_rand() with modulo (%) – thus no skewed results.

Returns
Random number between 0 and 1 (exclusive). Accuracy: 9.4 digits (32 bits) thus increments of approx. 2.3e-10.

Examples:

1 if (y_drand() < 0.123) ... -> change 12.3% true
2 double value = min + y_drand() * (max - min); // to range example
3 int value = min + y_drand() * (max - min + 1); // for min <= value <= max
Author
A.U. Luyer

Definition at line 189 of file y_core.c.

char* y_get_parameter ( const char *  param_name)

Get the content of a parameter and return it as a char *.

This is useful mostly for code that wants to manipulate parameter contents but not care about the name of the parameter itself. (Something which applies to most of ylib ..)

Parameters
[in]param_nameThe name of the parameter to fetch.
Returns
A char* buffer containing the contents of the parameter, allocated by lr_eval_string().
Warning
This returns memory allocated by lr_eval_string(). It is freed at the end of the iteration.

Example:

1 char *test;
2 lr_save_string("test123", "TestParam"); // save the string "test123" into parameter {TestParam}
3 test=y_get_parameter("TestParam");
4 lr_message("Test: %s", test);
See also
y_is_empty_parameter(), y_get_parameter_ext(), y_get_parameter_or_null()
Author
Floris Kraak

Definition at line 336 of file y_core.c.

char* y_get_parameter_eval_string ( const char *  param_name)

Obtain the string required to fetch the contents of a parameter through lr_eval_string().

Parameters
[in]param_nameThe parameter name to construct the eval text for.
Returns
a char* allocated with y_mem_alloc()
Warning
The return argument will need to be freed via a call to free()
See also
y_get_parameter(), y_get_parameter_ext(), y_get_parameter_or_null()
Author
Floris Kraak

Definition at line 287 of file y_core.c.

char* y_get_parameter_ext ( const char *  source_param)

Get the content of a parameter and return it as a char * (lr_eval_string_ext() version)

Like y_get_parameter, but the result will use lr_eval_string_ext() to acquire it's memory, rather than getting it from lr_eval_string. This can be useful when you want your data to remain in memory instead of getting freed at the end of each iteration. An example is the browser emulation code in y_emulate_browser.c, which sets up a linked list that has to stay allocated throughout the duration of the test. (And therefore never needs to be freed. But I digress.)

Parameters
[in]source_paramThe name of the parameter to fetch.
Returns
A char* buffer containing the contents of the parameter, allocated with lr_eval_string_ext()
Warning
Memory allocated in this manner must be freed using lr_eval_string_ext_free() or it will linger.

Example:

1 char *test;
2 lr_save_string("test123", "TestParam"); // save the string "test123" into parameter {TestParam}
3 test=y_get_parameter_ext("TestParam");
4 lr_message("Test: %s", test);
5 lr_eval_string_ext_free(test);
Author
Floris Kraak
See also
y_get_parameter(), lr_eval_string_ext()

Definition at line 441 of file y_core.c.

char* y_get_parameter_or_null ( const char *  param_name)

Get the content of a parameter and return it as a char *, or return NULL if it wasn't set.

This will return null in the most typical case: A parameter saved with web_reg_save_param(), but never filled. The actual check employed here is a test that looks if the parameter content matches the parameter name surrounded by brackets.

If the parameter was never filled, lr_eval_string() will return that. However, in many more elaborate cases we really need to know if it was never filled to begin with. This function mimics the behaviour we really want to see in LR, but don't have. (At least, not in LR 11.05, the version I'm working with.)

It would be really nice if there was a loadrunner built-in that did this.

Parameters
[in]param_nameThe name of the parameter to fetch.
Returns
A char* buffer containing the contents of the parameter, allocated by lr_eval_string(), or NULL.
Warning
This returns memory allocated by lr_eval_string(). It is likely to disappear (get freed) at the end of the iteration.
If the content of the parameter matches the name of the parameter surrounded by brackets this function will return NULL even if it's not empty.

Example:

1 char *test;
2 lr_save_string("test123", "TestParam"); // save the string "test123" into parameter {TestParam}
3 test=y_get_parameter("TestParam");
4 lr_message("Test: %s", test);
See also
y_get_parameter(), y_is_empty_parameter()
Author
Floris Kraak

Definition at line 373 of file y_core.c.

char* y_get_parameter_with_malloc_or_null ( const char *  src_param)

Get the content of a parameter and return it as a char * (malloc version)

This is like y_get_parameter(), but the result will use memory allocated with y_mem_alloc(), instead of acquired from lr_eval_string().

Parameters
[in]src_paramThe name of the parameter to fetch.
Returns
A char* buffer containing the contents of the parameter, allocated with y_mem_alloc()
Warning
Memory allocated in this manner must be freed using free() or it will linger.

Example:

1 char *test;
2 lr_save_string("test123", "TestParam"); // save the string "test123" into parameter {TestParam}
3 test=y_get_parameter_in_malloc_string("TestParam");
4 lr_message("Test: %s", test);
5 free(test);
Author
Floris Kraak

Definition at line 406 of file y_core.c.

int y_is_empty_parameter ( const char *  param_name)

Test if the given parameter is empty or not yet set. (These are two different things..) It would be nice if loadrunner had a builtin for this.

Parameters
[in]param_nameThe name of the parameter to
Returns
non-zero (true) if the parameter is empty, zero (false) otherwise.
See also
y_get_parameter_eval_string()
Author
Floris Kraak

Definition at line 305 of file y_core.c.

char* y_mem_alloc ( size_t  size)

Ylib wrapper for malloc()

Allocates a block of memory, but aborts the Vuser if that fails.

Parameters
[in]sizeNumber of bytes required.
Warning
The memory resulting from this call will need to be freed using free().

Example:

1 char *example_string = "some_text";
2 int size = strlen(example_string)+1;
3 char *example_string_copy = y_mem_alloc(size);
4 snprintf(example_string_copy, size, "%s", example_string);
5 lr_log_message("Copy of example string contains: %s", example_string_copy);
6 free(example_string_copy);
See also
y_array_alloc(), malloc()

Definition at line 221 of file y_core.c.

long y_rand ( void  )

Generate a random (integer) number between 0 and Y_RAND_MAX (30 bit maxint).

Returns
Random number (integer) between 0 and Y_RAND_MAX: 30-bit maxint - slightly over 1 billion.
Note
Superseded by y_drand

Example:

1 int random_number;
2 random_number=y_rand();
Author
Floris Kraak
Deprecated:
Superseded by y_drand

Definition at line 156 of file y_core.c.

void y_setup ( )

Ylib setup - determines and stores the identity of the virtual user.

This runs lr_whoami and sets y_virtual_user_id and y_virtual_user_group as global variables. Called y_rand() (for it's seed), y_is_vugen_run() and others dynamically.

Returns
void
Warning
Only call this if you need the y_virtual_user_id and y_virtual_group variables to be set. Ylib functions that need this will call it when required.
See also
y_rand()
Author
Floris Kraak

Definition at line 85 of file y_core.c.

char* y_strdup ( char *  source)

Copy a string into a malloc'd piece of memory using strdup(), and lr_abort() if the allocation fails. See the strdup() C documentation for what it does. This is just a simple wrapper around it that catches the strdup return value and handles any errors by aborting the script.

Parameters
[in]sourceThe string to copy.
Returns
A copy of the string, allocated via strdup().
Author
Floris Kraak

Definition at line 268 of file y_core.c.

Variable Documentation

int y_is_vugen_run_bool = 0

Boolean, true when running in Vugen. Not able to do this in pre-compile phase.

See also
y_setup()

Definition at line 46 of file y_core.c.

int y_scid

The virtual user scid, as reported by lr_whoami().

See also
y_setup()

Definition at line 44 of file y_core.c.

char* y_virtual_user_group = NULL

The virtual user group, as reported by lr_whoami().

See also
y_setup()

Definition at line 42 of file y_core.c.

int y_virtual_user_id = 0

The virtual user id, as reported by lr_whoami().

See also
y_setup()

Definition at line 40 of file y_core.c.