Skip to the content.

< Previous: Advanced Usage | 🏠 Home | Next: Architecture & Examples >


Performance Metrics and Throughput Tracking

cpp-tcpnet includes built-in support for monitoring performance, allowing you to track data transfer volumes, packet counts, active connection counts, and real-time throughput.

The library offers two complementary tracking models:

  1. Direct Stats (Atomic Counters): Low-overhead, synchronous snapshots of cumulative counts.
  2. Throughput Tracking (PubSub Event-Driven): An asynchronous sliding-window tracker powered by a background worker.

1. Direct Stats (Atomic Counters)

Both TcpListener and TcpClient maintain atomic counters of data transmitted and received. These stats can be queried synchronously at any time using the .GetStats() method.

Listener Stats

TcpListener::GetStats() returns a ListenerStats struct:

Example usage:

cpptcpnet::ListenerStats stats = server.GetStats();
std::cout << "Active Clients: " << stats.active_connections << "\n"
          << "Total Received: " << stats.bytes_received << " bytes ("
          << stats.packets_received << " reads)\n";

Client Stats

TcpClient::GetStats() returns a ClientStats struct:


2. Event-Driven Throughput Tracking

Whenever data is sent or received, the network loops publish a TransferEvent onto the "transfer_events" topic of the internal PubSub broker.

struct TransferEvent {
    uint64_t session_id;
    size_t bytes_transferred;
    bool is_send; // true if sent, false if received
};

You can subscribe to these events directly using the broker to implement custom metrics collection.

Using the ThroughputTracker Utility

cpp-tcpnet provides a built-in ThroughputTracker utility class that automatically monitors this event stream and computes a sliding-window data rate (bytes per second).

Example usage:

#include "cpptcpnet.hpp"
#include <iostream>
#include <thread>
#include <chrono>

int main() {
    cpptcpnet::TcpListener server(8080);
    server.Start();

    // Create a tracker with a 1-second sliding window
    cpptcpnet::ThroughputTracker tracker(server.GetEventBroker(), std::chrono::seconds(1));

    // Monitor throughput in a loop (assuming server.IsRunning() or similar loop condition)
    while (true) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        std::cout << "Upload rate: " << tracker.GetSendThroughputBytesPerSec() << " B/s | "
                  << "Download rate: " << tracker.GetRecvThroughputBytesPerSec() << " B/s\n";
    }

    server.Stop();
    return 0;
}

3. Throughput Scaling Utilities

cpp-tcpnet provides formatting helper functions to scale raw byte counts or bit rates into human-readable strings with binary (KB, MB, GB, etc.) or decimal (Kbps, Mbps, Gbps, etc.) units.

ScaledUnit Structure

Both utilities return a ScaledUnit struct containing:

Example usage:

uint64_t bytes_transferred = 5368709120; // 5 GB
cpptcpnet::ScaledUnit scaled = cpptcpnet::ScaleBytes(bytes_transferred);
std::cout << "Transferred: " << scaled.value << " " << scaled.unit << "\n"; // Outputs: Transferred: 5 GB

double bit_rate = 12500000; // 12.5 Mbps
cpptcpnet::ScaledUnit scaled_rate = cpptcpnet::ScaleBits(bit_rate);
std::cout << "Speed: " << scaled_rate.value << " " << scaled_rate.unit << "\n"; // Outputs: Speed: 12.5 Mbps

< Previous: Advanced Usage | 🏠 Home | Next: Architecture & Examples >