1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#ifndef WIBBLE_BENCHMARK_H
#define WIBBLE_BENCHMARK_H
#include <time.h>
#include <vector>
#include <string>
class Timer
{
protected:
clock_t start;
std::string desc;
public:
Timer(const Timer& t) : start(t.start), desc(t.desc)
{
const_cast<Timer*>(&t)->start = -1;
}
Timer(const std::string& desc) : start(clock()), desc(desc) {}
~Timer();
const Timer& operator=(const Timer& t)
{
start = t.start;
desc = t.desc;
const_cast<Timer*>(&t)->start = -1;
return *this;
}
void done();
};
class Benchmark
{
private:
std::string tag;
Benchmark* parent;
std::vector<Benchmark*> children;
protected:
// Main function with the benchmarks
virtual void main() {}
void setParent(Benchmark* parent) { this->parent = parent; }
Timer mktimer(const std::string& desc)
{
return Timer(fullName() + "/" + desc);
}
std::string name() const { return tag; }
std::string fullName() const
{
if (parent)
return parent->fullName() + "/" + tag;
else
return tag;
}
public:
Benchmark(const std::string& tag) : tag(tag), parent(0) {}
void addChild(Benchmark* child)
{
children.push_back(child);
child->setParent(this);
}
virtual ~Benchmark()
{
for (std::vector<Benchmark*>::iterator i = children.begin();
i != children.end(); i++)
delete *i;
}
// Run only the subtest at the given path
void run(const std::string& path);
// Run all subtests and this test
void run();
// Return the singleton instance of the toplevel benchmark class
static Benchmark* root();
};
struct RegisterRoot
{
RegisterRoot(Benchmark* b)
{
Benchmark::root()->addChild(b);
}
};
// vim:set ts=4 sw=4:
#endif
|