Given the following code
#include <benchmark/benchmark.h>
#include <iostream>
static void BM_foo(benchmark::State& state) {
std::cout << "Foo "<< std::endl;
for (auto _: state) {
std::cout << state.iterations() << " In loop " <<std::endl;
}
}
BENCHMARK(BM_foo);
BENCHMARK_MAIN();
I thought the std::cout << "Foo "<< std::endl; will be executed only once, but during my test, it runs 7 times.
So my questions are:
- How can I run some setup code only once before the benchmark, like "std::cout << "Foo "<< std::endl"?
- when I use "state.iterations()", I want to get "1, 2, 3 .." one by one, but i aways get 0. how can I get the seq number of the iterators?
In my real case, I want to run some storage engine related code in multi threads scenario. the steps like this:
- open connection to the storage engine. let's assume the connection object CAN be shared among different threads.
- open session based on connection. let's assume the session object CAN NOT be shared among threads.
- run many db_related ops, which is the only code I want to benchmark.
- close session, connection.
I have the following pseudo code, but I thought it 2 issues at least.
DBConnection* conn; // global variables, so that each thread can run access it.
static void BM_db_insert(benchmark::State& state) {
if (state.thread_index == 0) {
# only let 1 thread to setup the DBConnections
conn = openDBConn();
}
DBSession* session = conn.openSession();
for (auto _ : state) {
session.insertOp(id, ..);
}
session.close();
if (state.thread_index == 0) {
conn.close();
}
}
Issues:
- Shall I have to use global variables DBConnection here so that it can be accessed by each thread?
- Suppose "DBSession* session = conn.openSession();" can be executed before the "conn is really set up", and I also don't want to benchmark openSession every time, how shall I fix this issue?
So totally I have 4 questions in 2 parts, and if you have more suggestion of my code, that will be much better. Thanks!