aboutsummaryrefslogtreecommitdiffstats
path: root/checks
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-08-31 02:42:44 +0000
committerlloyd <[email protected]>2008-08-31 02:42:44 +0000
commit312d91067111c3de445c2ca9d31e2eb6fed3c332 (patch)
tree97b8ce89867a662ae2de6e3dd32a2859bc25c81f /checks
parentdc2752448ddef6e96ad81bbf585fcaf971eeaf5f (diff)
Add a simple timer class
Diffstat (limited to 'checks')
-rw-r--r--checks/timer.cpp88
-rw-r--r--checks/timer.h50
2 files changed, 138 insertions, 0 deletions
diff --git a/checks/timer.cpp b/checks/timer.cpp
new file mode 100644
index 000000000..8b7a6d9be
--- /dev/null
+++ b/checks/timer.cpp
@@ -0,0 +1,88 @@
+#include "timer.h"
+#include <time.h>
+#include <iomanip>
+
+u64bit Timer::get_clock()
+ {
+ struct timespec tv;
+ clock_gettime(CLOCK_REALTIME, &tv);
+ return (tv.tv_sec * 1000000000ULL + tv.tv_nsec);
+ }
+
+Timer::Timer(const std::string& n) : name(n)
+ {
+ time_used = 0;
+ timer_start = 0;
+ event_count = 0;
+ }
+
+void Timer::start()
+ {
+ stop();
+ timer_start = get_clock();
+ }
+
+void Timer::stop()
+ {
+ if(timer_start)
+ {
+ u64bit now = get_clock();
+
+ if(now > timer_start)
+ time_used += (now - timer_start);
+
+ timer_start = 0;
+ ++event_count;
+ }
+ }
+
+u64bit Timer::value()
+ {
+ stop();
+ return time_used;
+ }
+
+double Timer::seconds()
+ {
+ return milliseconds() / 1000.0;
+ }
+
+double Timer::milliseconds()
+ {
+ return value() / 1000000.0;
+ }
+
+double Timer::ms_per_event()
+ {
+ return milliseconds() / events();
+ }
+
+double Timer::seconds_per_event()
+ {
+ return seconds() / events();
+ }
+
+std::ostream& operator<<(std::ostream& out, Timer& timer)
+ {
+ //out << timer.value() << " ";
+
+ int events_per_second = timer.events() / timer.seconds();
+
+ out << events_per_second << " " << timer.get_name() << " per second; ";
+
+ if(timer.seconds_per_event() < 10)
+ out << std::setprecision(2) << std::fixed
+ << timer.ms_per_event() << " ms/" << timer.get_name();
+ else
+ out << std::setprecision(4) << std::fixed
+ << timer.seconds_per_event() << " s/" << timer.get_name();
+
+ if(timer.seconds() < 10)
+ out << " (" << timer.events() << " ops in "
+ << timer.milliseconds() << " ms)";
+ else
+ out << " (" << timer.events() << " ops in "
+ << timer.seconds() << " s)";
+
+ return out;
+ }
diff --git a/checks/timer.h b/checks/timer.h
new file mode 100644
index 000000000..80aeec9e6
--- /dev/null
+++ b/checks/timer.h
@@ -0,0 +1,50 @@
+
+#ifndef BOTAN_BENCHMARK_TIMER_H__
+#define BOTAN_BENCHMARK_TIMER_H__
+
+#include <botan/types.h>
+#include <ostream>
+#include <string>
+
+using Botan::u64bit;
+using Botan::u32bit;
+
+class Timer
+ {
+ public:
+ static u64bit get_clock();
+
+ Timer(const std::string& name);
+
+ void start();
+
+ void stop();
+
+ u64bit value();
+ double seconds();
+ double milliseconds();
+
+ double ms_per_event();
+ double seconds_per_event();
+
+ u32bit events() const { return event_count; }
+ std::string get_name() const { return name; }
+ private:
+ std::string name;
+ u64bit time_used, timer_start;
+ u32bit event_count;
+ };
+
+inline bool operator<(const Timer& x, const Timer& y)
+ {
+ return (x.get_name() < y.get_name());
+ }
+
+inline bool operator==(const Timer& x, const Timer& y)
+ {
+ return (x.get_name() == y.get_name());
+ }
+
+std::ostream& operator<<(std::ostream&, Timer&);
+
+#endif