Performance testing with pytest

Introduction to Testing in Python

Alexander Levin

Data Scientist

What is performance testing

Performance - how efficiently does software utilizes the resources of the system to accomplish a task.

Performance Testing - is a type of testing that measures software performance.

Introduction to Testing in Python

When performance testing is important

Resources:

  • Execution Time
  • CPU
  • RAM
  • Other resources

Cases:

  • Website speed optimization
  • App receiving millions of requests
  • Path planning for a robot vacuum
Introduction to Testing in Python

Benchmark fixture

Installation:

pip install pytest-benchmark
# Example_1.py
import time
def test_func(benchmark):
    benchmark(time.sleep, 1)

CLI Command:

pytest Example_1.py
Introduction to Testing in Python

Benchmarking results

The results we get after we execute the CLI Command: Benchmarking results after executing pytest command

For time.sleep(3) instead of time.sleep(1): Benchmarking results after executing pytest command for the program with time-dot sleep of 3 instead of 1

Introduction to Testing in Python

Benchmark decorator

# Example_2.py
import time
def test_func(benchmark):
    @benchmark
    def sleep_for_1_sec():
        time.sleep(1)

CLI Command:

pytest Example_2.py
Introduction to Testing in Python

Summary

  • Performance testing - a type of testing that measures the software performance.
  • Resources are usually finite.
  • Helpful when resources are constrained.
  • We can use pytest-benchmark fixture by:
    • calling benchmark directly
    • using @benchmark as a decorator
  • The results describe the sample of measured runs in seconds.
Introduction to Testing in Python

Let's practice!

Introduction to Testing in Python

Preparing Video For Download...