Commit a0b10b4c505f4cab75cb8e1ecd4cd7c23d8beef5
1 parent
90a02425
Adding timer
Showing
4 changed files
with
140 additions
and
0 deletions
CHANGELOG.md
| ... | ... | @@ -7,6 +7,7 @@ |
| 7 | 7 | * Lots of small bugfixes related to adding tests to increase coverage |
| 8 | 8 | * Error handling now uses scoped enum in errors |
| 9 | 9 | * Reparsing rules changed a little to accommodate Ini files. Callbacks are now called when parsing INI, and reset any time results are added. |
| 10 | +* Adding extra utilities in seperate files version only, `Timer` (not needed for parsing, but useful for general CLI applications). | |
| 10 | 11 | |
| 11 | 12 | ## Version 0.6 |
| 12 | 13 | ... | ... |
include/CLI/Timer.hpp
0 → 100644
| 1 | +#pragma once | |
| 2 | + | |
| 3 | +// Distributed under the LGPL v2.1 license. See accompanying | |
| 4 | +// file LICENSE or https://github.com/henryiii/CLI11 for details. | |
| 5 | + | |
| 6 | +#include <string> | |
| 7 | +#include <iostream> | |
| 8 | +#include <chrono> | |
| 9 | +#include <functional> | |
| 10 | + | |
| 11 | +namespace CLI { | |
| 12 | + | |
| 13 | +class Timer { | |
| 14 | +protected: | |
| 15 | + typedef std::chrono::high_resolution_clock clock; | |
| 16 | + typedef std::chrono::time_point<clock> time_point; | |
| 17 | + typedef std::function<std::string(std::string, std::string)> time_print_t; | |
| 18 | + | |
| 19 | + std::string title_; | |
| 20 | + time_point start_; | |
| 21 | + time_print_t time_print_; | |
| 22 | +public: | |
| 23 | + static std::string Simple(std::string title, std::string time) { | |
| 24 | + return title + ": " + time; | |
| 25 | + } | |
| 26 | + | |
| 27 | + static std::string Big(std::string title, std::string time) { | |
| 28 | + return "-----------------------------------------\n" | |
| 29 | + + title + " | Time = " + time + "\n" | |
| 30 | + + "-----------------------------------------"; | |
| 31 | + } | |
| 32 | + | |
| 33 | +public: | |
| 34 | + Timer(std::string title="Timer", time_print_t time_print = Simple) | |
| 35 | + : title_(title), time_print_(time_print), start_(clock::now()) {} | |
| 36 | + | |
| 37 | + | |
| 38 | + std::string make_time_str() const { | |
| 39 | + time_point stop = clock::now(); | |
| 40 | + std::chrono::duration<double, std::milli> elapsed = stop - start_; | |
| 41 | + double time = elapsed.count(); | |
| 42 | + | |
| 43 | + // LCOV_EXCL_START | |
| 44 | + if(time < 1) | |
| 45 | + return std::to_string(int(time*1000000)) + " ns"; | |
| 46 | + else if(time < 10) | |
| 47 | + return std::to_string(int(time*100) / 100.) + " ms"; | |
| 48 | + else if(time < 1000) | |
| 49 | + return std::to_string(int(time)) + " ms"; | |
| 50 | + else if(time < 10000) | |
| 51 | + return std::to_string(int(time*10000) / 10. ) + " s"; | |
| 52 | + else | |
| 53 | + return std::to_string(int(time*1000)) + " s"; | |
| 54 | + // LCOV_EXCL_END | |
| 55 | + } | |
| 56 | + std::string to_string() const { | |
| 57 | + return time_print_(title_, make_time_str()); | |
| 58 | + } | |
| 59 | +}; | |
| 60 | + | |
| 61 | +class AutoTimer : public Timer { | |
| 62 | +public: | |
| 63 | + using Timer::Timer; | |
| 64 | + | |
| 65 | + ~AutoTimer () { | |
| 66 | + std::cout << to_string() << std::endl; | |
| 67 | + } | |
| 68 | +}; | |
| 69 | + | |
| 70 | +} | |
| 71 | + | |
| 72 | +std::ostream & operator<< (std::ostream& in, const CLI::Timer& timer) { | |
| 73 | + return in << timer.to_string(); | |
| 74 | +} | ... | ... |
tests/CMakeLists.txt
| ... | ... | @@ -9,6 +9,10 @@ set(CLI_TESTS |
| 9 | 9 | SubcommandTest |
| 10 | 10 | HelpTest) |
| 11 | 11 | |
| 12 | +set(CLI_SINGLE_TESTS | |
| 13 | + TimerTest | |
| 14 | + ) | |
| 15 | + | |
| 12 | 16 | # Only affects current directory, so safe |
| 13 | 17 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}) |
| 14 | 18 | |
| ... | ... | @@ -26,3 +30,11 @@ foreach(T ${CLI_TESTS}) |
| 26 | 30 | |
| 27 | 31 | endforeach() |
| 28 | 32 | |
| 33 | +foreach(T ${CLI_SINGLE_TESTS}) | |
| 34 | + | |
| 35 | + add_executable(${T} ${T}.cpp ${CLI_headers}) | |
| 36 | + target_link_libraries(${T} PUBLIC CLI) | |
| 37 | + add_gtest(${T}) | |
| 38 | + | |
| 39 | +endforeach() | |
| 40 | + | ... | ... |
tests/TimerTest.cpp
0 → 100644
| 1 | +#include "gtest/gtest.h" | |
| 2 | +#include "gmock/gmock.h" | |
| 3 | +#include "CLI/Timer.hpp" | |
| 4 | +#include <string> | |
| 5 | +#include <chrono> | |
| 6 | +#include <thread> | |
| 7 | +#include <sstream> | |
| 8 | + | |
| 9 | +using ::testing::HasSubstr; | |
| 10 | + | |
| 11 | +TEST(Timer, MSTimes) { | |
| 12 | + CLI::Timer timer{"My Timer"}; | |
| 13 | + std::this_thread::sleep_for(std::chrono::milliseconds(123)); | |
| 14 | + std::string output = timer.to_string(); | |
| 15 | + EXPECT_THAT(output, HasSubstr("My Timer")); | |
| 16 | + EXPECT_THAT(output, HasSubstr(" ms")); | |
| 17 | +} | |
| 18 | + | |
| 19 | +/* Takes too long | |
| 20 | +TEST(Timer, STimes) { | |
| 21 | + CLI::Timer timer; | |
| 22 | + std::this_thread::sleep_for(std::chrono::seconds(1)); | |
| 23 | + std::string output = timer.to_string(); | |
| 24 | + EXPECT_THAT(output, HasSubstr(" s")); | |
| 25 | +} | |
| 26 | +*/ | |
| 27 | + | |
| 28 | +TEST(Timer, NStimes) { | |
| 29 | + CLI::Timer timer; | |
| 30 | + std::string output = timer.to_string(); | |
| 31 | + EXPECT_THAT(output, HasSubstr(" ns")); | |
| 32 | +} | |
| 33 | + | |
| 34 | +TEST(Timer, BigTimer) { | |
| 35 | + CLI::Timer timer{"My Timer", CLI::Timer::Big}; | |
| 36 | + std::string output = timer.to_string(); | |
| 37 | + EXPECT_THAT(output, HasSubstr("Time =")); | |
| 38 | + EXPECT_THAT(output, HasSubstr("-----------")); | |
| 39 | +} | |
| 40 | + | |
| 41 | +TEST(Timer, AutoTimer) { | |
| 42 | + CLI::AutoTimer timer; | |
| 43 | + std::string output = timer.to_string(); | |
| 44 | + EXPECT_THAT(output, HasSubstr("Timer")); | |
| 45 | +} | |
| 46 | + | |
| 47 | +TEST(Timer, PrintTimer) { | |
| 48 | + std::stringstream out; | |
| 49 | + CLI::AutoTimer timer; | |
| 50 | + out << timer; | |
| 51 | + std::string output = out.str(); | |
| 52 | + EXPECT_THAT(output, HasSubstr("Timer")); | |
| 53 | +} | ... | ... |