Bench Manager

class bench_manager.BenchManager(set_log_info=False)[source]

Manage benchmarks by timing function against different contexts.

General concept

A BenchManager is instantiated in order to collect functions to benchmark, like so:

>>> my_bench_manager = BenchManager()

In order to add functions to bench, one flag them for bench by using the bench() decorator. For example:

>>> @my_bench_manager.bench
... def add_one(n):
...     return n + 1

Each flagged function is then called against contexts. A context is a function with optional setup and teardown, and it must yield the parameter that benchmarked functions need:

>>> @my_bench_manager.context
... def three():
...     # optional setup
...     try:
...        # yield the parameter
...        yield 3
...     finally:
...        # optional teardown
...        pass

Finally, to perform the benchmarks, one must call:

>>> my_bench_manager.run('/tmp/my_results.csv')

The result of the two examples above is to time add_one(3).

Technical details

Each context is stored in the list _contexts. Each function to benchmark is stored in the list _bench_funcs.

When run() is called, it will iterate over functions and contexts to call each function against each context.

Variables:
  • _contexts (list) – contexts to apply
  • _bench_funcs (list) – functions to benchmark
  • _results (dict) – collected benchmark results
  • _logger – global logger
bench(func)[source]

Prepare a function to be benched and add it to the list to be run later.

Parameters:func (function) – the function to bench
context(func)[source]

Decorate a function to act as a context.

Parameters:func (function) – the function that describes the context
run(output_filename)[source]

Benchmark functions against contexts.

Parameters:output_filename (str) – filename of the CSV output
write_output(output_filename)[source]

Write results of the BenchManager to a nicely formatted CSV file.

Parameters:output_filename (str) – filename of the CSV output