Mercurial > code
changeset 532:33f98fda1884
ElapsedTimer: import
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 02 Jun 2016 16:56:44 +0200 |
parents | 25e6380d0849 |
children | 9bf71bfe02fe |
files | CMakeLists.txt modules/elapsed-timer/CMakeLists.txt modules/elapsed-timer/elapsed-timer.hpp modules/elapsed-timer/test/CMakeLists.txt modules/elapsed-timer/test/main.cpp |
diffstat | 5 files changed, 203 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Thu Jun 02 16:56:30 2016 +0200 +++ b/CMakeLists.txt Thu Jun 02 16:56:44 2016 +0200 @@ -44,6 +44,7 @@ add_subdirectory(modules/base64) add_subdirectory(modules/dynlib) +add_subdirectory(modules/elapsed-timer) add_subdirectory(modules/fs) add_subdirectory(modules/hash) add_subdirectory(modules/ini)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/elapsed-timer/CMakeLists.txt Thu Jun 02 16:56:44 2016 +0200 @@ -0,0 +1,22 @@ +# +# CMakeLists.txt -- code building for common code +# +# Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +code_define_module( + NAME elapsed-timer + SOURCES elapsed-timer.hpp +) \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/elapsed-timer/elapsed-timer.hpp Thu Jun 02 16:56:44 2016 +0200 @@ -0,0 +1,103 @@ +/* + * elapsed-timer.hpp -- measure elapsed time + * + * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef ELAPSED_TIMER_HPP +#define ELAPSED_TIMER_HPP + +/** + * \file elapsed-timer.hpp + * \brief Measure elapsed time + */ + +#include <chrono> + +/** + * \brief Measure elapsed time + * + * This class provides an abstraction to measure elapsed time since the construction of the object. It uses + * std::chrono::high_resolution_clock for more precision and uses milliseconds only. + */ +class ElapsedTimer { +private: + using TimePoint = std::chrono::time_point<std::chrono::high_resolution_clock>; + + TimePoint m_last; + bool m_paused{false}; + unsigned m_elapsed{0}; + +public: + /** + * Construct the elapsed timer, start counting. + */ + inline ElapsedTimer() noexcept + : m_last(std::chrono::high_resolution_clock::now()) + { + } + + /** + * Put the timer on pause, the already elapsed time is stored. + */ + inline void pause() noexcept + { + /* + * When we put the timer on pause, do not forget to set the already + * elapsed time. + */ + elapsed(); + m_paused = true; + } + + /** + * Restart the timer, does not reset it. + */ + inline void restart() noexcept + { + m_paused = false; + m_last = std::chrono::high_resolution_clock::now(); + } + + /** + * Reset the timer to 0. + */ + inline void reset() noexcept + { + m_elapsed = 0; + m_last = std::chrono::high_resolution_clock::now(); + } + + /** + * Get the number of elapsed milliseconds. + * + * \return the milliseconds + */ + inline unsigned elapsed() noexcept + { + using std::chrono::duration_cast; + using std::chrono::high_resolution_clock; + using std::chrono::milliseconds; + + if (!m_paused) { + m_elapsed += duration_cast<milliseconds>(high_resolution_clock::now() - m_last).count(); + m_last = high_resolution_clock::now(); + } + + return m_elapsed; + } +}; + +#endif // !ELAPSED_TIMER_HPP
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/elapsed-timer/test/CMakeLists.txt Thu Jun 02 16:56:44 2016 +0200 @@ -0,0 +1,23 @@ +# +# CMakeLists.txt -- CMake build system for irccd +# +# Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +irccd_define_test( + NAME elapsedtimer + SOURCES main.cpp + LIBRARIES libirccd +)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/elapsed-timer/test/main.cpp Thu Jun 02 16:56:44 2016 +0200 @@ -0,0 +1,54 @@ +/* + * main.cpp -- test ElapsedTimer + * + * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <thread> + +#include <gtest/gtest.h> + +#include <elapsed-timer.hpp> + +using namespace std::chrono_literals; + +TEST(TestElapsedTimer, standard) +{ + ElapsedTimer timer; + + std::this_thread::sleep_for(300ms); + + ASSERT_GE(timer.elapsed(), 250U); + ASSERT_LE(timer.elapsed(), 350U); +} + +TEST(TestElapsedTimer, reset) +{ + ElapsedTimer timer; + + std::this_thread::sleep_for(300ms); + + timer.reset(); + + ASSERT_LE(timer.elapsed(), 100U); +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +} +