43 mach_task_basic_info_data_t info;
44 mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
45 if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
46 reinterpret_cast<task_info_t
>(&info), &count) == KERN_SUCCESS) {
47 return info.resident_size;
50#elif defined(__linux__)
51 std::ifstream statm(
"/proc/self/statm");
54 return rss *
static_cast<size_t>(sysconf(_SC_PAGESIZE));
65 mach_task_basic_info_data_t info;
66 mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
67 if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
68 reinterpret_cast<task_info_t
>(&info), &count) == KERN_SUCCESS) {
69 return info.virtual_size;
72#elif defined(__linux__)
73 std::ifstream statm(
"/proc/self/statm");
76 return size *
static_cast<size_t>(sysconf(_SC_PAGESIZE));
98 std::cout <<
"\n========================================\n";
99 std::cout <<
" Profiling Connection Memory\n";
100 std::cout <<
" Target: " << num_connections <<
" connections\n";
101 std::cout <<
"========================================\n\n";
107 auto server = tcp.create_server({
109 .server_id =
"profile-server"
112 auto start_result = server->start(5555);
114 if (start_result.is_err()) {
115 std::cerr <<
"Failed to start server: " << start_result.error().message << std::endl;
120 std::this_thread::sleep_for(std::chrono::milliseconds(100));
122 std::vector<std::shared_ptr<interfaces::i_protocol_client>> clients;
123 clients.reserve(
static_cast<size_t>(num_connections));
127 for (
int i = 0; i < num_connections; ++i) {
128 auto client = tcp.create_client({
129 .client_id =
"client-" + std::to_string(i)
131 auto result = client->start(
"localhost", 5555);
133 if (result.is_ok()) {
134 clients.push_back(std::move(client));
136 std::cerr <<
"Failed to connect client " << i << std::endl;
139 if ((i + 1) % 50 == 0) {
145 size_t total_client_memory = after_clients_rss - before_clients_rss;
147 std::cout <<
"\n--- Connection Memory Summary ---\n";
148 std::cout <<
"Connections created: " << clients.size() <<
"\n";
149 std::cout <<
"Total memory for clients: " << (total_client_memory / 1024) <<
" KB\n";
150 if (!clients.empty()) {
151 std::cout <<
"Memory per connection: ~"
152 << (total_client_memory / clients.size() / 1024) <<
" KB\n";
156 std::cout <<
"\n--- Cleanup ---\n";
157 for (
auto& client : clients) {
158 (void)client->stop();
162 std::this_thread::sleep_for(std::chrono::milliseconds(500));
165 (void)server->stop();
166 std::this_thread::sleep_for(std::chrono::milliseconds(100));
170 std::cout <<
"\nMemory delta from baseline: "
171 <<
static_cast<long long>(final_rss - baseline_rss) / 1024 <<
" KB\n";
178 std::cout <<
"\n========================================\n";
179 std::cout <<
" Profiling Message Memory\n";
180 std::cout <<
" Target: " << num_messages <<
" messages\n";
181 std::cout <<
"========================================\n\n";
187 auto server = tcp.create_server({
189 .server_id =
"profile-server"
192 auto server_result = server->start(5556);
194 if (server_result.is_err()) {
195 std::cerr <<
"Failed to start server\n";
199 std::this_thread::sleep_for(std::chrono::milliseconds(100));
201 auto client = tcp.create_client({
202 .client_id =
"profile-client"
204 auto client_result = client->start(
"localhost", 5556);
206 if (client_result.is_err()) {
207 std::cerr <<
"Failed to connect client\n";
208 (void)server->stop();
214 auto start_time = std::chrono::steady_clock::now();
216 for (
int i = 0; i < num_messages; ++i) {
217 std::vector<uint8_t> msg(1024,
static_cast<uint8_t
>(i % 256));
218 (void)client->send(std::move(msg));
220 if ((i + 1) % 5000 == 0) {
225 auto end_time = std::chrono::steady_clock::now();
226 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
230 std::cout <<
"\n--- Message Throughput Summary ---\n";
231 std::cout <<
"Messages sent: " << num_messages <<
"\n";
232 std::cout <<
"Total time: " << duration.count() <<
" ms\n";
233 std::cout <<
"Throughput: " << (num_messages * 1000 / std::max(1LL,
static_cast<long long>(duration.count())))
237 std::this_thread::sleep_for(std::chrono::seconds(1));
241 (void)client->stop();
242 (void)server->stop();
244 std::this_thread::sleep_for(std::chrono::milliseconds(200));
248 std::cout <<
"\nMemory delta from baseline: "
249 <<
static_cast<long long>(final_rss - baseline_rss) / 1024 <<
" KB\n";
256 std::cout <<
"\n========================================\n";
257 std::cout <<
" Profiling Long-Running Stability\n";
258 std::cout <<
" Duration: " << duration_seconds <<
" seconds\n";
259 std::cout <<
"========================================\n\n";
262 size_t peak_rss = baseline_rss;
266 auto server = tcp.create_server({
268 .server_id =
"profile-server"
271 auto server_result = server->start(5557);
273 if (server_result.is_err()) {
274 std::cerr <<
"Failed to start server\n";
280 auto start_time = std::chrono::steady_clock::now();
282 int successful_cycles = 0;
284 while (std::chrono::steady_clock::now() - start_time <
285 std::chrono::seconds(duration_seconds)) {
288 auto client = tcp.create_client({
289 .client_id =
"temp-client"
291 auto result = client->start(
"localhost", 5557);
293 if (result.is_ok()) {
294 (void)client->send({
't',
'e',
's',
't'});
295 (void)client->stop();
302 if (current_rss > peak_rss) {
303 peak_rss = current_rss;
306 if (iterations % 100 == 0) {
310 std::this_thread::sleep_for(std::chrono::milliseconds(10));
313 std::cout <<
"\n--- Long-Running Summary ---\n";
314 std::cout <<
"Total iterations: " << iterations <<
"\n";
315 std::cout <<
"Successful cycles: " << successful_cycles <<
"\n";
316 std::cout <<
"Peak RSS: " << (peak_rss / (1024 * 1024)) <<
" MB\n";
318 (void)server->stop();
320 std::this_thread::sleep_for(std::chrono::milliseconds(200));
324 std::cout <<
"\nMemory delta from baseline: "
325 <<
static_cast<long long>(final_rss - baseline_rss) / 1024 <<
" KB\n";
326 std::cout <<
"Memory leak indicator: "
327 << (final_rss > baseline_rss + 1024 * 1024 ?
"POSSIBLE LEAK" :
"OK")