Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google benchmark custom main

I would like to have a custom main function called before the benchmark starts to run with Google's benchmark library. So that I could setup several things. I've searched for quite a bit but I wasn't able to find anything. Should I simply modify the macro manually? Or simply use my main function and initialize the benchmark myself. Would that affect the library initialization in any way? Is there another way without requiring me to modify that macro or copying it's contents?

benchmark\benchmark_api.h

// Helper macro to create a main routine in a test that runs the benchmarks
#define BENCHMARK_MAIN()                   \
  int main(int argc, char** argv) {        \
    ::benchmark::Initialize(&argc, argv);  \
    ::benchmark::RunSpecifiedBenchmarks(); \
  }
like image 289
SLC Avatar asked Dec 21 '15 18:12

SLC


1 Answers

BENCHMARK_MAIN() is just a helper macro, so you should be able to define your own version of main() like this:

int main(int argc, char** argv)
{
   your_custom_init();
   ::benchmark::Initialize(&argc, argv);
   ::benchmark::RunSpecifiedBenchmarks();
}

Edit: you can also define global object and perform your custom initialization within its constructor. I usually do it this way, e.g. to initialize global array with input data:

int data[10];

class MyInit
{
public:
    MyInit()
    {
        for (int n = 0; n < 10; ++n)
            data[n] = n;
    }
};

MyInit my_init;
like image 85
Daniel Frużyński Avatar answered Sep 21 '22 11:09

Daniel Frużyński