forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathtimer_test.cpp
More file actions
209 lines (181 loc) · 5.98 KB
/
timer_test.cpp
File metadata and controls
209 lines (181 loc) · 5.98 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "../timer.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <fstream>
#include <cstdio>
#include <chrono>
#include <thread>
#ifdef __MPI
#include "mpi.h"
#endif
/************************************************
* unit test of class timer
***********************************************/
/**
* - Tested Functions:
* - Tick
* - tick 1st time, set start_flag to false
* - tick 2nd time, calculate time duration
* - Start
* - start total time calculation
* - PrintAll
* - print computational processes with time > 0.1 s
* - Finish
* - finish total time calculation
* - print computational processes with time > 0.1 s
* - PrintUntilNow
* - stop total time calculation
* - print total time until now
* - then start total time calculation again
*/
class TimerTest : public testing::Test
{
protected:
//
// for capturing stdout
std::string output;
// for output in file
std::ofstream ofs;
std::ifstream ifs;
int T_Elapse = 100; // microseconds = 0.1 milliseconds
void TearDown()
{
remove("tmp");
}
};
TEST_F(TimerTest, Tick)
{
ModuleBase::timer::start("wavefunc","evc");
// after 1st call of tick, start_flag becomes false
EXPECT_FALSE(ModuleBase::timer::timer_pool["wavefunc"]["evc"].start_flag);
std::this_thread::sleep_for(std::chrono::microseconds(T_Elapse)); // 0.1 ms
// then we can have time elapsed in cpu_second
ModuleBase::timer::end("wavefunc","evc");
EXPECT_GT(ModuleBase::timer::timer_pool["wavefunc"]["evc"].cpu_second,0.0001);
}
TEST_F(TimerTest, Start)
{
ModuleBase::timer::start();
// start() called tick() once
EXPECT_FALSE(ModuleBase::timer::timer_pool[""]["total"].start_flag);
ModuleBase::timer::end("","total");
}
TEST_F(TimerTest, write_to_json)
{
ModuleBase::timer::start("wavefunc","evc");
std::this_thread::sleep_for(std::chrono::microseconds(T_Elapse)); // 0.1 ms
ModuleBase::timer::end("wavefunc","evc");
ModuleBase::timer::write_to_json("tmp.json");
// check if tmp.json exists
ifs.open("tmp.json");
EXPECT_TRUE(ifs.good());
// read all lines and remove all spaces and tabs and newlines
std::string line;
std::string tmp;
std::string content;
while(getline(ifs,line))
{
tmp = line;
tmp.erase(std::remove(tmp.begin(),tmp.end(),' '),tmp.end());
tmp.erase(std::remove(tmp.begin(),tmp.end(),'\t'),tmp.end());
tmp.erase(std::remove(tmp.begin(),tmp.end(),'\n'),tmp.end());
content += tmp;
}
EXPECT_THAT(content,testing::HasSubstr("\"total\":"));
EXPECT_THAT(content,testing::HasSubstr("\"sub\":[{\"class_name\":\"wavefunc\",\"sub\":[{\"name\":\"evc\",\"cpu_second\":"));
EXPECT_THAT(content,testing::HasSubstr("\"calls\":2,\"cpu_second_per_call\":"));
EXPECT_THAT(content,testing::HasSubstr("\"cpu_second_per_total\":"));
EXPECT_THAT(content,testing::HasSubstr("}]}]}"));
ifs.close();
remove("tmp.json");
}
TEST_F(TimerTest, PrintAll)
{
ModuleBase::timer::start("wavefunc","evc");
std::this_thread::sleep_for(std::chrono::microseconds(T_Elapse)); // 0.1 ms
ModuleBase::timer::end("wavefunc","evc");
// call print_all
ofs.open("tmp");
testing::internal::CaptureStdout();
ModuleBase::timer::print_all(ofs, true);
output = testing::internal::GetCapturedStdout();
ofs.close();
// checout output on screen
// std::cout << "Get captured stdout: \n" << std::endl;
// std::cout << output << std::endl;
EXPECT_THAT(output,testing::HasSubstr("TIME STATISTICS"));
EXPECT_THAT(output,testing::HasSubstr("CLASS_NAME"));
EXPECT_THAT(output,testing::HasSubstr("NAME"));
EXPECT_THAT(output,testing::HasSubstr("TIME/s"));
EXPECT_THAT(output,testing::HasSubstr("CALLS"));
EXPECT_THAT(output,testing::HasSubstr("AVG/s"));
EXPECT_THAT(output,testing::HasSubstr("PER/%"));
// check output in file
ifs.open("tmp");
// std::cout << "Capture contents line by line from output file: \n" << std::endl;
getline(ifs,output);
getline(ifs,output);
EXPECT_THAT(output,testing::HasSubstr("TIME STATISTICS"));
getline(ifs,output);
getline(ifs,output);
EXPECT_THAT(output,testing::HasSubstr("CLASS_NAME"));
EXPECT_THAT(output,testing::HasSubstr("NAME"));
EXPECT_THAT(output,testing::HasSubstr("TIME/s"));
EXPECT_THAT(output,testing::HasSubstr("CALLS"));
EXPECT_THAT(output,testing::HasSubstr("AVG/s"));
EXPECT_THAT(output,testing::HasSubstr("PER/%"));
ifs.close();
remove("time.json");
}
TEST_F(TimerTest, PrintUntilNow)
{
long double time = ModuleBase::timer::print_until_now();
EXPECT_GE(time, 0.0);
}
TEST_F(TimerTest, Finish)
{
ModuleBase::timer::start("wavefunc","evc");
std::this_thread::sleep_for(std::chrono::microseconds(T_Elapse)); // 0.1 ms
ModuleBase::timer::end("wavefunc","evc");
// call print_all
ofs.open("tmp");
testing::internal::CaptureStdout();
ModuleBase::timer::finish(ofs);
output = testing::internal::GetCapturedStdout();
ofs.close();
// checout output on screen
//std::cout << "Get captured stdout: \n" << std::endl;
//std::cout << output << std::endl;
EXPECT_THAT(output,testing::HasSubstr("TIME STATISTICS"));
EXPECT_THAT(output,testing::HasSubstr("CLASS_NAME"));
EXPECT_THAT(output,testing::HasSubstr("NAME"));
EXPECT_THAT(output,testing::HasSubstr("TIME/s"));
EXPECT_THAT(output,testing::HasSubstr("CALLS"));
EXPECT_THAT(output,testing::HasSubstr("AVG/s"));
EXPECT_THAT(output,testing::HasSubstr("PER/%"));
// check output in file
ifs.open("tmp");
//std::cout << "Capture contents line by line from output file: \n" << std::endl;
getline(ifs,output);
getline(ifs,output);
EXPECT_THAT(output,testing::HasSubstr("TIME STATISTICS"));
getline(ifs,output);
getline(ifs,output);
EXPECT_THAT(output,testing::HasSubstr("CLASS_NAME"));
EXPECT_THAT(output,testing::HasSubstr("NAME"));
EXPECT_THAT(output,testing::HasSubstr("TIME/s"));
EXPECT_THAT(output,testing::HasSubstr("CALLS"));
EXPECT_THAT(output,testing::HasSubstr("AVG/s"));
EXPECT_THAT(output,testing::HasSubstr("PER/%"));
ifs.close();
}
#ifdef __MPI
int main(int argc, char **argv)
{
MPI_Init(&argc,&argv);
testing::InitGoogleTest(&argc,argv);
int result = RUN_ALL_TESTS();
MPI_Finalize();
return result;
}
#endif