2020-08-29 08:25:16 +08:00
|
|
|
#include "includes.h"
|
|
|
|
#include "test_sink.h"
|
|
|
|
#include "spdlog/stopwatch.h"
|
|
|
|
|
|
|
|
TEST_CASE("stopwatch1", "[stopwatch]")
|
|
|
|
{
|
|
|
|
using std::chrono::milliseconds;
|
2022-06-24 08:18:35 +08:00
|
|
|
using clock = std::chrono::steady_clock;
|
2023-02-26 08:31:12 +08:00
|
|
|
milliseconds wait_ms(200);
|
2020-08-29 08:25:16 +08:00
|
|
|
milliseconds tolerance_ms(250);
|
2022-06-24 08:18:35 +08:00
|
|
|
auto start = clock::now();
|
2020-08-29 08:25:16 +08:00
|
|
|
spdlog::stopwatch sw;
|
|
|
|
std::this_thread::sleep_for(wait_ms);
|
2022-06-24 08:18:35 +08:00
|
|
|
auto stop = clock::now();
|
|
|
|
auto diff_ms = std::chrono::duration_cast<milliseconds>(stop - start);
|
|
|
|
REQUIRE(sw.elapsed() >= diff_ms);
|
|
|
|
REQUIRE(sw.elapsed() <= diff_ms + tolerance_ms);
|
2020-08-29 08:25:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("stopwatch2", "[stopwatch]")
|
|
|
|
{
|
|
|
|
using spdlog::sinks::test_sink_st;
|
2022-06-24 08:18:35 +08:00
|
|
|
using std::chrono::duration_cast;
|
|
|
|
using std::chrono::milliseconds;
|
|
|
|
using clock = std::chrono::steady_clock;
|
2020-08-29 08:25:16 +08:00
|
|
|
|
2023-02-26 08:31:12 +08:00
|
|
|
clock::duration wait_duration(milliseconds(200));
|
2022-06-24 08:18:35 +08:00
|
|
|
clock::duration tolerance_duration(milliseconds(250));
|
2020-08-29 08:25:16 +08:00
|
|
|
|
|
|
|
auto test_sink = std::make_shared<test_sink_st>();
|
|
|
|
|
2022-06-24 08:18:35 +08:00
|
|
|
auto start = clock::now();
|
2020-08-29 08:25:16 +08:00
|
|
|
spdlog::stopwatch sw;
|
|
|
|
spdlog::logger logger("test-stopwatch", test_sink);
|
|
|
|
logger.set_pattern("%v");
|
|
|
|
std::this_thread::sleep_for(wait_duration);
|
2022-06-24 08:18:35 +08:00
|
|
|
auto stop = clock::now();
|
2020-08-29 08:25:16 +08:00
|
|
|
logger.info("{}", sw);
|
|
|
|
auto val = std::stod(test_sink->lines()[0]);
|
2022-06-24 08:18:35 +08:00
|
|
|
auto diff_duration = duration_cast<std::chrono::duration<double>>(stop - start);
|
2020-08-29 08:25:16 +08:00
|
|
|
|
2022-06-24 08:18:35 +08:00
|
|
|
REQUIRE(val >= diff_duration.count());
|
|
|
|
REQUIRE(val <= (diff_duration + tolerance_duration).count());
|
2020-08-29 08:25:16 +08:00
|
|
|
}
|