JMeter+Python implements asynchronous interface testing

When using JMeter and Python to implement asynchronous interface testing, you can follow the following steps:

1. Install JMeter and Java Development Kit (JDK):

Download and install JMeter (https://jmeter.apache.org/download_jmeter.cgi) and the JDK for your operating system .

2. Write JMX test plan file:

Create and configure test plans using JMeter GUI. Open JMeter and click "File" > "Templates" > "Building a Web Test Plan" > "Create" to create a new test plan. Then follow the steps below to configure:

picture

Thread Group: Set the number of threads, number of concurrent users, number of loops, etc.
Async HTTP Request: Set URL, request method, parameters, etc.
Assertion: Set assertion rules to verify whether the results returned by the interface meet expectations.
View Results Tree: used to view interface responses and results.

3. Save the JMX file:

Click "File" > "Save Test Plan As" to save the test plan as a JMX file.

4. Write Python script:

Use Python scripts to execute JMeter test plans. Here is a sample code:

import subprocess 

jmeter_path = "/path/to/jmeter" # 替换为你的JMeter路径 
jmeter_test_plan = "/path/to/your_test_plan.jmx" # 替换为你的测试计划文件路径 

def run_jmeter_test_plan(): 
    cmd = [jmeter_path, "-n", "-t", jmeter_test_plan, "-l", "/path/to/result.jtl"] 
    try: 
        subprocess.check_call(cmd) 
        print("JMeter test plan executed successfully.") 
    except subprocess.CalledProcessError as e: 
        print("JMeter test plan execution failed:", e) 
        
run_jmeter_test_plan()

In the Python script, the jmeter_path and jmeter_test_plan variables need to be replaced with the actual path and file name. The -l option is used to specify the output path of the result file.

5. Run the Python script:

Run the above Python script and it will call the JMeter command line to execute the test plan and save the results to the specified JTL file.

6. Analyze the test results:

Use Python to parse the JMeter test result file and obtain the corresponding response time, error information, etc. You can use Python's pandas library or other related libraries to process test result files in CSV or JTL format.

import pandas as pd

def parse_results():
    results = pd.read_csv(result_file, delimiter=",", skiprows=1)

    # 获取响应时间信息
    response_times = results['Latency'].tolist()

    # 获取错误信息
    errors = results.loc[results['Success'] == False, 'ResponseMessage'].tolist()

    # 输出结果
    print("Response Times:", response_times)
    print("Errors:", errors)

parse_results()

In this example, the pandas library is used to read the CSV file and skip the header row of the first row (skiprows=1). Then, get the response time and error information by accessing the corresponding columns. Additional information can be parsed and processed based on actual needs and the structure of the result file. The pandas library provides rich data processing capabilities, and you can use its flexible methods and functions to process JMeter test results.

The above only provides a basic framework, and the actual implementation may need to be adjusted according to specific needs and test plans. Additional configuration may be required, such as adding assertions, setting timers, etc. to simulate asynchronous requests.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!

Guess you like

Origin blog.csdn.net/NHB456789/article/details/133096369