1

I want to modify a templated google benchmark (with custom arguments) to run with a test fixture class, but don't know if this is really possible and if so, how the correct syntax is.

Just adding the fixture class doesn't seem to be enough.

static void CustomArguments(benchmark::internal::Benchmark* b) {
    // define I, J
    for (auto i : I)
    {
        for (auto j : J)
            b->Args({i, j});
    }
}

template<typename my_type>
class My_Fixture : public benchmark::Fixture
{
protected:
    void SetUp(const ::benchmark::State& state) { ...}

    virtual void TearDown() { ...}
};

template <typename any_type>
static void insert(benchmark::State& state)
{
    for (auto _ : state)
    { ...}
}

BENCHMARK_TEMPLATE_F(My_Fixture, insert, my_type)->Apply(CustomArguments);

BENCHMARK_MAIN();

The compiler complains about the before-last code line error: expected initializer before '->' token, but also fails to find the variable declarations from the fixture inside the test. This might be a consequence or an additional error. Do I need to register the fixture additionally? The example is running perfectly without fixture.

1 Answers1

1

You'll need to use a slightly less convenient registration macro:

BENCHMARK_TEMPLATE_DEFINE_F defines the templated benchmark with the fixture. Then BENCHMARK_REGISTER_F can be used to register the benchmark, and that is where you'd add the ->Apply(CustomArguments) call.

See here for an example.

dma
  • 1,758
  • 10
  • 25
  • Moving the `->Apply(CustomArguments)` is one issue, but is there a way to have a function call when using the `BENCHMARK_TEMPLATE_DEFINE_F` macro? All the examples directly define the test corpus after this macro. So, I could make it run, but had to move my `insert` function into the `BENCHMARK_TEMPLATE_DEFINE_F` block. The advantage of the one-liner was, that I could just duplicate it, to make a call with another `my_type` (a struct). How could I elegantly use it with multiple `my_types`? – Marie Hoffmann May 07 '19 at 14:00
  • 1
    Sorry, I missed that. This is a limitation of the fixture feature which requires the definition to be inline. However, you may not need the fixture class if you're using templates. You can do setup and teardown around the `for` loop. Eg: https://godbolt.org/z/Ybhv0o – dma May 08 '19 at 19:12
  • This was actually my initial solution. But thanks for confirming that this is the way to go in my case! – Marie Hoffmann Jun 03 '19 at 12:23