This is a repository of code snippets. Please send me any useful sections of code that you have written.

Note that this repository does not contain trivial examples (i.e. something you could learn by looking at the example code in the LoadRunner Online Function Reference – accessed by pressing F1 in VuGen).

Code snippets that have been written as separate Tech Tips:

Writing to a file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Writes a string to the end of a file.
Arguments:
 - file_name: Include the full path in the file name, and escape any slashes. E.g. "C:\\TEMP\\output.txt". Note that file does not have to exist beforehand, but directory does.
 - string: If attempting to write a single line, include a newline character at the end of the string.
Returns 0 on success. On failure, function will raise lr_error_message and return -1.
*/
int jds_append_to_file(char* file_name, char* string) {
  int fp; // file pointer
  int rc; // return code
  int length = strlen(string);
 
  // Check that file_name is not NULL.
  if (file_name == NULL) {
    lr_error_message("Error. File name is NULL");
	return -1;
  }
 
  fp = fopen(file_name, "a"); // open file in "append" mode.
  if (fp == NULL) {
    lr_error_message("Error opening file: %s", file_name);
    return -1;
  }
 
  rc = fprintf(fp, "%s", string);
  if (rc != length) {
     lr_error_message("Error writing to file: %s", file_name);
     return -1;
  }
 
  rc = fclose(fp);
  if (rc != 0) {
    lr_error_message("Error closing file: %s", file_name);
    return -1;
  }
 
  return 0;
}

Check if a file already exists

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Checks if a file already exists on the filesystem.
// Arguments:
//  - file_name: Include the full path in the file name. 
// Returns TRUE (1) if file exists and user has read access to the file, otherwise function returns FALSE (0).
int jds_file_exists(char* file_name) {
  int fp; // file pointer
 
  fp = fopen(file_name, "r+"); // open file in read mode. File must already exist.
  if (fp == NULL) {
    return FALSE;
  } else {
    fclose(fp);
    return TRUE;
  }
}

Saving a file to the hard disk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Saves a file to the hard disk.
// Arguments:
// - file_name: Include the full path in the file name. Note that file must not exist before function is called.
// - file_content: The data to save to the file. Can be binary or string data.
// - file_size: The size/length of the data to save to the file. If it is string data, you can find this using strlen(). If you are saving binary data from a web page, use web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE).
// Returns 0 on success. On failure, function will raise lr_error_message and return -1.
int jds_save_file(char* file_name, void* file_content, unsigned int file_size) {
  int rc; // function return code
  int fp; // file pointer
 
  // Check input values
  if (file_name == NULL) {
    lr_error_message("File name is NULL");
    return -1;
  } else if (file_content == NULL) {
    lr_error_message("File content is NULL");
    return -1;
  } else if (file_size < 1) {
    lr_error_message("Invalid file size: %d", file_size);
    return -1;
  }
 
  // Does the file already exist?
  if (jds_file_exists(file_name) == TRUE) {
    lr_error_message("File %s already exists", file_name);
    return -1;
  }
 
  fp = fopen(file_name, "wb"); // open file in "write, binary" mode.
  if (fp == NULL) {
    lr_error_message("Error opening file: %s", file_name);
    return -1;
  }
 
  rc = fwrite(file_content, file_size, 1, fp);
  if (rc != 1) {
    lr_error_message("Error writing to file. Items written: %d", rc);
    return -1;
  }
 
  rc = fclose(fp);
  if (rc != 0) {
    lr_error_message("Error closing file: %s", file_name);
    return -1;
  }
 
  return 0;
}

Saving a binary file from a webpage (like a PDF or a GIF). Note that this is not a good way to veryify that your LoadRunner/BPM script is running successfully.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Action()
{
  int size;
  char* file = "C:\\TEMP\\test.zip";
 
  // Make this big enough to hold the downloaded file. 
  web_set_max_html_param_len("1048576"); // 1 MB
 
  // Save entire HTTP response body
  web_reg_save_param("FileContents",
    "LB/BIN=",
    "RB/BIN=",
    "Search=Body",
    LAST);
 
  // Note that it is best to use web_custom_request, as this guarantees that only one file is being downloaded by this step.
  web_custom_request("DownloadPlugin", 
    "URL=http://www.example.com/files/test.zip", 
    "Method=GET", 
    "Resource=1", 
    "RecContentType=text/css", 
    "Referer=http://www.jds.net.au", 
    "Snapshot=t1.inf", 
    LAST);
 
  // returns the size of the previous HTTP response
  size = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
 
  jds_save_file(file, lr_eval_string("{FileContents}"), size);
 
  return 0;
}

