9 #ifdef EASYLOGGINGPP_ASYNC 11 std::atomic<int> atomic_integer(0);
12 std::chrono::milliseconds max_time = (std::chrono::milliseconds)0;
13 std::chrono::milliseconds min_time = (std::chrono::milliseconds)50;
14 std::chrono::milliseconds avg_time = (std::chrono::milliseconds)0;
15 int max_time_iteration = -1;
16 const int number_of_iterations = 10000;
17 const int number_of_threads = 10;
18 std::mutex stats_mutex;
20 const int checked_log_write_time = 10;
21 const int required_log_write_time = 50;
22 const int checked_lag_time = 15;
23 const int required_lag_time = 50;
29 auto time_word = line.substr(7, 12);
30 auto hours =
stoi(time_word.substr(0, 2));
31 auto minutes =
stoi(time_word.substr(3, 5));
32 auto seconds =
stoi(time_word.substr(6, 8));
33 auto ms =
stoi(time_word.substr(9, 12));
35 return (ms + seconds * 1000 + minutes * 1000 * 60 + hours * 1000 * 60 * 60);
42 for (
int i = 0;
i < (number_of_iterations * number_of_threads) - 1; ++
i)
54 int get_log_ideal_time(
int first_time_ms,
int first_value,
int second_time_ms,
int second_value,
int value_in_log)
56 int ideal_time = value_in_log * (second_time_ms - first_time_ms) / (second_value - first_value) +
57 (first_time_ms * (second_value / (second_value - first_value))) - second_time_ms * (first_value / (second_value - first_value));
62 std::pair<unsigned int, int> get_next_50_lines_time_val(std::ifstream& ifs)
64 unsigned int avg_time_ms = -1;
66 for (
int i = 0;
i < 50; ++
i) {
69 avg_time_ms += get_time_from_line(line);
70 avg_value +=
stoi(line.substr(line.find_last_of(
" ") + 1));
74 return std::make_pair(avg_time_ms, avg_value);
79 TEST_CASE(
"async logger",
"[log][async_log]")
81 size_t n_callbacks = 0;
88 auto func = [](
int required_value) {
91 while (iterations < number_of_iterations)
94 ss <<
"atomic integer = " << ++atomic_integer;
98 std::chrono::milliseconds delta_ms = ms - start_ms;
100 std::lock_guard<std::mutex>
lock(stats_mutex);
101 if (delta_ms > max_time) max_time = delta_ms;
102 if (delta_ms < min_time) min_time = delta_ms;
103 avg_time += delta_ms;
108 const char* log_file_path =
".//multi-thread-logs.log";
111 remove(log_file_path);
116 std::vector<std::thread> threads;
118 for (
int i = 0;
i < number_of_threads; ++
i)
120 threads.push_back(std::thread(
func,
i + 1));
123 for (
auto&&
t : threads)
125 if (
t.joinable())
t.join();
128 std::this_thread::sleep_for(std::chrono::milliseconds(5000));
129 std::stringstream ss;
135 CHECK_NOFAIL(max_time.count() < checked_log_write_time);
136 REQUIRE(max_time.count() < required_log_write_time);
141 std::array<char, number_of_iterations * number_of_threads> check_table;
143 memset(&check_table[0], 0, check_table.size());
146 if (check_file.good())
149 while (std::getline(check_file, line))
151 auto value_in_log =
stoi(line.substr(line.find_last_of(
" ")));
153 REQUIRE((value_in_log - 1) < (number_of_iterations* number_of_threads));
154 check_table.at(value_in_log - 1) = 1;
157 std::for_each(check_table.cbegin(), check_table.cend(), [](
const char c) {
168 int overall_logs = 0;
169 if (check_file.good())
174 auto first_50_pair = get_next_50_lines_time_val(check_file);
175 unsigned int first_50_avg_time_ms = first_50_pair.first;
176 int first_50_avg_value = first_50_pair.second;
181 for (
int i = 0;
i < (number_of_iterations * number_of_threads) - 1 - 50 - 50; ++
i)
182 getline(check_file, line);
184 auto last_50_pair = get_next_50_lines_time_val(check_file);
185 unsigned int last_50_avg_time_ms = last_50_pair.first;
186 int last_50_avg_value = last_50_pair.second;
194 while (getline(check_file, line))
196 auto value_in_log =
stoi(line.substr(line.find_last_of(
" ")));
197 auto log_time_ms = get_time_from_line(line);
198 auto log_ideal_time_ms = get_log_ideal_time(first_50_avg_time_ms, first_50_avg_value, last_50_avg_time_ms, last_50_avg_value, value_in_log);
199 auto delta = std::abs(log_ideal_time_ms - log_time_ms);
200 if (delta > max_delta) max_delta = delta;
205 REQUIRE(max_delta < required_lag_time);
214 int overall_logs = 0;
215 if (check_file.good())
218 while (std::getline(check_file, line))
220 auto previous_time = get_time_from_line(line);
221 int logs_counter_in_one_ms = 1;
222 while (std::getline(check_file, line))
227 overall_logs += logs_counter_in_one_ms;
232 ++logs_counter_in_one_ms;
237 float average_logs_in_one_ms = (float)overall_logs / overall_ms;
238 REQUIRE(average_logs_in_one_ms >= 20.0F);
static const textual_icon lock
GLenum GLuint GLenum severity
void log(rs2_log_severity severity, const char *message)
GLsizei const GLchar *const * string
void log_to_file(rs2_log_severity min_severity, const char *file_path=nullptr)
#define CHECK_NOFAIL(...)
int stoi(const std::string &value)
rs2_log_severity
Severity of the librealsense logger.