pytorch/caffe2/core/timer_test.cc
Nikita Shulga 3a66a1cb99 [clang-tidy] Exclude cppcoreguidelines-avoid-magic-numbers (#57841)
Summary:
Add cppcoreguidelines-avoid-magic-numbers exclusion to clang-tidy
Remove existing nolint warnings using following script:
```
for file in `git ls-files | grep -v \.py`; do gsed '/^ *\/\/ NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)/d' -i  $file; done
```

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

Reviewed By: samestep

Differential Revision: D28295045

Pulled By: malfet

fbshipit-source-id: 7c6e8d1213c9593f169ed3df6a916498f1a97163
2021-05-07 20:02:33 -07:00

67 lines
1.7 KiB
C++

#include <chrono>
#include <iostream>
#include <thread>
#include "caffe2/core/timer.h"
#include <gtest/gtest.h>
namespace caffe2 {
namespace {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
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);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
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