Thursday, April 25, 2024

How to execute python script in JMeter? | Python script with Apache JMeter

 You can execute Python scripts in JMeter by using the JSR223 Sampler, which supports scripting languages such as Groovy, JavaScript, and Python. The JSR223 Sampler is part of JMeter's core functionality and can be used to execute scripts within your JMeter test plan. Here's how you can execute a Python script in JMeter using the JSR223 Sampler:

1. Add a Thread Group: First, add a Thread Group to your test plan by right-clicking on the test plan and selecting *Add > Threads (Users) > Thread Group*. Configure the Thread Group settings as per your requirements.

2. Add a JSR223 Sampler: Within the Thread Group, add a JSR223 Sampler by right-clicking on the Thread Group and selecting *Add > Sampler > JSR223 Sampler*.

3. Configure the JSR223 Sampler:

    - Name: Give the sampler a descriptive name.

    - Language: Select "python" from the language dropdown.

    - Script: In the script box, enter the Python code you want to execute. You can either write your script directly in the script box or reference an external Python file.

4. Use External Python Files (Optional):

    - If you want to use an external Python file, you can import the file in the script box using the `import` statement.

    - Ensure the Python file is accessible from the JMeter script. You may need to adjust your classpath to include the directory where the file is located.

5. Add Listeners (Optional): You can add listeners like View Results Tree or Summary Report to view the output of the sampler.

6. Run the Test Plan: Execute the test plan by clicking on the green play button at the top of the JMeter interface. The Python script will be executed as part of the test plan.

Here is an example of how you might use the JSR223 Sampler to execute a Python script:


This script prints a message and performs a simple calculation. You can expand on this script to include more complex operations or integrate it with other parts of your JMeter test plan.

Method 2:

In JMeter, the OS Process Sampler is used to execute operating system commands, including scripts like Python scripts. You can use the OS Process Sampler to execute a Python script as part of your JMeter test plan. 

Here's how you can execute a Python script using the OS Process Sampler:
  • Add a Thread Group: First, add a Thread Group to your test plan by right-clicking on the test plan and selecting Add > Threads (Users) > Thread Group. Configure the Thread Group settings according to your needs.
  • Add an OS Process Sampler: Within the Thread Group, add an OS Process Sampler by right-clicking on the Thread Group and selecting Add > Sampler > OS Process Sampler.
  • Configure the OS Process Sampler:
    • Name: Give the sampler a descriptive name.
    • Command: Specify the command to execute the Python script. You will need to provide the path to the Python executable followed by the path to your Python script.
    • Arguments: If your Python script requires any command-line arguments, provide them here.
    • Working Directory: Set the working directory (optional), which is the directory where the script will execute.
  • For example, if you want to run a Python script called my_script.py located at /path/to/script/ using the Python interpreter located at /usr/bin/python3, your configuration would look like this:
    • Command: /usr/bin/python3
    • Arguments: /path/to/script/my_script.py
    • Working Directory: /path/to/script/ (optional)
  • Add Listeners (Optional): You can add listeners such as View Results Tree or Summary Report to view the output of the OS Process Sampler.
  • Run the Test Plan: Execute the test plan by clicking on the green play button at the top of the JMeter interface. The Python script will be executed as part of the test plan.

Happy Testing !!

Token Management in JMeter for Repeated Requests

Apache JMeter is a powerful Java application used for testing the performance and functional behavior of various applications and protocols. One common scenario in testing is the need to generate a token once and then reuse it for multiple requests. Here’s how you can efficiently manage tokens in JMeter:

Step 1: Generate and Extract Token
First, generate the token in one Thread Group and store it using a Json Extractor. Ensure to configure the request with necessary parameters such as host, path, grant-type, client-id, and client-secret. Then, define the JSON Path expressions to extract the token and store it in a variable, such as `access_token`.

Step 2: Store Token
Using a BeanShell Assertion, store the token using the `setProperty()` function. This function facilitates interThreadCommunication, ensuring the token is accessible across different threads. The syntax would be: `${__setProperty(access_token_new, ${access_token})}`.

Step 3: Reuse Token
In subsequent threads, configure requests to utilize the token. This is achieved by using the `property()` function: `${__property(access_token_new)}`.

Important Note:
- Ensure that the number of users/threads for token generation is set to 1, while for other requests, it can be 1 or more.
- Running the test plan will generate the token once, which can then be reused multiple times for other requests.