srs-control 0.2.2
Loading...
Searching...
No Matches
AppReport.cpp
Go to the documentation of this file.
1#include "AppReport.hpp"
3#include <algorithm>
4#include <cstddef>
5#include <format>
6#include <limits>
7#include <ranges>
8#include <spdlog/spdlog.h>
9#include <sstream>
10#include <string>
11#include <tabulate/color.hpp>
12#include <tabulate/font_align.hpp>
13#include <tabulate/font_style.hpp>
14#include <tabulate/table.hpp>
15#include <tabulate/termcolor.hpp>
16#include <type_traits>
17#include <vector>
18
19namespace srs
20{
21 namespace
22 {
23 using Row = tabulate::Table::Row_t;
24
25 void format_solid_border(tabulate::Table& table)
26 {
27 table.format().border_top("─").border_bottom("─").border_left("│").border_right("│");
28 }
29
30 auto format_records(const auto& records, const Row& title_names, auto row_adder) -> std::string
31 {
32 using namespace tabulate;
33
34 auto content_table = tabulate::Table{};
35 format_solid_border(content_table);
36
37 content_table.add_row(title_names);
38 for (auto& cell : content_table.row(0))
39 {
40 cell.format().font_color(Color::yellow).font_align(FontAlign::center).font_style({ FontStyle::bold });
41 }
42
43 using native_type = std::remove_cvref_t<decltype(records)>;
45 {
46 if (records.empty())
47 {
48 return std::string{};
49 }
50 for (const auto& [name, split_record] : records)
51 {
52 if constexpr (is_vector_type<std::remove_cvref_t<decltype(split_record)>>::value)
53 {
54 for (const auto [n_pipe, record] : std::views::zip(std::views::iota(0), split_record))
55 {
56 if (n_pipe != 0)
57 {
58 auto row = Row{ "" };
59 row_adder(row, n_pipe, record);
60 content_table.add_row(row);
61 content_table.row(content_table.size() - 1).format().border_top("-");
62 content_table[content_table.size() - 1][0].format().border_top(" ");
63 }
64 else
65 {
66
67 auto row = Row{ name };
68 row_adder(row, n_pipe, record);
69 content_table.add_row(row);
70 }
71 }
72 }
73 else
74 {
75 auto row = Row{ name };
76 row_adder(row, split_record);
77 content_table.add_row(row);
78 }
79 }
80 }
81 else
82 {
83 auto row = Row{ records.first };
84 row_adder(row, records.second);
85 content_table.add_row(row);
86 }
87
88 content_table.column(0).format().font_align(FontAlign::left);
89
90 for (const auto [idx, _] :
91 std::views::zip(std::views::iota(std::size_t{}), title_names) | std::views::drop(1))
92 {
93 content_table.column(idx).format().font_align(FontAlign::center);
94 }
95
96 auto sstream = std::stringstream{};
97 sstream << termcolor::colorize << content_table;
98 return sstream.str();
99 }
100 } // namespace
101
103 {
104 auto str = format_records(sink_records_,
105 {
106 "Sink name",
107 "Split",
108 "Bytes written (KB)",
109 },
110 [](Row& row, int idx, std::size_t bytes)
111 {
112 const auto kb_written = bytes == 0 ? std::numeric_limits<double>::quiet_NaN()
113 : static_cast<double>(bytes) / 1000.;
114 row.emplace_back(std::format("{}", idx));
115 row.emplace_back(std::format("{:.1f}", kb_written));
116 });
117 spdlog::debug("Bytes written to output sinks:\n{}", str);
118 }
119
121 {
122 auto str = format_records(task_records_,
123 {
124 "Task name",
125 "Split",
126 "Total time (ms)",
127 "Average time (ns)",
128 "Frames read",
129 },
130 [](Row& row, int idx, const TaskStat& stat)
131 {
132 const auto total_time_ms = stat.total_time_ms;
133 const auto ave_time =
134 stat.total_time_ms == 0
135 ? 0.
136 : 1e6 * stat.total_time_ms / static_cast<double>(stat.total_sample_size);
137 row.push_back(std::format("{}", idx));
138 row.push_back(std::format("{:.1f}", total_time_ms));
139 row.push_back(std::format("{:.1f}", ave_time));
140 row.push_back(std::format("{}", stat.total_sample_size));
141 });
142
143 spdlog::debug("Performance report from required tasks:\n{}", str);
144 }
145
147 {
148 for (auto& [socket_name, stats] : switch_socket_records_)
149 {
150 std::ranges::sort(stats,
151 [](const auto& left, const auto& right) -> bool
152 { return left.second.send_time < right.second.send_time; });
153 if (stats.empty())
154 {
155 continue;
156 }
157 const auto first_time = stats.front().second.send_time;
158 for (auto& [_, stat] : stats)
159 {
160 stat.send_time_offset = stat.send_time - first_time;
161 }
162 }
163
164 auto str = format_records(switch_socket_records_,
165 {
166 "Local socket",
167 "Remote socket",
168 "connection",
169 "Send time (us)",
170 "Receiv latency (us)",
171 },
172 [](Row& row, int, const auto& stats)
173 {
174 const auto& [remote_socket, stat] = stats;
175 row.push_back(std::format("{}", remote_socket));
176 row.push_back(std::format("{}", stat.connection_name));
177 row.push_back(std::format("{} (+{})", stat.send_time, stat.send_time_offset));
178 row.push_back(std::format("{}", stat.receiv_time - stat.send_time));
179 });
180 spdlog::debug("Performance report from sockets:\n{}", str);
181 }
182
184 {
185 auto str = format_records(frame_reading_records_,
186 {
187 "Actor",
188 "Avg. time (ns/frame)",
189 "Avg. bytes (/frame)",
190 "Frames",
191 },
192 [](Row& row, const auto& stat)
193 {
194 const auto avg_time =
195 static_cast<double>(stat.total_time_ns) / static_cast<double>(stat.n_frames);
196
197 const auto bytes_per_frame = static_cast<double>(stat.total_bytes_read) /
198 static_cast<double>(stat.n_frames);
199 row.push_back(std::format("{:.1f}", avg_time));
200 row.push_back(std::format("{:.0f}", bytes_per_frame));
201 row.push_back(std::format("{}", stat.n_frames));
202 });
203 spdlog::debug("Performance report from frame reading processes:\n{}", str);
204 }
205
207 {
208 auto str = format_records(queue_record_,
209 {
210 "Causes of failure",
211 "Empty trash queue",
212 "Full trash queue",
213 "Full valid queue",
214 },
215 [](Row& row, const auto& stat)
216 {
217 row.push_back(std::format("{}", stat.empty_trash));
218 row.push_back(std::format("{}", stat.full_trash));
219 row.push_back(std::format("{}", stat.full_valid));
220 });
221 spdlog::debug("Buffer Queue report:\n{}", str);
222 }
223
232} // namespace srs
std::vector< std::pair< std::string, FrameReadingStat > > frame_reading_records_
Definition AppReport.hpp:76
std::pair< std::string_view, QueueStat > queue_record_
Definition AppReport.hpp:77
void report_buffer_result()
std::vector< std::pair< std::string, std::vector< std::pair< std::string, FecSwitchStat > > > > switch_socket_records_
Definition AppReport.hpp:75
void report_task_result()
std::map< std::string, std::vector< TaskStat > > task_records_
Definition AppReport.hpp:73
std::map< std::string, std::vector< std::size_t > > sink_records_
Definition AppReport.hpp:74
void report_socket_result()
void report_sink_file_result()
void report_frame_reading_result()