mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-02 05:33:53 +00:00
Adding timer
This commit is contained in:
parent
90a02425f0
commit
a0b10b4c50
@ -7,6 +7,7 @@
|
|||||||
* Lots of small bugfixes related to adding tests to increase coverage
|
* Lots of small bugfixes related to adding tests to increase coverage
|
||||||
* Error handling now uses scoped enum in errors
|
* Error handling now uses scoped enum in errors
|
||||||
* Reparsing rules changed a little to accommodate Ini files. Callbacks are now called when parsing INI, and reset any time results are added.
|
* Reparsing rules changed a little to accommodate Ini files. Callbacks are now called when parsing INI, and reset any time results are added.
|
||||||
|
* Adding extra utilities in seperate files version only, `Timer` (not needed for parsing, but useful for general CLI applications).
|
||||||
|
|
||||||
## Version 0.6
|
## Version 0.6
|
||||||
|
|
||||||
|
74
include/CLI/Timer.hpp
Normal file
74
include/CLI/Timer.hpp
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Distributed under the LGPL v2.1 license. See accompanying
|
||||||
|
// file LICENSE or https://github.com/henryiii/CLI11 for details.
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
namespace CLI {
|
||||||
|
|
||||||
|
class Timer {
|
||||||
|
protected:
|
||||||
|
typedef std::chrono::high_resolution_clock clock;
|
||||||
|
typedef std::chrono::time_point<clock> time_point;
|
||||||
|
typedef std::function<std::string(std::string, std::string)> time_print_t;
|
||||||
|
|
||||||
|
std::string title_;
|
||||||
|
time_point start_;
|
||||||
|
time_print_t time_print_;
|
||||||
|
public:
|
||||||
|
static std::string Simple(std::string title, std::string time) {
|
||||||
|
return title + ": " + time;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string Big(std::string title, std::string time) {
|
||||||
|
return "-----------------------------------------\n"
|
||||||
|
+ title + " | Time = " + time + "\n"
|
||||||
|
+ "-----------------------------------------";
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
Timer(std::string title="Timer", time_print_t time_print = Simple)
|
||||||
|
: title_(title), time_print_(time_print), start_(clock::now()) {}
|
||||||
|
|
||||||
|
|
||||||
|
std::string make_time_str() const {
|
||||||
|
time_point stop = clock::now();
|
||||||
|
std::chrono::duration<double, std::milli> elapsed = stop - start_;
|
||||||
|
double time = elapsed.count();
|
||||||
|
|
||||||
|
// LCOV_EXCL_START
|
||||||
|
if(time < 1)
|
||||||
|
return std::to_string(int(time*1000000)) + " ns";
|
||||||
|
else if(time < 10)
|
||||||
|
return std::to_string(int(time*100) / 100.) + " ms";
|
||||||
|
else if(time < 1000)
|
||||||
|
return std::to_string(int(time)) + " ms";
|
||||||
|
else if(time < 10000)
|
||||||
|
return std::to_string(int(time*10000) / 10. ) + " s";
|
||||||
|
else
|
||||||
|
return std::to_string(int(time*1000)) + " s";
|
||||||
|
// LCOV_EXCL_END
|
||||||
|
}
|
||||||
|
std::string to_string() const {
|
||||||
|
return time_print_(title_, make_time_str());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class AutoTimer : public Timer {
|
||||||
|
public:
|
||||||
|
using Timer::Timer;
|
||||||
|
|
||||||
|
~AutoTimer () {
|
||||||
|
std::cout << to_string() << std::endl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream & operator<< (std::ostream& in, const CLI::Timer& timer) {
|
||||||
|
return in << timer.to_string();
|
||||||
|
}
|
@ -9,6 +9,10 @@ set(CLI_TESTS
|
|||||||
SubcommandTest
|
SubcommandTest
|
||||||
HelpTest)
|
HelpTest)
|
||||||
|
|
||||||
|
set(CLI_SINGLE_TESTS
|
||||||
|
TimerTest
|
||||||
|
)
|
||||||
|
|
||||||
# Only affects current directory, so safe
|
# Only affects current directory, so safe
|
||||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
|
||||||
@ -26,3 +30,11 @@ foreach(T ${CLI_TESTS})
|
|||||||
|
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|
||||||
|
foreach(T ${CLI_SINGLE_TESTS})
|
||||||
|
|
||||||
|
add_executable(${T} ${T}.cpp ${CLI_headers})
|
||||||
|
target_link_libraries(${T} PUBLIC CLI)
|
||||||
|
add_gtest(${T})
|
||||||
|
|
||||||
|
endforeach()
|
||||||
|
|
||||||
|
53
tests/TimerTest.cpp
Normal file
53
tests/TimerTest.cpp
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "gmock/gmock.h"
|
||||||
|
#include "CLI/Timer.hpp"
|
||||||
|
#include <string>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
using ::testing::HasSubstr;
|
||||||
|
|
||||||
|
TEST(Timer, MSTimes) {
|
||||||
|
CLI::Timer timer{"My Timer"};
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(123));
|
||||||
|
std::string output = timer.to_string();
|
||||||
|
EXPECT_THAT(output, HasSubstr("My Timer"));
|
||||||
|
EXPECT_THAT(output, HasSubstr(" ms"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Takes too long
|
||||||
|
TEST(Timer, STimes) {
|
||||||
|
CLI::Timer timer;
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
|
std::string output = timer.to_string();
|
||||||
|
EXPECT_THAT(output, HasSubstr(" s"));
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
TEST(Timer, NStimes) {
|
||||||
|
CLI::Timer timer;
|
||||||
|
std::string output = timer.to_string();
|
||||||
|
EXPECT_THAT(output, HasSubstr(" ns"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Timer, BigTimer) {
|
||||||
|
CLI::Timer timer{"My Timer", CLI::Timer::Big};
|
||||||
|
std::string output = timer.to_string();
|
||||||
|
EXPECT_THAT(output, HasSubstr("Time ="));
|
||||||
|
EXPECT_THAT(output, HasSubstr("-----------"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Timer, AutoTimer) {
|
||||||
|
CLI::AutoTimer timer;
|
||||||
|
std::string output = timer.to_string();
|
||||||
|
EXPECT_THAT(output, HasSubstr("Timer"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Timer, PrintTimer) {
|
||||||
|
std::stringstream out;
|
||||||
|
CLI::AutoTimer timer;
|
||||||
|
out << timer;
|
||||||
|
std::string output = out.str();
|
||||||
|
EXPECT_THAT(output, HasSubstr("Timer"));
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user