-
Notifications
You must be signed in to change notification settings - Fork 9
/
ToolsTestStopWatch.cpp
147 lines (117 loc) · 5.17 KB
/
ToolsTestStopWatch.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "ToolsTestStopWatch.h"
#include "StopWatch.h"
#include "ThreadSafeStopWatch.h"
#include <chrono>
#include <thread>
TEST_F(ToolsTestStopWatch, UsSimple) {
auto preStart = std::chrono::steady_clock::now();
StopWatch watch{};
auto elapsedUs = watch.ElapsedUs();
auto preElapsedUs = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - preStart).count());
EXPECT_TRUE(elapsedUs <= preElapsedUs) << "elapsedUs:" << elapsedUs << ", preElapsedUs:" << preElapsedUs;
}
TEST_F(ToolsTestStopWatch, UsSimpleAfterSleep) {
auto preStart = std::chrono::steady_clock::now();
StopWatch watch;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto elapsedUs = watch.ElapsedUs();
auto preElapsedUs = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - preStart).count());
EXPECT_TRUE(elapsedUs <= preElapsedUs) << "elapsedUs:" << elapsedUs << ", preElapsedUs:" << preElapsedUs;
EXPECT_TRUE(elapsedUs >= 1000 * 100);
}
TEST_F(ToolsTestStopWatch, MsSimpleAfterSleep) {
auto preStart = std::chrono::steady_clock::now();
StopWatch watch;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto elapsedMs = watch.ElapsedMs();
auto preElapsedMs = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - preStart).count());
EXPECT_TRUE(elapsedMs <= preElapsedMs) << "elapsedUs:" << elapsedMs << ", preElapsedMs:" << preElapsedMs;
EXPECT_TRUE(elapsedMs >= 100);
}
TEST_F(ToolsTestStopWatch, SecSimpleAfterSleep) {
auto preStart = std::chrono::steady_clock::now();
StopWatch watch;
std::this_thread::sleep_for(std::chrono::seconds(1));
auto elapsedSec = watch.ElapsedSec();
auto preElapsedSec = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - preStart).count());
EXPECT_TRUE(elapsedSec <= preElapsedSec) << "elapsedSec:" << elapsedSec << ", preElapsedSec:" << preElapsedSec;
EXPECT_TRUE(elapsedSec >= 1);
}
TEST_F(ToolsTestStopWatch, Restart) {
auto pre = std::chrono::steady_clock::now();
StopWatch watch;
auto restart = watch.Restart();
EXPECT_TRUE(restart >= pre); // can be greater than previous
std::this_thread::sleep_for(std::chrono::milliseconds(100));
restart = watch.Restart();
EXPECT_TRUE(restart > pre); // must be greater than previous
}
TEST_F(ToolsTestStopWatch, Comparisons) {
StopWatch watch{};
std::this_thread::sleep_for(std::chrono::seconds(1));
auto elapsedNs = watch.ElapsedNs();
auto elapsedUs = watch.ElapsedUs();
auto elapsedMs = watch.ElapsedMs();
auto elapsedSec = watch.ElapsedSec();
// compare with itself
EXPECT_TRUE(elapsedNs / 1000 <= elapsedUs) << "ns: " << elapsedUs << ". us: " << elapsedUs;
EXPECT_TRUE(elapsedUs / 1000 <= elapsedMs) << "us: " << elapsedUs << ". ms: " << elapsedMs;
EXPECT_TRUE(elapsedMs / 1000 <= elapsedSec);
EXPECT_TRUE(elapsedUs >= 1000000);
EXPECT_TRUE(elapsedMs >= 1000);
EXPECT_TRUE(elapsedSec >= 1);
}
TEST_F(ToolsTestStopWatch, PrecisionComparisons) {
PrecisionStopWatch watch{};
std::this_thread::sleep_for(std::chrono::seconds(1));
auto elapsedNs = watch.ElapsedNs();
auto elapsedUs = watch.ElapsedUs();
auto elapsedMs = watch.ElapsedMs();
auto elapsedSec = watch.ElapsedSec();
// compare with itself
EXPECT_TRUE(elapsedNs / 1000 <= elapsedUs) << "ns: " << elapsedUs << ". us: " << elapsedUs;
EXPECT_TRUE(elapsedUs / 1000 <= elapsedMs) << "us: " << elapsedUs << ". ms: " << elapsedMs;
EXPECT_TRUE(elapsedMs / 1000 <= elapsedSec);
EXPECT_TRUE(elapsedUs >= 1000000);
EXPECT_TRUE(elapsedMs >= 1000);
EXPECT_TRUE(elapsedSec >= 1);
}
TEST_F(ToolsTestStopWatch, ComparisonsWithOld) {
StopWatch watch;
auto oldStart = std::time(NULL);
std::this_thread::sleep_for(std::chrono::seconds(1));
auto oldElapsed = static_cast<uint64_t>(std::time(NULL) - oldStart);
auto elapsedSec = watch.ElapsedSec();
EXPECT_TRUE(elapsedSec >= oldElapsed);
EXPECT_TRUE(elapsedSec >= 1);
EXPECT_TRUE(oldElapsed >= 1);
}
/* ThreadSafeStopWatch tests*/
TEST_F(ToolsTestStopWatch, ThreadSafeSecSimpleAfterSleep) {
ThreadSafeStopWatch threadSafeWatch;
auto elapsedSec = threadSafeWatch.GetStopWatch().ElapsedSec();
std::this_thread::sleep_for(std::chrono::seconds(1));
elapsedSec = threadSafeWatch.GetStopWatch().ElapsedSec();
EXPECT_TRUE(elapsedSec >= 1);
}
void StopWatchThread() {
/* Start stop watch */
ThreadSafeStopWatch threadSafeWatch;
auto elapsedSec = threadSafeWatch.GetStopWatch().ElapsedSec();
std::this_thread::sleep_for(std::chrono::seconds(5));
elapsedSec = threadSafeWatch.GetStopWatch().ElapsedSec();
EXPECT_TRUE(elapsedSec >= 5);
}
TEST_F(ToolsTestStopWatch, ThreadSafeSecBasicTest) {
std::vector<std::thread*> threads;
int threadCount = 5;
for (int i = 0; i < threadCount; i++) {
std::this_thread::sleep_for(std::chrono::seconds(1));
threads.push_back(new std::thread(StopWatchThread));
}
for (auto jt = threads.begin(); jt != threads.end(); jt++) {
(*jt)->join();
delete *jt;
}
threads.clear();
}