How to do performance testing with Pytest? 5 easy steps to learn!

Pytest can actually do performance testing or benchmarking. is very convenient. Consider using the Pytest-benchmark class library.

Install pytest-benchmark

First, make sure you have installed pytest and the pytest-benchmark plugin. The plugin can be installed with the following command:

pip install pytest pytest-benchmark

Create the function under test

Create the function under test as follows:

import time, pytest_benchmark
def method1(duration=0.000001):
    time.sleep(duration)
    return "test"

Create the function under test

Create a performance test function: Write a function that needs to be tested or benchmarked, and use the pytest decorator to mark it as a performance test function. For example:

import pytest, pytest_benchmark

def test_my_function(benchmark):
    result = benchmark(method1, argument1, argument2)
    assert result == expected_result

In the above example, test_my_function is the performance test function and benchmark is a decorator provided by the pytest-benchmark plugin. method1 is the function to be tested for performance, argument1 and argument2 are the parameters of the function, and these parameters can be adjusted according to actual needs.

run test

pytest

Test Results

After running a performance test with pytest-benchmark, the test results will be displayed in the console. Here's what each column in the result means:

1. name: The name of the test function. Each test function has its own name, which is used to distinguish different tests.

2. min: The minimum time to execute the test function. This is the shortest time observed across all runs.

3. max: The maximum time to execute the test function. This is the longest time observed across all runs.

4. mean: The average time to execute the test function. This is the average of all running times.

5. stddev: The standard deviation of the execution test function. The standard deviation is a measure of how spread out the running times are. Smaller standard deviations indicate more stable runtimes.

6. median: The median time to execute the test function. The median is the value in the middle of all running times sorted by size.

7. IQR: InterQuartile Range. This is a different way of measuring variance.

8. outliers: The number of outliers observed while executing the test function. An outlier is a value that deviates significantly from other observations.

9. OPS: Indicates how many times the test method is called within a certain period of time (such as 1 second).

10. rounds: The number of times to execute the test function. By default, pytest-benchmark will run the test function multiple times, and calculate the average time and other statistical data based on these running results.

11. iterations: The number of iterations to execute the test function in each run. pytest-benchmark executes the test function multiple iterations per run and calculates the time for each iteration.

When analyzing the results, you can compare the results of different functions and consider whether the performance requirements are met. Optimizations and improvements can be made based on the test results and the tests re-run to verify the effects of the improvements.

Finally: In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free【保证100%免费】

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

Guess you like

Origin blog.csdn.net/IT_LanTian/article/details/131576917