forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer_test.cc
59 lines (50 loc) · 1.46 KB
/
timer_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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%.
EXPECT_NEAR(ns, 100000000, 10000000);
EXPECT_NEAR(us, 100000, 10000);
EXPECT_NEAR(ms, 100, 10);
// 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