-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessing_time.h
More file actions
212 lines (180 loc) · 9.02 KB
/
processing_time.h
File metadata and controls
212 lines (180 loc) · 9.02 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
210
211
212
/* clang-format off */
/*****************************************************************************
The MIT License
Copyright © 2023 Pavel Karelin (hkarel), <hkarel@yandex.ru>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
---
Простой механизм для измерения времени выполнения циклических операций.
Декларация:
DECL_PROCESSING_TIME_BEGIN(100) // Количество итераций
DECL_PROCESSING_TIME_LABEL(10) // Метки для вывода в лог 10, 20, 30
DECL_PROCESSING_TIME_LABEL(20)
DECL_PROCESSING_TIME_LABEL(30)
DECL_PROCESSING_TIME_END
Рабочий код:
while (true) {
RESET_PROCESSING_TIME
...
LOG_PROCESSING_TIME(10)
...
LOG_PROCESSING_TIME(20)
...
LOG_PROCESSING_TIME(30)
INDEXUP_PROCESSING_TIME
}
*****************************************************************************/
#pragma once
#include "shared/utils.h"
#include "shared/steady_timer.h"
#include "shared/logger/logger.h"
#include <vector>
#include <stdint.h>
#define DECL_PROCESSING_TIME_BEGIN(COUNT) \
struct { \
bool enabled = {true}; \
size_t index = {0}; \
size_t count = {COUNT}; \
steady_timer timer; \
struct time_array : std::vector<int64_t> \
{ \
time_array(size_t num) {reserve(num);} \
};
#define DECL_PROCESSING_TIME_LABEL(LABEL) \
time_array test##LABEL {count};
#define DECL_PROCESSING_TIME_END \
} __processing_time__;
#define ENABLED_PROCESSING_TIME(VAL) \
__processing_time__.enabled = VAL;
#define RESET_PROCESSING_TIME \
if (__processing_time__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
__processing_time__.timer.reset(); \
}
#define TRACE_PROCESSING_TIME(LABEL) \
if (__processing_time__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
__processing_time__.test##LABEL.push_back(__processing_time__.timer.elapsed<chrono::microseconds>()); \
}
#define PRINT_PROCESSING_TIME(LABEL) \
if (__processing_time__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
if (__processing_time__.index == (__processing_time__.count - 1)) { \
if (__processing_time__.test##LABEL.size()) \
log_debug2_m << "Processing time (average) "#LABEL << ": " \
<< alog::round(utl::average(__processing_time__.test##LABEL), 3) << " us"; \
else \
log_debug2_m << "Processing time (average) "#LABEL << ": none"; \
__processing_time__.test##LABEL.clear(); \
}}
/**
LOG_PROCESSING_TIME объединяет два макроса: TRACE_PROCESSING_TIME и PRINT_PROCESSING_TIME
*/
#define LOG_PROCESSING_TIME(LABEL) \
if (__processing_time__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
/* TRACE_PROCESSING_TIME */ \
__processing_time__.test##LABEL.push_back(__processing_time__.timer.elapsed<chrono::microseconds>()); \
/* PRINT_PROCESSING_TIME */ \
if (__processing_time__.index == (__processing_time__.count - 1)) { \
if (__processing_time__.test##LABEL.size()) \
log_debug2_m << "Processing time (average) "#LABEL << ": " \
<< alog::round(utl::average(__processing_time__.test##LABEL), 3) << " us"; \
else \
log_debug2_m << "Processing time (average) "#LABEL << ": none"; \
__processing_time__.test##LABEL.clear(); \
}}
#define INDEXUP_PROCESSING_TIME \
if (__processing_time__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
if (++__processing_time__.index == __processing_time__.count) \
__processing_time__.index = 0; \
}
//----------------------------------------------------------------------------
#define DECL_PROCESSING_TIME2_BEGIN(TIMEOUT /*сек*/) \
struct { \
bool enabled = {true}; \
bool print_times = {true}; \
int64_t timeout = {TIMEOUT*1000*1000}; \
int64_t diff_sum = {0}; \
steady_timer print_timer; \
steady_timer timer; \
steady_timer diff_timer; \
struct time_array : std::vector<int64_t> \
{ \
time_array(size_t num) {reserve(num);} \
};
#define DECL_PROCESSING_TIME2_LABEL(LABEL) \
time_array test##LABEL {500};
#define DECL_PROCESSING_TIME2_END \
} __processing_time2__;
#define ENABLED_PROCESSING_TIME2(VAL) \
__processing_time2__.enabled = VAL;
#define BEGIN_PROCESSING_TIME2 \
if (__processing_time2__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
if (__processing_time2__.print_times) { \
__processing_time2__.print_timer.reset(); \
__processing_time2__.print_times = false; \
} \
__processing_time2__.diff_sum = 0; \
__processing_time2__.timer.reset(); \
if (__processing_time2__.print_timer.elapsed<chrono::microseconds>() > __processing_time2__.timeout) \
__processing_time2__.print_times = true; \
}
#define TRACE_PROCESSING_TIME2(LABEL) \
if (__processing_time2__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
__processing_time2__.test##LABEL.push_back(__processing_time2__.timer.elapsed<chrono::microseconds>()); \
}
#define PRINT_PROCESSING_TIME2(LABEL) \
if (__processing_time2__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
if (__processing_time2__.print_times) { \
if (__processing_time2__.test##LABEL.size()) \
log_debug2_m << "Processing time (average) "#LABEL << ": " \
<< alog::round(utl::average(__processing_time2__.test##LABEL), 3) << " us"; \
else \
log_debug2_m << "Processing time (average) "#LABEL << ": none"; \
__processing_time2__.test##LABEL.clear(); \
}}
/**
LOG_PROCESSING_TIME2 объединяет два макроса: TRACE_PROCESSING_TIME2 и PRINT_PROCESSING_TIME2
*/
#define LOG_PROCESSING_TIME2(LABEL) \
if (__processing_time2__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
/* TRACE_PROCESSING_TIME2 */ \
__processing_time2__.test##LABEL.push_back(__processing_time2__.timer.elapsed<chrono::microseconds>()); \
/* PRINT_PROCESSING_TIME2 */ \
if (__processing_time2__.print_times) { \
if (__processing_time2__.test##LABEL.size()) \
log_debug2_m << "Processing time (average) "#LABEL << ": " \
<< alog::round(utl::average(__processing_time2__.test##LABEL), 3) << " us"; \
else \
log_debug2_m << "Processing time (average) "#LABEL << ": none"; \
__processing_time2__.test##LABEL.clear(); \
}}
#define BEGIN_PROCESSING_DIFF_TIME2 \
if (__processing_time2__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
__processing_time2__.diff_sum = 0; \
__processing_time2__.diff_timer.reset(); \
}
#define TRACE_PROCESSING_DIFF_TIME2(LABEL, ITERATION_COUNT) \
if (__processing_time2__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
int64_t t = __processing_time2__.diff_timer.elapsed<chrono::microseconds>(); \
__processing_time2__.test##LABEL.push_back(t * ITERATION_COUNT); \
__processing_time2__.diff_sum += t; \
__processing_time2__.diff_timer.reset(); \
}
#define TRACE_PROCESSING_DIFF_SUM2(LABEL, ITERATION_COUNT) \
if (__processing_time2__.enabled && (alog::logger().level() == alog::Level::Debug2)) { \
__processing_time2__.test##LABEL.push_back(__processing_time2__.diff_sum * ITERATION_COUNT); \
}
#define PRINT_PROCESSING_DIFF_TIME2(LABEL) PRINT_PROCESSING_TIME2(LABEL)
#define PRINT_PROCESSING_DIFF_SUM2(LABEL) PRINT_PROCESSING_TIME2(LABEL)