Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional networking statistics #1

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LiteNetLib/NetManager.Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ internal int SendRaw(byte[] message, int start, int length, IPEndPoint remoteEnd

if (EnableStatistics)
{
Statistics.IncrementPacketsSent();
Statistics.IncrementPacketsSent(1);
Statistics.AddBytesSent(length);
}

Expand Down
4 changes: 2 additions & 2 deletions LiteNetLib/NetPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ private void SendMerged()

if (NetManager.EnableStatistics)
{
Statistics.IncrementPacketsSent();
Statistics.IncrementPacketsSent(_mergeCount);
Statistics.AddBytesSent(bytesSent);
}

Expand All @@ -1288,7 +1288,7 @@ internal void SendUserData(NetPacket packet)

if (NetManager.EnableStatistics)
{
Statistics.IncrementPacketsSent();
Statistics.IncrementPacketsSent(1);
Statistics.AddBytesSent(bytesSent);
}

Expand Down
81 changes: 77 additions & 4 deletions LiteNetLib/NetStatistics.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
using System.Threading;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;

namespace LiteNetLib
{
public sealed class NetStatistics
{
public bool TrackAveragePacketSize;
public int PacketSizeAverageWindow = 256;

public bool TrackAveragePacketMergeCount;
public int PacketMergeAverageWindow = 256;

private long _packetsSent;
private long _packetsReceived;
private long _bytesSent;
private long _bytesReceived;
private long _packetLoss;
private long _windowWaits;

ConcurrentQueue<long> _packetSizes = new ConcurrentQueue<long>();
ConcurrentQueue<int> _packetMerges = new ConcurrentQueue<int>();

public long PacketsSent => Interlocked.Read(ref _packetsSent);
public long PacketsReceived => Interlocked.Read(ref _packetsReceived);
public long BytesSent => Interlocked.Read(ref _bytesSent);
public long BytesReceived => Interlocked.Read(ref _bytesReceived);
public long PacketLoss => Interlocked.Read(ref _packetLoss);
public long WindowWaitCount => Interlocked.Read(ref _windowWaits);

public long PacketLossPercent
{
Expand All @@ -26,18 +40,68 @@ public long PacketLossPercent
}
}

public double ComputeAveragePacketSize()
{
if (!TrackAveragePacketSize)
throw new InvalidOperationException("Tracking average packet size is not enabled");

return ComputeAverage(_packetSizes, size => (double)size);
}

public double ComputeAveragePacketMerge()
{
if (!TrackAveragePacketMergeCount)
throw new InvalidOperationException("Tracking average packet merge is not enabled");

return ComputeAverage(_packetMerges, count => (double)count);
}

double ComputeAverage<T>(ConcurrentQueue<T> data, Func<T, double> selector)
{
int count = 0;
double sum = 0;

foreach (var value in data)
{
count++;
sum += selector(value);
}

if (count == 0)
return 0;

return sum / count;
}

void Store<T>(ConcurrentQueue<T> data, T value, int max)
{
data.Enqueue(value);

while (data.Count > 0 && data.Count > max)
data.TryDequeue(out _);
}

public void Reset()
{
Interlocked.Exchange(ref _packetsSent, 0);
Interlocked.Exchange(ref _packetsReceived, 0);
Interlocked.Exchange(ref _bytesSent, 0);
Interlocked.Exchange(ref _bytesReceived, 0);
Interlocked.Exchange(ref _packetLoss, 0);
Interlocked.Exchange(ref _windowWaits, 0);

#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
_packetSizes.Clear();
_packetMerges.Clear();
#endif
}

public void IncrementPacketsSent()
public void IncrementPacketsSent(int merged)
{
Interlocked.Increment(ref _packetsSent);

if (TrackAveragePacketMergeCount)
Store(_packetMerges, merged, PacketMergeAverageWindow);
}

public void IncrementPacketsReceived()
Expand All @@ -48,6 +112,9 @@ public void IncrementPacketsReceived()
public void AddBytesSent(long bytesSent)
{
Interlocked.Add(ref _bytesSent, bytesSent);

if (TrackAveragePacketSize)
Store(_packetSizes, bytesSent, PacketSizeAverageWindow);
}

public void AddBytesReceived(long bytesReceived)
Expand All @@ -65,17 +132,23 @@ public void AddPacketLoss(long packetLoss)
Interlocked.Add(ref _packetLoss, packetLoss);
}

public void IncrementWindowWaits()
{
Interlocked.Increment(ref _windowWaits);
}

public override string ToString()
{
return
string.Format(
"BytesReceived: {0}\nPacketsReceived: {1}\nBytesSent: {2}\nPacketsSent: {3}\nPacketLoss: {4}\nPacketLossPercent: {5}\n",
"BytesReceived: {0}\nPacketsReceived: {1}\nBytesSent: {2}\nPacketsSent: {3}\nPacketLoss: {4}\nPacketLossPercent: {5}\nWindow Wait Count: {6}",
BytesReceived,
PacketsReceived,
BytesSent,
PacketsSent,
PacketLoss,
PacketLossPercent);
PacketLossPercent,
WindowWaitCount);
}
}
}
7 changes: 6 additions & 1 deletion LiteNetLib/ReliableChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ protected override bool SendNextPackets()
{
int relate = NetUtils.RelativeSequenceNumber(_localSeqence, _localWindowStart);
if (relate >= _windowSize)
{
if (Peer.NetManager.EnableStatistics)
Peer.Statistics.IncrementWindowWaits();

break;
}

var netPacket = OutgoingQueue.Dequeue();
netPacket.Sequence = (ushort) _localSeqence;
Expand Down Expand Up @@ -294,7 +299,7 @@ public override bool ProcessPacket(NetPacket packet)
//detailed check
if (seq == _remoteSequence)
{
NetDebug.Write("[RR]ReliableInOrder packet succes");
NetDebug.Write("[RR]ReliableInOrder packet success");
Peer.AddReliablePacket(_deliveryMethod, packet);
_remoteSequence = (_remoteSequence + 1) % NetConstants.MaxSequence;

Expand Down
Loading