question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is is possible to "parametrize" a benchmark?

See original GitHub issue

I want to benchmark different JSON engines serialization/deserialization functions, with different sets of data. More specifically, I’m trying to convert an already existing set of benchmarks to pytest-benchmark.

Here the contenders is a list of tuples (name, serialization_func, deserialization_func):

@pytest.mark.benchmark(group='serialize default_data')
@pytest.mark.parametrize('serializer',
                         [c[1] for c in contenders],
                         ids=[c[0] for c in contenders])
def test_serializer_benchmark(serializer, benchmark):
    benchmark(serializer, default_data)

@pytest.mark.benchmark(group='deserialize default_data')
@pytest.mark.parametrize('serializer,deserializer',
                         [(c[1], c[2]) for c in contenders],
                         ids=[c[0] for c in contenders])
def test_deserialization_benchmark(serializer, deserializer, benchmark):
    data = serializer(default_data)
    benchmark(deserializer, data)

This will produce two distinct benchmarks tables, one for the serialization function and one for its counterpart. I can go down the boring way of repeating that pattern for each dataset…

What I’d like to achieve is to factorize that to something like the following (that does not work):

@pytest.mark.parametrize('name,data', [('default data', default_data)])
def test_gen(name, data):
    @pytest.mark.benchmark(group=name + ': serialize')
    @pytest.mark.parametrize('serializer',
                             [c[1] for c in contenders],
                             ids=[c[0] for c in contenders])
    def serializer_benchmark(serializer, benchmark):
        benchmark(serializer, data)

    @pytest.mark.benchmark(group=name + ': deserialize')
    @pytest.mark.parametrize('serializer,deserializer',
                             [(c[1], c[2]) for c in contenders],
                             ids=[c[0] for c in contenders])
    def deserializer_benchmark(serializer, deserializer, benchmark):
        serialized_data = serializer(data)
        benchmark(deserializer, serialized_data)

    yield serializer_benchmark
    yield deserializer_benchmark

That way I could reuse the very same code to create benchmarks against all other sets of data, without repeating the code, simply adding them to the initial parametrize:

@pytest.mark.parametrize('name,data', [('default data', default_data),
                                       ('array 256 doubles', doubles),
                                       ('array 256 unicode', unicode_strings),
                                      ])
def test_gen(name, data):
...

Is there any trick I’m missing?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
ionelmccommented, Apr 9, 2016

It didn’t initially occur to me but you can also do this:

@pytest.mark_parametrize('foo', [1,2,3])
def test_perf(benchmark, foo):
   benchmark.group = '%s - perf' % foo
   benchmark(....)
2reactions
ionelmccommented, Apr 9, 2016

Also, you may set the group from a fixture to reduce boilerplate, eg:

@pytest.fixture(params=[1,2,3])
def foo(benchmark, request):
   benchmark.group = '%s - perf' % request.param
   return request.param

def test_perf(benchmark, foo):
   benchmark(....)

The only constraint is that the fixture needs to be function scoped (as benchmark fixture is).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Grouping Parametrized Benchmarks with pytest - Stack Overflow
I'm able to group by case , but not by (case,n) with this method. I added a @benchmark_this decorator above each test case...
Read more >
Task Bench: A Parameterized Benchmark for Evaluating ...
We present Task Bench, a parameterized benchmark designed to explore the performance of distributed programming systems under a variety of application ...
Read more >
Parameterization | BenchmarkDotNet
As an alternative to using [Params] , you can specify arguments for your benchmarks. There are several ways to do it (described below)....
Read more >
A Dataset and Benchmark for Mesh Parameterization - DeepAI
There is no single metric capturing parameterization quality in practice, since the quality of a parameterization heavily depends on its ...
Read more >
A Parameterized Benchmark for Evaluating Parallel Runtime ...
By explicitly modeling the task graph (with tasks as vertices and dependencies as edges), we make it possible to explore a wide variety...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found