Take the following code, which uses a Fixture
Since, the time to run the test is small, the framework decides to run the same test point multiple times to get a stable time. However, while trying to do so, it does not execute the setup/teardown code.
While the issue can be easily worked around, this kind of behavior violates the x-unit pattern fixture setup/teardown workflow.
The idea of the test is to time how long it takes to insert a set of strings to an EMPTY unordered_map.
class MySUTClass
{
public:
void insert (std::string key, int val)
{
auto search = myMap.find(key);
if(search != myMap.end()) {
std::cout << "tried to insert duplicate key in the same map " << std::endl;
std::abort();
} else {
myMap.insert({key, val});
}
}
void clear()
{
myMap.clear();
}
private:
std::unordered_map<std::string, int> myMap;
};
class MyFixtureTest : public ::benchmark::Fixture
{
public:
MyFixtureTest()
{
std::cout << "ran ctor" << std::endl;
}
void SetUp(const ::benchmark::State& state)
{
std::cout << "ran SetUp" << std::endl;
}
~MyFixtureTest(){}
void TearDown(const ::benchmark::State& state)
{
m_Table.clear();
std::cout << "ran teardown" << std::endl;
}
public:
MySUTClass m_Table;
};
BENCHMARK_DEFINE_F(MyFixtureTest, TableInsertion)(benchmark::State& st) {
std::cout << "outside the while loop\n";
while(st.KeepRunning()){
st.PauseTiming();
std::cout << "inside the while loop" << std::endl;
std::set<std::string> stringSet;
stringSet.insert("abc");
stringSet.insert("def");
stringSet.insert("hfash");
st.ResumeTiming();
for (auto const & id: stringSet) {
m_Table.insert(id.c_str(), 1); // contrived example, all keys have same value;
}
}
}
BENCHMARK_REGISTER_F(MyFixtureTest,TableInsertion);`
Benchmark Time CPU Iterations
ran SetUp
outside the while loop
inside the while loop
ran teardown
ran SetUp
outside the while loop
inside the while loop
inside the while loop
tried to insert duplicate key in the same map
The right sequence should be:
Benchmark Time CPU Iterations
ran SetUp
outside the while loop
inside the while loop
ran teardown
ran SetUp
outside the while loop
inside the while loop
ran teardown
...
Take the following code, which uses a Fixture
Since, the time to run the test is small, the framework decides to run the same test point multiple times to get a stable time. However, while trying to do so, it does not execute the setup/teardown code.
While the issue can be easily worked around, this kind of behavior violates the x-unit pattern fixture setup/teardown workflow.
The idea of the test is to time how long it takes to insert a set of strings to an EMPTY unordered_map.
Benchmark Time CPU Iterations
ran SetUp
outside the while loop
inside the while loop
ran teardown
ran SetUp
outside the while loop
inside the while loop
inside the while loop
tried to insert duplicate key in the same map
The right sequence should be:
Benchmark Time CPU Iterations
ran SetUp
outside the while loop
inside the while loop
ran teardown
ran SetUp
outside the while loop
inside the while loop
ran teardown
...