Saturday 27 April 2024

How to Save the downloaded file to local system in load runner

In certain scenarios, downloading files becomes a requirement. Instead of needing to store the files on your local machine for later verification, you may want to measure the transaction time of the download process. To do this, you can capture data from the server response and save it to a file using standard file operations. Just be sure you know the type of file you will be downloading.

Here's an example of downloading a `.pdf` file and saving it to your local system:


// Declare variables
int filePointer;
long fileSize;

// Create a file for writing
filePointer = fopen("c://test_file.pdf", "wb");

// Start a transaction to measure download time
lr_start_transaction("file_download");

// Increase the parameter size to accommodate the data
web_set_max_html_param_len("100000");

// Use web_reg_save_param with the appropriate boundaries to capture server response data for the download
web_reg_save_param("FILEDATA", "LB=", "RB=", "Search=Body", LAST);

// HTTP request to download the .pdf file
web_url("http://serverURL:port/app/resource/getme.pdf");

// Retrieve the download size
fileSize = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);

// Write the captured data to an output file
fwrite(lr_eval_string("{FILEDATA}"), fileSize, 1, filePointer);

// End the transaction
lr_end_transaction("file_download", LR_AUTO);

// Close the file
fclose(filePointer);

This code snippet demonstrates how to use file operations and LoadRunner functions to download a PDF file from a server, capture the download data, save it to a local file, and measure the transaction time. By following this process, you can efficiently analyze the download performance without needing to store the downloaded files on your machine for later verification.

Python Script for Dynamically Generating LoadRunner Transaction Names | Automate Transaction Names in LoadRunner

In performance testing and monitoring, it's crucial to understand how different parts of your application are performing under various conditions. Monitoring transactions around key sections of code, such as API calls and critical functions, helps identify performance bottlenecks and areas for optimization. LoadRunner provides powerful tools for performance testing, including functions like `lr_start_transaction` and `lr_end_transaction` that enable you to measure the execution time of specific code blocks. However, adding these functions manually can be a time-consuming and error-prone process. To streamline this task, I've developed a script that automatically inserts transaction monitoring statements around key function calls in a C file. This script simplifies the process and helps you achieve more efficient and consistent performance testing results. Let's explore how this script works.

import re

# Open the data file
with open('C:\\Users\\easyloadrunner\\Downloads\\FeatureChange-V1\\Action.c', 'r') as file:
    data = file.read()

# Split the data into lines
lines = data.split('\n')

# Initialize a variable to store the captured value
captured_value = ""

# Open the output file
with open('C:\\Users\\easyloadrunner\\Downloads\\FeatureChange-V1\\ModifiedAction.C', 'w') as output_file:
    # Loop through each line
    for line in lines:
        # Check if the line contains "web_custom_request "
        if "web_custom_request(" in line:
            # Use regular expression to capture the dynamic value
            captured_value = re.search(r'web_custom_request\("(.+?)"', line).group(1)
            # Add the captured value to the start of the transaction
            new_line = 'lr_start_transaction("' + captured_value + '");\n' + line
        elif "LAST);" in line:
            # Add the end of the transaction
            new_line = line + '\n' + 'lr_end_transaction("' + captured_value + '", LR_AUTO);'
        else:
            new_line = line
        # Write the modified line to the output file
        output_file.write(new_line + '\n')