pytest-benchmark icon indicating copy to clipboard operation
pytest-benchmark copied to clipboard

Fail if benchmark slower than threshold?

Open adamalpi opened this issue 3 years ago • 5 comments

Is there a way to fail a test if average goes beyond some threshold?

It would be nice to have something like the code below. So it would fail if average time takes more the 0.5 seconds.

@pytest.mark.benchmark(
    max_time_fail=0.5
)
def test_my_stuff(benchmark):
    # benchmark something
    result = benchmark(something)
    assert result == 123

adamalpi avatar Aug 18 '20 12:08 adamalpi

You could add something like:


   assert benchmark.stats['min'] < something

ionelmc avatar Aug 19 '20 09:08 ionelmc

Hi there, yes, this seems flexible enough. I achieved it via a similar one:

assert benchmark.stats.stats.mean < threshold

Thanks. We can close the issue.

adamalpi avatar Aug 24 '20 06:08 adamalpi

This should be reopened unless is added to the docs example. I was surprised not be able to find a documentation example that would make a test fail if it did run too slow or too fast, this being the main goal when I started looking for a benchmarking solution.

ssbarnea avatar Apr 13 '21 08:04 ssbarnea

Dully noted. This is actually a bigger thing, I'd need to document and explain all the stuff going on in that Benchmark object.

ionelmc avatar Apr 17 '21 15:04 ionelmc

I was also looking for this in the docs.

MarcSkovMadsen avatar Dec 29 '22 05:12 MarcSkovMadsen