Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Advanced Python Programming

You're reading from   Advanced Python Programming Accelerate your Python programs using proven techniques and design patterns

Arrow left icon
Product type Paperback
Published in Mar 2022
Publisher Packt
ISBN-13 9781801814010
Length 606 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Quan Nguyen Quan Nguyen
Author Profile Icon Quan Nguyen
Quan Nguyen
Arrow right icon
View More author details
Toc

Table of Contents (32) Chapters Close

Preface 1. Section 1: Python-Native and Specialized Optimization
2. Chapter 1: Benchmarking and Profiling FREE CHAPTER 3. Chapter 2: Pure Python Optimizations 4. Chapter 3: Fast Array Operations with NumPy, Pandas, and Xarray 5. Chapter 4: C Performance with Cython 6. Chapter 5: Exploring Compilers 7. Chapter 6: Automatic Differentiation and Accelerated Linear Algebra for Machine Learning 8. Section 2: Concurrency and Parallelism
9. Chapter 7: Implementing Concurrency 10. Chapter 8: Parallel Processing 11. Chapter 9: Concurrent Web Requests 12. Chapter 10: Concurrent Image Processing 13. Chapter 11: Building Communication Channels with asyncio 14. Chapter 12: Deadlocks 15. Chapter 13: Starvation 16. Chapter 14: Race Conditions 17. Chapter 15: The Global Interpreter Lock 18. Section 3: Design Patterns in Python
19. Chapter 16: The Factory Pattern 20. Chapter 17: The Builder Pattern 21. Chapter 18: Other Creational Patterns 22. Chapter 19: The Adapter Pattern 23. Chapter 20: The Decorator Pattern 24. Chapter 21: The Bridge Pattern 25. Chapter 22: The Façade Pattern 26. Chapter 23: Other Structural Patterns 27. Chapter 24: The Chain of Responsibility Pattern 28. Chapter 25: The Command Pattern 29. Chapter 26: The Observer Pattern 30. Assessments 31. Other Books You May Enjoy

Writing better tests and benchmarks with pytest-benchmark

The Unix time command is a versatile tool that can be used to assess the running time of small programs on a variety of platforms. For larger Python applications and libraries, a more comprehensive solution that deals with both testing and benchmarking is pytest, in combination with its pytest-benchmark plugin.

In this section, we will write a simple benchmark for our application using the pytest testing framework. For those who are, the pytest documentation, which can be found at http://doc.pytest.org/en/latest/, is the best resource to learn more about the framework and its uses.

You can install pytest from the console using the pip install pytest command. The benchmarking plugin can be installed, similarly, by issuing the pip install pytest-benchmark command.

A testing framework is a set of tools that simplifies writing, executing, and debugging tests, and provides rich reports and summaries of the test results. When using the pytest framework, it is recommended to place tests separately from the application code. In the following example, we create a test_simul.py file that contains the test_evolve function:

    from simul import Particle, ParticleSimulator
    def test_evolve():
        particles = [
                     Particle( 0.3,  0.5, +1),
                     Particle( 0.0, -0.5, -1),
                     Particle(-0.1, -0.4, +3)
          ]
        simulator = ParticleSimulator(particles)
        simulator.evolve(0.1)
    
        p0, p1, p2 = particles
        def fequal(a, b, eps=1e-5):
            return abs(a - b) < eps
        assert fequal(p0.x, 0.210269)
        assert fequal(p0.y, 0.543863)
        assert fequal(p1.x, -0.099334)
        assert fequal(p1.y, -0.490034)
        assert fequal(p2.x,  0.191358)
        assert fequal(p2.y, -0.365227)

The pytest executable can be used from the command line to discover and run tests contained in Python modules. To execute a specific test, we can use the pytest path/to/module.py::function_name syntax. To execute test_evolve, we can type the following command in a console to obtain simple but informative output:

$ pytest test_simul.py::test_evolve
platform linux -- Python 3.5.2, pytest-3.0.5, py-1.4.32, 
pluggy-0.4.0
rootdir: /home/gabriele/workspace/hiperf/chapter1, inifile: 
plugins:
collected 2 items 
test_simul.py .
=========================== 1 passed in 0.43 seconds ===========================

Once we have a test in place, it is possible for you to execute your test as a benchmark using the pytest-benchmark plugin. If we change our test function so that it accepts an argument named benchmark, the pytest framework will automatically pass the benchmark resource as an argument (in pytest terminology, these resources are called fixtures). The benchmark resource can be called by passing the function that we intend to benchmark as the first argument, followed by the additional arguments. In the following snippet, we illustrate the edits necessary to benchmark the ParticleSimulator.evolve function:

    from simul import Particle, ParticleSimulator
    def test_evolve(benchmark):
        # ... previous code
        benchmark(simulator.evolve, 0.1)

To run the benchmark, it is sufficient to rerun the pytest test_simul.py::test_evolve command. The resulting output will contain detailed timing information regarding the test_evolve function, as shown here:

Figure 1.4 – Output from pytest

Figure 1.4 – Output from pytest

For each test collected, pytest-benchmark will execute the benchmark function several times and provide a statistic summary of its running time. The preceding output shown is very interesting as it shows how running times vary between runs.

In this example, the benchmark in test_evolve was run 34 times (Rounds column), its timings ranged between 29 and 41 milliseconds (ms) (Min and Max), and the Average and Median times were fairly similar at about 30 ms, which is actually very close to the best timing obtained. This example demonstrates how there can be substantial performance variability between runs and that, as opposed to taking timings with one-shot tools such as time, it is a good idea to run the program multiple times and record a representative value, such as the minimum or the median.

pytest-benchmark has many more features and options that can be used to take accurate timings and analyze the results. For more information, consult the documentation at http://pytest-benchmark.readthedocs.io/en/stable/usage.html.

You have been reading a chapter from
Advanced Python Programming - Second Edition
Published in: Mar 2022
Publisher: Packt
ISBN-13: 9781801814010
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image