aboutsummaryrefslogtreecommitdiffstats
path: root/fbench/src/util/timer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'fbench/src/util/timer.cpp')
-rw-r--r--fbench/src/util/timer.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/fbench/src/util/timer.cpp b/fbench/src/util/timer.cpp
new file mode 100644
index 00000000000..c96a52a1d70
--- /dev/null
+++ b/fbench/src/util/timer.cpp
@@ -0,0 +1,90 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+#include "timer.h"
+#include <stdio.h>
+#include <thread>
+
+Timer::Timer()
+ : _time(),
+ _timespan(0),
+ _maxTime(0),
+ _running(false)
+{
+}
+
+void
+Timer::SetMax(double max)
+{
+ _maxTime = max;
+}
+
+void
+Timer::Start()
+{
+ if (_running)
+ return;
+ _running = true;
+ _time = clock::now();
+}
+
+void
+Timer::Stop()
+{
+ if (!_running)
+ return;
+ _timespan = GetCurrent();
+ _running = false;
+}
+
+void
+Timer::Clear()
+{
+ _running = false;
+ _timespan = 0;
+}
+
+double
+Timer::GetTimespan()
+{
+ if (_running)
+ Stop();
+ return _timespan;
+}
+
+double
+Timer::GetRemaining()
+{
+ double span = GetTimespan();
+ return (span < _maxTime) ? _maxTime - span : 0;
+}
+
+double
+Timer::GetCurrent()
+{
+ if (!_running)
+ return 0;
+ using milliseconds = std::chrono::duration<double, std::milli>;
+ return std::chrono::duration_cast<milliseconds>(time_point(clock::now()) - _time).count();
+}
+
+void
+Timer::TestClass()
+{
+ Timer test;
+
+ printf("*** Start Testing: class Timer ***\n");
+ printf("set max time to 5 seconds, then sleep for 1...\n");
+ test.SetMax(5000);
+ test.Start();
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+ test.Stop();
+ printf("elapsed: %f, left:%f\n",
+ test.GetTimespan(), test.GetRemaining());
+ printf("set max time to 1 second, then sleep for 2...\n");
+ test.SetMax(1000);
+ test.Start();
+ std::this_thread::sleep_for(std::chrono::seconds(2));
+ test.Stop();
+ printf("elapsed: %f, left:%f\n",
+ test.GetTimespan(), test.GetRemaining());
+ printf("*** Finished Testing: class Timer ***\n");
+}