9 comments on “VuGen Code Snippets

  1. I need to prepare a script handling dropdown values as below:

    My web page is having 3 dropdown values. web_url function for that page generates server response for all 3 drpdown values at a time.

    Eg:

    Question1
    Question2
    Question3

    Question1
    Question2
    Question3

    Question1
    Question2
    Question3

    All the dropdown values that are generated changes for each execution. Number of dropdown values differ from user to user.
    Eg: Next user it could be


    Question1
    Question2
    Question3
    Question2
    Question3

    Question1
    Question2
    Question3

    Question1
    Question2

    So, I need to prepare a script which counts number of values present in dropdown1, dropdown2, dropdown3 seperately. But, When I take —-
    “LB =Option value =\ “” and “RB=\”>” in web_reg_save_param….. It gives me count for all dropdown values Eg. 9 (in first case) and 10(in above case)

    Please let me know How can I handle it?

  2. I need to prepare a script handling dropdown values as below:

    My web page is having 3 dropdown values. web_url function for that page generates server response for all 3 drpdown values at a time.

    Eg:

    Question1
    Question2
    Question3

    Question1
    Question2
    Question3

    Question1
    Question2
    Question3

    All the dropdown values that are generated changes for each execution. Number of dropdown values differ from user to user.
    Eg: Next user it could be

    Question1
    Question2
    Question3
    Question2
    Question3

    Question1
    Question2
    Question3

    Question1
    Question2

    So, I need to prepare a script which counts number of values present in dropdown1, dropdown2, dropdown3 seperately. But, When I take —-
    “LB =Option value =\ “” and “RB=\”>” in web_reg_save_param….. It gives me count for all dropdown values Eg. 9 (in first case) and 10(in above case)

    Please let me know How can I handle it?

    1. To find the size of a file you are downloading, try

      web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);

      To find the size of a file on disk, try

      web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);

      Cheers,
      Stuart.

  3. I need some help with vugen code. I am working on a asynchronous document depository application. I first have to go to a mapped network drive to either create a text file or open up a PDF or word files(with different size..5MB, 10MB, 20MB) then save them. Once it is saved there is a process that runs in the background and the web application will pick up this newly created/ modified file. I need to track the latency. My thoughts are to create a web script in vugen using the C function fopen something like that to write a file to source location. Then use the use vugen web function to search for the file in the web application using a loop.

    I’m not sure about the fopen syntax/ is there something else I should be using pulling the various size of the bigger files, can someone provide some examples for the pseudo code please.

    Thanks!

    1. Rather than using fopen(), I would suggest that you solve your problem by calling a command line program using either system() or popen().
      [http://www.myloadtest.com/dos-commands-from-loadrunner/]

      The cp command can create a new file by copying an existing file and giving it a new name, and the touch command can update the “last modified” property of the file. [http://en.wikipedia.org/wiki/Touch_%28Unix%29] You can easily find a Windows port of the touch command with a little searching.

      For your script, I would do it like this:

      1. “update” file on the network drive
      2. get system time
      3. query the database – do this *infrequently* – like every minute or two, so all of your vusers do not introduce too much additional load on the system
      Check if the updated file has been added, and get the “last modified” timestamp from the database
      4. Subtract system timestamp from database timestamp and create your transaction timing in LoadRunner using lr_set_transaction().
      Note: the clocks on the two systems must be syncrhonised.

      Solving your problem this way allows you to measure the processing time of the files with accuracy to 1 second without having *all* of your vusers polling the web server every single second (which would create lots of load).

  4. Did you know about this function…

    1
    2
    
    //Creates a directory using the given path name. 
    int mkdir ( const char *path );

Leave a Reply