Get startedGet started for free

Performance testing with pytest

1. Performance testing with pytest

Now, we will be running performance tests with the "pytest-benchmark" fixture. Let's start by discussing what performance testing is.

2. What is performance testing

Performance measures how efficiently software utilizes the system's resources to accomplish some task. This means that performance is a number (or set of numbers) that describes the efficiency of some software. Performance Testing is a type of testing that measures software performance. In other words, if the performance is a number, then performance testing is the process of actually measuring this number. But why and when should we use it?

3. When performance testing is important

We have to optimize the performance because we are usually constrained by the resources we use. Sometimes, it is critical that the application run for not more than a certain amount of time. For example, if we are building a website and we want the pages to load as fast as possible. Sometimes, we want to make the optimal algorithm to use for a larger amount of data. For example, that can be the case if we are creating an app that has to process millions of requests per second. And sometimes, we want to optimize RAM usage so the software can be launched on some small device in terms of resources, like a path planning algorithm executing on a robot vacuum. OK. Now, let's dive into the code.

4. Benchmark fixture

There is an extra pytest fixture called "pytest-benchmark" for performance testing. We can install it with the following command. This package will add the benchmark function to our pytest module fixtures. Now, let's try to conduct a performance test for the "time-dot-sleep function" function. To use the benchmark fixture, we have to pass the benchmark function inside our test function. Then, call the "benchmark" function and pass the function we want to measure and its arguments. In this example, that means calling "benchmark" with passing "time-dot-sleep function" function with an argument "one". Note that we can pass positional and keyword arguments to the function we want to test through the benchmark function. Finally, we can run the script from CLI and get the testing results.

5. Benchmarking results

We will see something like this several seconds after we have launched the benchmarking with the pytest CLI command. That is our benchmarking results. What does it tell us? So, the benchmark fixture runs the test function several times and saves each execution in seconds. That means that the benchmark got a sample of numbers at some point, and the results are just a statistical description of this sample. Here, for example, the minimum, the maximum, and the mean are about 1 second. It makes sense since we created a function that was sleeping for exactly 1 second. We can also see that the statistics will change if we set the time sleep to 3 instead of 1. Now, let's summarize everything.

6. Benchmark decorator

We can also use a benchmark function as a decorator for content inside the test function. This script is similar to the previous example. But here, we use the decorator syntax to wrap the "time sleep" function. This syntax lets you to create a custom function for benchmarking, right inside the test function.

7. Summary

Today, we discussed what performance testing is, why and when it is important to use performance testing, how to implement performance testing using the pytest-benchmark fixture, and how to read the obtained performance results.

8. Let's practice!

Now try to play with performance testing by yourself. Notice the results you get depending on the scripts you run. And see you in the next video!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.