[Groonga-commit] groonga/grnxx [master] Add grnxx::Stopwatch.

Zurück zum Archiv-Index

susumu.yata null+****@clear*****
Thu Feb 28 12:10:04 JST 2013


susumu.yata	2013-02-28 12:10:04 +0900 (Thu, 28 Feb 2013)

  New Revision: 8ca0d8f4149ee9b1442072ec529cc01d2e3f1ac9
  https://github.com/groonga/grnxx/commit/8ca0d8f4149ee9b1442072ec529cc01d2e3f1ac9

  Log:
    Add grnxx::Stopwatch.

  Added files:
    lib/stopwatch.hpp
    test/test_stopwatch.cpp
  Modified files:
    .gitignore
    test/Makefile.am
    test/test_broken_down_time.cpp

  Modified: .gitignore (+1 -0)
===================================================================
--- .gitignore    2013-02-28 11:36:19 +0900 (a22f989)
+++ .gitignore    2013-02-28 12:10:04 +0900 (66dbbd7)
@@ -53,6 +53,7 @@ test/test_mutex
 test/test_os
 test/test_recycler
 test/test_slice
+test/test_stopwatch
 test/test_string
 test/test_string_builder
 test/test_string_format

  Added: lib/stopwatch.hpp (+76 -0) 100644
===================================================================
--- /dev/null
+++ lib/stopwatch.hpp    2013-02-28 12:10:04 +0900 (cc9bdde)
@@ -0,0 +1,76 @@
+/*
+  Copyright (C) 2012-2013  Brazil, Inc.
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+#ifndef GRNXX_STOPWATCH_HPP
+#define GRNXX_STOPWATCH_HPP
+
+#include "basic.hpp"
+#include "steady_clock.hpp"
+
+namespace grnxx {
+
+// To measure the amount of time elapsed.
+class Stopwatch {
+ public:
+  Stopwatch() : elapsed_(0), start_time_(), is_running_(false) {}
+
+  // Return true iff the stopwatch is running.
+  bool is_running() const {
+    return is_running_;
+  }
+
+  // Start measurement.
+  void start() {
+    if (!is_running_) {
+      start_time_ = SteadyClock::now();
+      is_running_ = true;
+    }
+  }
+  // Stop measurement.
+  void stop() {
+    if (is_running_) {
+      elapsed_ += SteadyClock::now() - start_time_;
+      is_running_ = false;
+    }
+  }
+
+  // Clear the elapsed time.
+  void reset() {
+    if (is_running_) {
+      start_time_ = SteadyClock::now();
+    }
+    elapsed_ = Duration(0);
+  }
+
+  // Get the current elapsed time.
+  Duration elapsed() const {
+    if (is_running_) {
+      return elapsed_ + (SteadyClock::now() - start_time_);
+    } else {
+      return elapsed_;
+    }
+  }
+
+ private:
+  Duration elapsed_;
+  Time start_time_;
+  bool is_running_;
+};
+
+}  // namespace grnxx
+
+#endif  // GRNXX_STOPWATCH_HPP

  Modified: test/Makefile.am (+4 -0)
===================================================================
--- test/Makefile.am    2013-02-28 11:36:19 +0900 (4115bff)
+++ test/Makefile.am    2013-02-28 12:10:04 +0900 (72c1ec1)
@@ -26,6 +26,7 @@ TESTS =					\
 	test_os				\
 	test_recycler			\
 	test_slice			\
+	test_stopwatch			\
 	test_string			\
 	test_string_builder		\
 	test_string_format		\
@@ -109,6 +110,9 @@ test_recycler_LDADD = ../lib/libgrnxx.la
 test_slice_SOURCES = test_slice.cpp
 test_slice_LDADD = ../lib/libgrnxx.la
 
+test_stopwatch_SOURCES = test_stopwatch.cpp
+test_stopwatch_LDADD = ../lib/libgrnxx.la
+
 test_string_SOURCES = test_string.cpp
 test_string_LDADD = ../lib/libgrnxx.la
 

  Modified: test/test_broken_down_time.cpp (+1 -1)
===================================================================
--- test/test_broken_down_time.cpp    2013-02-28 11:36:19 +0900 (cde8438)
+++ test/test_broken_down_time.cpp    2013-02-28 12:10:04 +0900 (528bf5b)
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2012-2013  Brazil, Inc.
+  Copyright (C) 2013  Brazil, Inc.
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public

  Added: test/test_stopwatch.cpp (+66 -0) 100644
===================================================================
--- /dev/null
+++ test/test_stopwatch.cpp    2013-02-28 12:10:04 +0900 (7b226a5)
@@ -0,0 +1,66 @@
+/*
+  Copyright (C) 2013  Brazil, Inc.
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+#include <cassert>
+
+#include "logger.hpp"
+#include "stopwatch.hpp"
+#include "thread.hpp"
+
+int main() {
+  grnxx::Logger::set_flags(grnxx::LOGGER_WITH_ALL |
+                           grnxx::LOGGER_ENABLE_COUT);
+  grnxx::Logger::set_max_level(grnxx::NOTICE_LOGGER);
+
+  grnxx::Stopwatch stopwatch;
+  assert(stopwatch.elapsed() == grnxx::Duration(0));
+
+  stopwatch.start();
+  grnxx::Thread::sleep(grnxx::Duration::milliseconds(1));
+  grnxx::Duration elapsed = stopwatch.elapsed();
+  assert(elapsed > grnxx::Duration(0));
+
+  grnxx::Thread::sleep(grnxx::Duration::milliseconds(1));
+  assert(stopwatch.elapsed() > elapsed);
+
+  stopwatch.stop();
+  elapsed = stopwatch.elapsed();
+
+  grnxx::Thread::sleep(grnxx::Duration::milliseconds(1));
+  assert(stopwatch.elapsed() == elapsed);
+
+  stopwatch.start();
+  grnxx::Thread::sleep(grnxx::Duration::milliseconds(1));
+  assert(stopwatch.elapsed() > elapsed);
+
+  GRNXX_NOTICE() << "stopwatch.elapsed() = " << stopwatch.elapsed();
+
+  elapsed = stopwatch.elapsed();
+  stopwatch.reset();
+  grnxx::Thread::sleep(grnxx::Duration::milliseconds(1));
+  assert(stopwatch.elapsed() < elapsed);
+  assert(stopwatch.elapsed() > grnxx::Duration(0));
+
+  stopwatch.stop();
+  stopwatch.reset();
+  assert(stopwatch.elapsed() == grnxx::Duration(0));
+
+  grnxx::Thread::sleep(grnxx::Duration::milliseconds(1));
+  assert(stopwatch.elapsed() == grnxx::Duration(0));
+
+  return 0;
+}
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index