pytest के साथ परफॉर्मेंस टेस्टिंग

Python में Testing का परिचय

Alexander Levin

Data Scientist

Performance testing क्या है

Performance - सॉफ्टवेयर किसी कार्य को करने के लिए सिस्टम के संसाधनों का कितना कुशल उपयोग करता है.

Performance Testing - सॉफ्टवेयर की परफॉर्मेंस को मापने वाली टेस्टिंग.

Python में Testing का परिचय

जब performance testing महत्वपूर्ण है

Resources:

  • Execution Time
  • CPU
  • RAM
  • अन्य संसाधन

उपयोग के मामले:

  • वेबसाइट स्पीड ऑप्टिमाइज़ेशन
  • ऐप को लाखों रिक्वेस्ट मिलना
  • रोबोट वैक्यूम के लिए पाथ प्लानिंग
Python में Testing का परिचय

Benchmark फ़िक्स्चर

इंस्टॉलेशन:

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

CLI कमांड:

pytest Example_1.py
Python में Testing का परिचय

बेंचमार्किंग परिणाम

CLI कमांड चलाने के बाद जो परिणाम मिलते हैं: pytest कमांड चलाने के बाद बेंचमार्किंग परिणाम

time.sleep(1) की जगह time.sleep(3) होने पर: प्रोग्राम में 1 की जगह 3 सेकंड के time-dot sleep पर pytest कमांड के बाद बेंचमार्किंग परिणाम

Python में Testing का परिचय

Benchmark डेकोरेटर

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

CLI कमांड:

pytest Example_2.py
Python में Testing का परिचय

सारांश

  • Performance testing - सॉफ्टवेयर की परफॉर्मेंस मापने वाली टेस्टिंग.
  • संसाधन आमतौर पर सीमित होते हैं.
  • संसाधन सीमित हों तो यह मददगार है.
  • हम pytest-benchmark फ़िक्स्चर का उपयोग कर सकते हैं:
    • benchmark को सीधे कॉल करके
    • @benchmark को डेकोरेटर की तरह लगाकर
  • परिणाम सेकंड में मापी गई रन की सैंपल को बताते हैं.
Python में Testing का परिचय

अभ्यास करते हैं!

Python में Testing का परिचय

Preparing Video For Download...