Saturday, April 27, 2024

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')

No comments: