11 #include <unordered_map> 14 namespace traffic_manager {
16 namespace chr = std::chrono;
18 using TimePoint = chr::time_point<chr::system_clock, chr::nanoseconds>;
34 TimePoint current_time = chr::system_clock::now();
36 if (print_clocks.find(snippet_name) == print_clocks.end()) {
37 print_clocks.insert({snippet_name, current_time});
39 if (snippet_clocks.find(snippet_name) == snippet_clocks.end()) {
40 snippet_clocks.insert({snippet_name, current_time});
42 if (snippet_durations.find(snippet_name) == snippet_durations.end()) {
43 snippet_durations.insert({snippet_name, chr::duration<float>()});
45 if (number_of_calls.find(snippet_name) == number_of_calls.end()) {
46 number_of_calls.insert({snippet_name, 0u});
49 TimePoint &print_clock = print_clocks.at(snippet_name);
50 TimePoint &snippet_clock = snippet_clocks.at(snippet_name);
51 chr::duration<float> &snippet_duration = snippet_durations.at(snippet_name);
52 unsigned long &call_count = number_of_calls.at(snippet_name);
55 snippet_clock = current_time;
57 chr::duration<float> measured_duration = current_time - snippet_clock;
58 snippet_duration += measured_duration;
62 chr::duration<float> print_duration = current_time - print_clock;
63 if (print_duration.count() > 1.0f) {
64 call_count = call_count == 0u ? 1 : call_count;
65 std::cout <<
"Snippet name : " << snippet_name <<
", " 66 <<
"avg. duration : " << 1000 * snippet_duration.count() / call_count <<
" ms, " 67 <<
"total duration : " << snippet_duration.count() <<
" s, " 68 <<
"total calls : " << call_count <<
", " 71 snippet_duration = 0s;
74 print_clock = current_time;
std::unordered_map< std::string, chr::duration< float > > snippet_durations
std::unordered_map< std::string, unsigned long > number_of_calls
chr::time_point< chr::system_clock, chr::nanoseconds > TimePoint
This file contains definitions of common data structures used in traffic manager. ...
std::unordered_map< std::string, TimePoint > snippet_clocks
void MeasureExecutionTime(std::string snippet_name, bool begin_or_end)
std::unordered_map< std::string, TimePoint > print_clocks