File: timer_test.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (65 lines) | stat: -rw-r--r-- 1,647 bytes parent folder | download
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
60
61
62
63
64
65
#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