Performance testen met pytest

Introductie tot testen in Python

Alexander Levin

Data Scientist

Wat is performance testing

Performance: hoe efficiënt software systeemresources gebruikt om een taak te voltooien.

Performance testing: een testtype dat softwareprestatie meet.

Introductie tot testen in Python

Wanneer performance testing belangrijk is

Resources:

  • Uitvoertijd
  • CPU
  • RAM
  • Overige resources

Use-cases:

  • Website sneller maken
  • App met miljoenen requests
  • Padplanning voor een robotstofzuiger
Introductie tot testen in Python

Benchmark-fixture

Installatie:

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

CLI-opdracht:

pytest Example_1.py
Introductie tot testen in Python

Benchmarkresultaten

De resultaten na het uitvoeren van de CLI-opdracht: Benchmarkresultaten na uitvoeren van pytest-opdracht

Voor time.sleep(3) in plaats van time.sleep(1): Benchmarkresultaten na uitvoeren van pytest voor het programma met time-dot sleep van 3 in plaats van 1

Introductie tot testen in Python

Benchmark-decorator

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

CLI-opdracht:

pytest Example_2.py
Introductie tot testen in Python

Samenvatting

  • Performance testing: test die de prestatie van software meet.
  • Resources zijn meestal beperkt.
  • Handig bij beperkte resources.
  • Gebruik de pytest-benchmark fixture door:
    • benchmark direct aan te roepen
    • @benchmark als decorator te gebruiken
  • Resultaten beschrijven de sample van metingen in seconden.
Introductie tot testen in Python

Laten we oefenen!

Introductie tot testen in Python

Preparing Video For Download...