ionelmc / pytest-benchmark

py.test fixture for benchmarking code
BSD 2-Clause "Simplified" License
1.22k stars 115 forks source link

Context Manager #225

Closed jcampbell05 closed 1 year ago

jcampbell05 commented 1 year ago

It can be quite awkward to wrap functions into a lambda and so a context manager could simplify some code.

The idea being one could do this:

with benchmark:
      # Code I want to benchmark

__enter__ would be used to start the time measurement and __exit__ to finish it

LewisGaul commented 1 year ago

How would benchmark then run multiple iterations of that code in the context manager?

jcampbell05 commented 1 year ago

Thinking about this a little more then it seems the only way you could do this is using a generator function unless the entire test function was called multiple times.

for _ in benchmark():
   print("My benchmarked code goes here")

I'm not sure if there is a way in python of detection calling the function in a generator context vs a standard lib call (perhaps pytest-benchmark can do this by assuming the user wants to use a generator if a lambda isn't passed in as an argument.

But if this isn't ideal then a generator function could be returned from a context manager like so

with benchmark as b:
    for _ in b:
        print("My benchmarked code goes here")
LewisGaul commented 1 year ago

Given the initial goal

It can be quite awkward to wrap functions into a lambda and so a context manager could simplify some code.

your realisation that this would require some magic such as a special generator object makes it seem to me that maybe status quo is preferable.