Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python benchmark tool like nosetests?

What I want

I would like to create a set of benchmarks for my Python project. I would like to see the performance of these benchmarks change as I introduce new code. I would like to do this in the same way that I test Python, by running the utility command like nosetests and getting a nicely formatted readout.

What I like about nosetests

The nosetests tool works by searching through my directory structure for any functions named test_foo.py and runs all functions test_bar() contained within. It runs all of those functions and prints out whether or not they raised an exception.

I'd like something similar that searched for all files bench_foo.py and ran all contained functions bench_bar() and reported their runtimes.

Questions

Does such a tool exist?

If not what are some good starting points? Is some of the nose source appropriate for this?

like image 240
MRocklin Avatar asked Oct 06 '13 14:10

MRocklin


People also ask

Can JUnit be used for Python?

The Python unit testing framework, dubbed 'PyUnit' by convention, is a Python language version of JUnit, by smart cookies Kent Beck and Erich Gamma. JUnit is, in turn, a Java version of Kent's Smalltalk testing framework. Each is the de facto standard unit testing framework for its respective language.

What is Nosetest Python?

Nose is a popular test automation framework in Python that extends unittest to make testing easier. The other advantages of using the Nose framework are the enablement of auto discovery of test cases and documentation collection.

What is benchmarking in Python?

In Python, we have a by default module for benchmarking which is called timeit. With the help of the timeit module, we can measure the performance of small bit of Python code within our main program.


1 Answers

nosetests can run any type of test, so you can decide if they test functionality, input/output validity etc., or performance or profiling (or anything else you'd like). The Python Profiler is a great tool, and it comes with your Python installation.

import unittest
import cProfile

class ProfileTest(unittest.TestCase):
    test_run_profiler:
        cProfile.run('foo(bar)')
        cProfile.run('baz(bar)')

You just add a line to the test, or add a test to the test case for all the calls you want to profile, and your main source is not polluted with test code.

If you only want to time execution and not all the profiling information, timeit is another useful tool.

like image 59
Henrik Avatar answered Nov 15 '22 18:11

Henrik