Statistics¶
MafiaNet provides detailed statistics about network performance.
Getting Statistics¶
MafiaNet::RakNetStatistics stats;
peer->GetStatistics(systemAddress, &stats);
// Or for all connections combined
MafiaNet::RakNetStatistics* stats = peer->GetStatistics(
MafiaNet::UNASSIGNED_SYSTEM_ADDRESS);
Key Statistics¶
Bandwidth¶
// Bytes sent/received per second (actual)
uint64_t bytesSentPerSec = stats.valueOverLastSecond[
MafiaNet::ACTUAL_BYTES_SENT];
uint64_t bytesRecvPerSec = stats.valueOverLastSecond[
MafiaNet::ACTUAL_BYTES_RECEIVED];
// User data only (excluding protocol overhead)
uint64_t userBytesSent = stats.valueOverLastSecond[
MafiaNet::USER_MESSAGE_BYTES_SENT];
Packet Counts¶
// Total messages
uint64_t messagesSent = stats.runningTotal[
MafiaNet::USER_MESSAGE_BYTES_PUSHED];
// Packets sent/received
uint64_t packetsSent = stats.valueOverLastSecond[
MafiaNet::ACTUAL_BYTES_SENT] / averagePacketSize;
Latency¶
// Get ping to a specific system
int ping = peer->GetAveragePing(systemAddress);
// Last ping value
int lastPing = peer->GetLastPing(systemAddress);
// Lowest ping recorded
int lowestPing = peer->GetLowestPing(systemAddress);
Packet Loss¶
// Messages lost
uint64_t messagesLost = stats.runningTotal[
MafiaNet::USER_MESSAGE_BYTES_RESENT] /
averageMessageSize; // Approximate
// Packet loss percentage
float packetLossPercent = stats.packetlossLastSecond;
Connection Quality¶
// Bandwidth exceeded (sending too fast)
bool bandwidthExceeded = stats.bandwidthExceededStatistic;
// Connection start time
MafiaNet::Time connectionTime = stats.connectionStartTime;
Statistics Fields Reference¶
Field |
Description |
|---|---|
|
Total bytes sent including overhead |
|
Total bytes received |
|
User data bytes sent |
|
User data bytes received |
|
Bytes queued for sending |
|
Bytes resent due to packet loss |
|
Packet loss percentage (0.0-1.0) |
|
True if sending faster than bandwidth |
Displaying Statistics¶
void DisplayNetworkStats(MafiaNet::RakPeerInterface* peer,
MafiaNet::SystemAddress addr) {
MafiaNet::RakNetStatistics stats;
peer->GetStatistics(addr, &stats);
printf("=== Network Statistics ===\n");
printf("Ping: %d ms\n", peer->GetAveragePing(addr));
printf("Packet Loss: %.1f%%\n", stats.packetlossLastSecond * 100);
printf("Bytes Sent/sec: %llu\n",
stats.valueOverLastSecond[MafiaNet::ACTUAL_BYTES_SENT]);
printf("Bytes Recv/sec: %llu\n",
stats.valueOverLastSecond[MafiaNet::ACTUAL_BYTES_RECEIVED]);
if (stats.bandwidthExceededStatistic) {
printf("WARNING: Bandwidth exceeded!\n");
}
}
Monitoring Connection Health¶
enum ConnectionQuality {
EXCELLENT, // < 50ms, < 1% loss
GOOD, // < 100ms, < 5% loss
FAIR, // < 200ms, < 10% loss
POOR // > 200ms or > 10% loss
};
ConnectionQuality GetConnectionQuality(MafiaNet::RakPeerInterface* peer,
MafiaNet::SystemAddress addr) {
int ping = peer->GetAveragePing(addr);
MafiaNet::RakNetStatistics stats;
peer->GetStatistics(addr, &stats);
float loss = stats.packetlossLastSecond;
if (ping < 50 && loss < 0.01f) return EXCELLENT;
if (ping < 100 && loss < 0.05f) return GOOD;
if (ping < 200 && loss < 0.10f) return FAIR;
return POOR;
}
StatisticsHistory Plugin¶
For tracking statistics over time, use the StatisticsHistory plugin:
#include "mafianet/StatisticsHistory.h"
MafiaNet::StatisticsHistory* statsHistory =
MafiaNet::StatisticsHistory::GetInstance();
peer->AttachPlugin(statsHistory);
// Later, query historical data
// ...
See Also¶
Debugging Disconnects - Diagnosing connection issues
Reliability Types - Impact on bandwidth usage