Class SessionStats

java.lang.Object
com.frostwire.jlibtorrent.SessionStats

public final class SessionStats extends Object
Aggregated session-wide statistics with sliding average tracking.

SessionStats maintains real-time and historical statistics about data transfer rates in the session. It tracks payload bytes (actual torrent data), protocol overhead (BitTorrent protocol messages), and IP-level overhead separately for uploaded and downloaded traffic. Useful for monitoring bandwidth usage and performance tracking over time.

Understanding Network Traffic Layers:

 Total Downloaded = Payload + Protocol Overhead + IP Overhead

 Payload:      Actual file data from torrents (what matters to users)
 Protocol:     BitTorrent protocol messages (request, have, bitfield, etc)
 IP Overhead:  TCP/IP headers and retransmissions (network-level)

 Example for 1MB download:
   Payload:    900 KB (actual file data)
   Protocol:   80 KB (peer messages)
   IP Overhead: 20 KB (TCP/IP headers)
   Total:      1000 KB
 

Accessing Session Statistics:

 // From SessionManager after listening to SessionStatsAlert
 SessionStats stats = sm.getSessionStats();

 // Overall totals (lifetime)
 long totalDown = stats.totalDownload();  // All bytes ever downloaded
 long totalUp = stats.totalUpload();      // All bytes ever uploaded

 // Current rates (bytes per second, 5-second average)
 long downloadRate = stats.downloadRate();  // KB/s (approx)
 long uploadRate = stats.uploadRate();      // KB/s (approx)

 // DHT network status
 long dhtNodes = stats.dhtNodes();  // Number of DHT nodes in routing table
 

Typical Usage Pattern:

 // Enable session stats monitoring
 sm.postSessionStats();  // Request stats alert

 sm.addListener(new AlertListener() {
     private SessionStats stats = new SessionStats();

     public int[] types() {
         return new int[] {AlertType.SESSION_STATS.swig()};
     }

     public void alert(Alert<?> alert) {
         SessionStatsAlert a = (SessionStatsAlert) alert;
         stats.update(a);  // Update with new metrics

         // Display statistics
         System.out.println(\"Download: \" +
             (stats.downloadRate() / 1024) + \" KB/s\");
         System.out.println(\"Upload: \" +
             (stats.uploadRate() / 1024) + \" KB/s\");
         System.out.println(\"DHT nodes: \" + stats.dhtNodes());
     }
 });
 

Bandwidth Breakdown Analysis:

 SessionStats stats = ...;

 long totalDown = stats.totalDownload();
 long payloadDown = stats.stat[DOWNLOAD_PAYLOAD].total();
 long protocolDown = stats.stat[DOWNLOAD_PROTOCOL].total();
 long ipDown = stats.stat[DOWNLOAD_IP_PROTOCOL].total();

 double payloadPercent = (double) payloadDown / totalDown * 100;
 double protocolPercent = (double) protocolDown / totalDown * 100;
 double ipPercent = (double) ipDown / totalDown * 100;

 System.out.println(\"Payload: \" + String.format(\"%.1f%%\", payloadPercent));
 System.out.println(\"Protocol: \" + String.format(\"%.1f%%\", protocolPercent));
 System.out.println(\"IP overhead: \" + String.format(\"%.1f%%\", ipPercent));
 

Rate Calculation (5-Second Average):

  • Rates are smoothed over 5 seconds to avoid jitter
  • Formula: current_avg = (current_avg * 4/5) + (new_sample / 5)
  • Gives recent activity 20% weight, history 80% weight
  • More stable than raw byte counts between updates

Performance Monitoring:

 // Track bandwidth efficiency
 long dl = stats.downloadRate();
 long payload = dl * 0.95;  // Assume 95% is payload
 long overhead = dl * 0.05; // Assume 5% is overhead

 if (overhead > dl * 0.2) {
     System.out.println(\"High overhead ratio detected\");
     // May indicate: slow connections, many peers, packet loss
 }

 // Monitor DHT bootstrap
 long dhtNodes = stats.dhtNodes();
 if (dhtNodes < 10) {
     System.out.println(\"DHT not fully bootstrapped yet\");
 } else if (dhtNodes > 1000) {
     System.out.println(\"DHT fully bootstrapped\");
 }
 
See Also:
  • Method Details

    • totalDownload

      public long totalDownload()
    • totalUpload

      public long totalUpload()
    • downloadRate

      public long downloadRate()
    • uploadRate

      public long uploadRate()
    • dhtNodes

      public long dhtNodes()