-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test_stopwatch: fix on mingw #2415
Conversation
@neheb Could you also fix the test case "stopwatch2"? It should have the same problem. |
Having trouble figuring that out. stopwatch2 deals with doubles whereas std::chrono::milliseconds deals with long_int. Some weird conversion is needed. |
In GCC #include <iostream>
#include <cstdlib>
#include <chrono>
#include <thread>
int main()
{
using std::chrono::milliseconds;
using std::chrono::duration_cast;
using clock = std::chrono::steady_clock;
using duration = clock::duration;
duration wait_duration(milliseconds(250));
duration tolerance_duration(milliseconds(250));
auto start_duration = clock::now();
std::this_thread::sleep_for(wait_duration);
auto stop_duration = clock::now();
auto diff_duration = stop_duration - start_duration;
std::cout << duration_cast<duration>(milliseconds(250)).count() << "\n";
std::cout << duration_cast<milliseconds>(diff_duration).count() << "\n";
std::cout << diff_duration.count() << "\n";
std::cout << (diff_duration + tolerance_duration).count() << "\n";
} |
I noticed that TEST_CASE("stopwatch2", "[stopwatch]")
{
using spdlog::sinks::test_sink_st;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using clock = std::chrono::steady_clock;
clock::duration wait_duration(milliseconds(250));
clock::duration tolerance_duration(milliseconds(250));
auto test_sink = std::make_shared<test_sink_st>();
auto start = clock::now();
spdlog::stopwatch sw;
spdlog::logger logger("test-stopwatch", test_sink);
logger.set_pattern("%v");
std::this_thread::sleep_for(wait_duration);
auto stop = clock::now();
logger.info("{}", sw);
std::cout << test_sink->lines()[0] << "\n";
auto val = std::stod(test_sink->lines()[0]);
auto diff_duration = duration_cast<std::chrono::duration<double>>(stop - start);
REQUIRE(val >= diff_duration.count());
REQUIRE(val <= (diff_duration + tolerance_duration).count());
} Diff TEST_CASE("stopwatch2", "[stopwatch]")
{
using spdlog::sinks::test_sink_st;
+ using std::chrono::duration_cast;
+ using std::chrono::milliseconds;
+ using clock = std::chrono::steady_clock;
- std::chrono::duration<double> wait_duration(0.250);
- std::chrono::duration<double> tolerance_duration(0.250);
+ clock::duration wait_duration(milliseconds(250));
+ clock::duration tolerance_duration(milliseconds(250));
auto test_sink = std::make_shared<test_sink_st>();
+ auto start = clock::now();
spdlog::stopwatch sw;
spdlog::logger logger("test-stopwatch", test_sink);
logger.set_pattern("%v");
std::this_thread::sleep_for(wait_duration);
+ auto stop = clock::now();
logger.info("{}", sw);
+ std::cout << test_sink->lines()[0] << "\n";
auto val = std::stod(test_sink->lines()[0]);
+ auto diff_duration = duration_cast<std::chrono::duration<double>>(stop - start);
- REQUIRE(val >= wait_duration.count());
- REQUIRE(val <= (wait_duration + tolerance_duration).count());
+ REQUIRE(val >= diff_duration.count());
+ REQUIRE(val <= (diff_duration + tolerance_duration).count());
} |
It already passes. Just test one had issues. edit: nvm just failed:
|
Updated. |
Oh wait, the source code I pasted still has the statement for debugging. |
I don’t think that’s the error here. |
Ah I have a stoll call. |
There are some timing shenanigans with GCC's chrono that make this unreliable. Add a start/stop and test for that to work around. Signed-off-by: Rosen Penev <rosenp@gmail.com>
logger.info("{}", sw); | ||
auto val = std::stod(test_sink->lines()[0]); | ||
auto diff_duration = duration_cast<std::chrono::duration<double>>(stop - start); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets please use same diff method in both functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed because test2 uses a double.
@tt4g found out why this was an issue. LLVM's libc++ calls the proper windows APIs directly instead of going through clock_gettime. libstdc++ uses clock_gettime. The difference is that clock_gettime as implemented in winpthreads calls GetSystemTimeAsFileTime. libc++ uses GetSystemTimePreciseAsFileTime and falls back to GetSystemTimeAsFileTime for Windows versions older than 8. |
@neheb Great! |
There are some timing shenanigans with GCC's chrono that make this
unreliable. Add a start/stop and test for that to work around.
Signed-off-by: Rosen Penev rosenp@gmail.com