-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestThreadLog.cpp
More file actions
211 lines (154 loc) · 5.38 KB
/
testThreadLog.cpp
File metadata and controls
211 lines (154 loc) · 5.38 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
209
#include <chrono>
#include <thread>
#include "Shared.h"
#include "Common.h"
#include <LogEngine.h>
#include "testThreadLog.h"
CPPUNIT_TEST_SUITE_REGISTRATION( ThreadLogTest );
using namespace LogEngine;
void ThreadLogTest::setUp ()
{
}
void ThreadLogTest::tearDown ()
{
ShutdownLoggers();
}
struct ThreadInfoStruct
{
Logger *logger;
std::atomic_bool begin;
};
int testThreadProc(void* param)
{
ThreadInfoStruct *info = (ThreadInfoStruct*) param;
Logger *log = info->logger;
if(log == nullptr)
return 0; // <<< unused, so 0. (void*) 1 looks silly =)
while (!info->begin.load()) // waiting for begin
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::string s = IntToStr(GetThreadID());
log->Crit(s + " is executing 1.");
log->Error(s + " is executing 2.");
log->Info(s + " is executing 3.");
log->Warn(s + " is executing 4.");
log->Debug(s + " is executing 5.");
log->Trace(s + " is executing 6.");
log->CritFmt("{} is executing {}.", GetThreadID(), "11");
log->ErrorFmt("{} is executing {}.", GetThreadID(), "22");
log->WarnFmt("{} is executing {}.", GetThreadID(), "33");
log->LogFmt(Levels::llInfo, "{} is executing {}.", GetThreadID(), "44");
log->LogFmt(Levels::llDebug, "{} is executing {}.", GetThreadID(), "55");
log->LogFmt(Levels::llTrace, "{} is executing {}.", GetThreadID(), "66");
return 0;
}
#define nthreads 10
void ThreadLogTest::testCallLogFromManyThreads()
{
// create thread safe logger
Logger& logger = GetFileLoggerMT("testCallLogFromManyThreads", LOG_FILES_FOLDER "testCallLogFromManyThreads.log");
logger.Info("begin creating threads");
THArray<std::thread*> threads;
ThreadInfoStruct info = { &logger, false };
logger.Info("Creating threads");
for(uint i = 0; i < nthreads; i++)
{
std::thread* thr = new std::thread(testThreadProc, &info);
threads.AddValue(thr);
logger.InfoFmt("Created thread #{}", GetThreadID(thr->get_id()));
}
logger.Info("all threads created");
logger.Info("begin resuming");
info.begin.store(true);
logger.Info("all threads resumed");
// waiting till all threads finished
for (uint i = 0; i < threads.Count(); i++)
{
threads[i]->join();
}
//freeing memory
for (uint i = 0; i < threads.Count(); i++)
{
delete threads[i];
}
}
void ThreadLogTest::testAsyncLog1()
{
Logger& log = GetFileLogger("testAsyncLog1", LOG_FILES_FOLDER "testAsyncLog1.log");
std::string str("main thread ID: ");
str += IntToStr(GetThreadID());
log.Info(str);
log.SetLogLevel(Levels::llDebug);
log.SetAsyncMode(true);
log.Crit("threaded crit #1");
log.Error("threaded error #1");
log.Warn("threaded warning #1");
log.Info("threaded info #1");
log.Debug("threaded debug #1");
log.Trace("threaded trace #1");
log.SetAsyncMode(false); // this call waits till logging thread finishes i.e. saves all log events into file
}
// measure log time when AsynMode=true
void ThreadLogTest::testThreadLogMeasureTime1()
{
std::string fileName = LOG_FILES_FOLDER "testThreadLogMeasureTime1.log";
std::string loggerName = "testThreadLogMeasureTime1";
remove(fileName.c_str());
Logger& log = GetFileLoggerST(loggerName, fileName); // do not need thread safe logger here
log.SetAsyncMode(true);
auto start1 = std::chrono::high_resolution_clock::now();
const int NUM_LOGS = 100'000;
for (size_t i = 0; i < NUM_LOGS; i++)
{
log.Warn("threaded warning ###" + std::to_string(i));
}
auto stop = std::chrono::high_resolution_clock::now();
std::cout << std::endl << NUM_LOGS << " written in ASYNC mode.\n";
std::cout << " Excec time: " << MillisecToStr<std::string>(std::chrono::duration_cast<std::chrono::milliseconds>(stop - start1).count()) << std::endl;
std::fstream ff(fileName, std::ios::in);
if (!ff)
throw IOException(loggerName + " Cannot open file for reading: " + fileName);
std::cout << "verifying log file..." << std::endl;
log.WaitEmptyQueue(); // wait till separate thread writes all log messages to file
std::string ln;
size_t n;
for (size_t i = 0; i < NUM_LOGS; i++)
{
std::getline(ff, ln);
n = ln.find("###");
std::string seq = ln.substr(n + 3);
std::string si = std::to_string(i);
CPPUNIT_ASSERT_EQUAL(seq, si);
}
std::cout << "Done" << std::endl;
}
// measure log time when AsynMode=false
void ThreadLogTest::testNONThreadLogMeasureTime1()
{
std::string fileName = LOG_FILES_FOLDER "testNONThreadLogMeasureTime1.log";
remove(fileName.c_str());
Logger& log = GetFileLoggerST("testNONThreadLogMeasureTime1", fileName); // do not need thread safe logger here
auto start1 = std::chrono::high_resolution_clock::now();
const int NUM_LOGS = 100'000;
for (size_t i = 0; i < NUM_LOGS; i++)
{
log.Warn("NON-threaded warning ###" + std::to_string(i));
}
auto stop = std::chrono::high_resolution_clock::now();
std::cout << std::endl << NUM_LOGS << " written in SYNC mode.\n";
std::cout << " Excec time: " << MillisecToStr<std::string>(std::chrono::duration_cast<std::chrono::milliseconds>(stop - start1).count()) << std::endl;
std::fstream ff(fileName, std::ios::in);
if (!ff)
throw IOException("[testNONThreadLogMeasureTime1] Cannot open file for reading: " + fileName);
std::cout << "verifying log file..." << std::endl;
std::string ln;
size_t n;
for (size_t i = 0; i < NUM_LOGS; i++)
{
std::getline(ff, ln);
n = ln.find("###");
std::string seq = ln.substr(n + 3);
std::string si = std::to_string(i);
CPPUNIT_ASSERT_EQUAL(seq, si);
}
std::cout << "Done" << std::endl;
}