1
- using System . Threading ;
1
+ using System ;
2
+ using System . Collections . Concurrent ;
3
+ using System . Linq ;
4
+ using System . Threading ;
2
5
3
6
namespace LiteNetLib
4
7
{
5
8
public sealed class NetStatistics
6
9
{
10
+ public bool TrackAveragePacketSize ;
11
+ public int PacketSizeAverageWindow = 256 ;
12
+
13
+ public bool TrackAveragePacketMergeCount ;
14
+ public int PacketMergeAverageWindow = 256 ;
15
+
7
16
private long _packetsSent ;
8
17
private long _packetsReceived ;
9
18
private long _bytesSent ;
10
19
private long _bytesReceived ;
11
20
private long _packetLoss ;
21
+ private long _windowWaits ;
22
+
23
+ ConcurrentQueue < long > _packetSizes = new ConcurrentQueue < long > ( ) ;
24
+ ConcurrentQueue < int > _packetMerges = new ConcurrentQueue < int > ( ) ;
12
25
13
26
public long PacketsSent => Interlocked . Read ( ref _packetsSent ) ;
14
27
public long PacketsReceived => Interlocked . Read ( ref _packetsReceived ) ;
15
28
public long BytesSent => Interlocked . Read ( ref _bytesSent ) ;
16
29
public long BytesReceived => Interlocked . Read ( ref _bytesReceived ) ;
17
30
public long PacketLoss => Interlocked . Read ( ref _packetLoss ) ;
31
+ public long WindowWaitCount => Interlocked . Read ( ref _windowWaits ) ;
18
32
19
33
public long PacketLossPercent
20
34
{
@@ -26,18 +40,68 @@ public long PacketLossPercent
26
40
}
27
41
}
28
42
43
+ public double ComputeAveragePacketSize ( )
44
+ {
45
+ if ( ! TrackAveragePacketSize )
46
+ throw new InvalidOperationException ( "Tracking average packet size is not enabled" ) ;
47
+
48
+ return ComputeAverage ( _packetSizes , size => ( double ) size ) ;
49
+ }
50
+
51
+ public double ComputeAveragePacketMerge ( )
52
+ {
53
+ if ( ! TrackAveragePacketMergeCount )
54
+ throw new InvalidOperationException ( "Tracking average packet merge is not enabled" ) ;
55
+
56
+ return ComputeAverage ( _packetMerges , count => ( double ) count ) ;
57
+ }
58
+
59
+ double ComputeAverage < T > ( ConcurrentQueue < T > data , Func < T , double > selector )
60
+ {
61
+ int count = 0 ;
62
+ double sum = 0 ;
63
+
64
+ foreach ( var value in data )
65
+ {
66
+ count ++ ;
67
+ sum += selector ( value ) ;
68
+ }
69
+
70
+ if ( count == 0 )
71
+ return 0 ;
72
+
73
+ return sum / count ;
74
+ }
75
+
76
+ void Store < T > ( ConcurrentQueue < T > data , T value , int max )
77
+ {
78
+ data . Enqueue ( value ) ;
79
+
80
+ while ( data . Count > 0 && data . Count > max )
81
+ data . TryDequeue ( out _ ) ;
82
+ }
83
+
29
84
public void Reset ( )
30
85
{
31
86
Interlocked . Exchange ( ref _packetsSent , 0 ) ;
32
87
Interlocked . Exchange ( ref _packetsReceived , 0 ) ;
33
88
Interlocked . Exchange ( ref _bytesSent , 0 ) ;
34
89
Interlocked . Exchange ( ref _bytesReceived , 0 ) ;
35
90
Interlocked . Exchange ( ref _packetLoss , 0 ) ;
91
+ Interlocked . Exchange ( ref _windowWaits , 0 ) ;
92
+
93
+ #if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
94
+ _packetSizes . Clear ( ) ;
95
+ _packetMerges . Clear ( ) ;
96
+ #endif
36
97
}
37
98
38
- public void IncrementPacketsSent ( )
99
+ public void IncrementPacketsSent ( int merged )
39
100
{
40
101
Interlocked . Increment ( ref _packetsSent ) ;
102
+
103
+ if ( TrackAveragePacketMergeCount )
104
+ Store ( _packetMerges , merged , PacketMergeAverageWindow ) ;
41
105
}
42
106
43
107
public void IncrementPacketsReceived ( )
@@ -48,6 +112,9 @@ public void IncrementPacketsReceived()
48
112
public void AddBytesSent ( long bytesSent )
49
113
{
50
114
Interlocked . Add ( ref _bytesSent , bytesSent ) ;
115
+
116
+ if ( TrackAveragePacketSize )
117
+ Store ( _packetSizes , bytesSent , PacketSizeAverageWindow ) ;
51
118
}
52
119
53
120
public void AddBytesReceived ( long bytesReceived )
@@ -65,17 +132,23 @@ public void AddPacketLoss(long packetLoss)
65
132
Interlocked . Add ( ref _packetLoss , packetLoss ) ;
66
133
}
67
134
135
+ public void IncrementWindowWaits ( )
136
+ {
137
+ Interlocked . Increment ( ref _windowWaits ) ;
138
+ }
139
+
68
140
public override string ToString ( )
69
141
{
70
142
return
71
143
string . Format (
72
- "BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}\n PacketLoss: {4}\n PacketLossPercent: {5}\n " ,
144
+ "BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}\n PacketLoss: {4}\n PacketLossPercent: {5}\n Window Wait Count: {6} " ,
73
145
BytesReceived ,
74
146
PacketsReceived ,
75
147
BytesSent ,
76
148
PacketsSent ,
77
149
PacketLoss ,
78
- PacketLossPercent ) ;
150
+ PacketLossPercent ,
151
+ WindowWaitCount ) ;
79
152
}
80
153
}
81
154
}
0 commit comments