pytorch/caffe2/core/timer_test.cc
Nikita Shulga a9b0a921d5 Disable avoid-non-const-global-variables lint check (#62008)
Summary:
As GoogleTest `TEST` macro is non-compliant with it as well as `DEFINE_DISPATCH`

All changes but the ones to `.clang-tidy` are generated using following script:
```
for i in `find . -type f -iname "*.c*" -or -iname "*.h"|xargs grep cppcoreguidelines-avoid-non-const-global-variables|cut -f1 -d:|sort|uniq`;  do sed -i "/\/\/ NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)/d" $i; done
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/62008

Reviewed By: driazati, r-barnes

Differential Revision: D29838584

Pulled By: malfet

fbshipit-source-id: 1b2f8602c945bd4ce50a9bfdd204755556e31d13
2021-07-22 18:04:40 -07:00

66 lines
1.6 KiB
C++

#include <chrono>
#include <iostream>
#include <thread>
#include "caffe2/core/timer.h"
#include <gtest/gtest.h>
namespace caffe2 {
namespace {
TEST(TimerTest, Test) {
Timer timer;
// A timer auto-starts when it is constructed.
std::this_thread::sleep_for(std::chrono::microseconds(1));
EXPECT_GT(timer.NanoSeconds(), 0);
// Sleep for a while, and get the time.
timer.Start();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
float ns = timer.NanoSeconds();
float us = timer.MicroSeconds();
float ms = timer.MilliSeconds();
// Time should be at least accurate +- 10%. (30% on Windows)
#ifndef _WIN32
EXPECT_NEAR(ns, 100000000, 10000000);
EXPECT_NEAR(us, 100000, 10000);
EXPECT_NEAR(ms, 100, 10);
#else
EXPECT_NEAR(ns, 100000000, 30000000);
EXPECT_NEAR(us, 100000, 30000);
EXPECT_NEAR(ms, 100, 30);
#endif
// Test restarting the clock.
timer.Start();
EXPECT_LT(timer.MicroSeconds(), 1000);
}
TEST(TimerTest, TestLatency) {
constexpr int iter = 1000;
float latency = 0;
Timer timer;
for (int i = 0; i < iter; ++i) {
timer.Start();
latency += timer.NanoSeconds();
}
std::cout << "Average nanosecond latency is: " << latency / iter << std::endl;
latency = 0;
for (int i = 0; i < iter; ++i) {
timer.Start();
latency += timer.MicroSeconds();
}
std::cout << "Average microsecond latency is: " << latency / iter << std::endl;
latency = 0;
for (int i = 0; i < iter; ++i) {
timer.Start();
latency += timer.MilliSeconds();
}
std::cout << "Average millisecond latency is: " << latency / iter << std::endl;
}
} // namespace
} // namespace caffe2