1- using NetworkLibrary . TCP . ByteMessage ;
1+ using NetworkLibrary . TCP ;
2+ using NetworkLibrary . TCP . ByteMessage ;
23using NetworkLibrary . Utils ;
34using System ;
45using System . Collections . Concurrent ;
1112using System . Threading ;
1213using System . Threading . Tasks ;
1314
14-
1515namespace ConsoleTest
1616{
1717
1818 internal class Program
1919 {
2020 static void Main ( string [ ] args )
2121 {
22- TcpTest ( ) ;
22+ //TcpTest();
23+ TcpTest2 ( ) ;
2324
2425 }
2526
27+
2628 private static void TcpTest ( )
2729 {
2830 // dont change.
2931 int NumFinishedClients = 0 ;
30-
32+ //CoreAssemblyConfig.UseUnmanaged=true;
3133 MiniLogger . AllLog += ( string log ) => Console . WriteLine ( log ) ;
3234
3335 int totMsgClient = 0 ;
@@ -52,6 +54,8 @@ private static void TcpTest()
5254 server . OnBytesReceived += OnServerReceviedMessage ;
5355 server . StartServer ( ) ;
5456
57+ server . GatherConfig = ScatterGatherConfig . UseQueue ;
58+
5559 Task [ ] toWait = new Task [ clientAmount ] ;
5660 for ( int i = 0 ; i < clientAmount ; i ++ )
5761 {
@@ -101,10 +105,15 @@ private static void TcpTest()
101105 {
102106 ShowStatus ( ) ;
103107 }
108+ server . ShutdownServer ( ) ;
109+ foreach ( var client1 in clients )
110+ {
111+ client1 . Disconnect ( ) ;
112+ }
104113 //----- End ---
105114 void ShowStatus ( )
106115 {
107- Console . WriteLine ( "Press E to Exit" ) ;
116+ Console . WriteLine ( "\n Press E to Exit\n " ) ;
108117
109118 Console . WriteLine ( "Total Messages on server: " + totMsgServer ) ;
110119 Console . WriteLine ( "Total Messages on clients: " + totMsgClient ) ;
@@ -138,22 +147,169 @@ void OnClientReceivedMessage(ByteMessageTcpClient client, byte[] arg2, int offse
138147
139148 }
140149 }
150+
141151 }
142152
143- void OnServerReceviedMessage ( Guid id , byte [ ] arg2 , int offset , int count )
153+ void OnServerReceviedMessage ( in Guid id , byte [ ] arg2 , int offset , int count )
144154 {
145155 Interlocked . Increment ( ref totMsgServer ) ;
146156 if ( count == 502 )
147157 {
148- server . SendBytesToClient ( id , new byte [ 502 ] ) ;
158+ server . SendBytesToClient ( in id , new byte [ 502 ] ) ;
149159 return ;
150160 }
161+
151162 // response = new byte[32000];
152- server . SendBytesToClient ( id , response ) ;
163+ server . SendBytesToClient ( in id , response ) ;
164+ }
165+
166+ }
167+
168+ private static void TcpTest2 ( )
169+ {
170+ // dont change.
171+ int NumFinishedClients = 0 ;
172+ MiniLogger . AllLog += ( string log ) => Console . WriteLine ( log ) ;
173+
174+ long totMsgClient = 0 ;
175+ long totMsgServer = 0 ;
176+ long lastTimeStamp = 1 ;
177+ int clientAmount = 100 ;
178+ const int numMsg = 1000 ;
179+ var message = new byte [ 32 ] ;
180+ var response = new byte [ 32 ] ;
181+
182+ bool done = false ;
183+ int port = 20011 ;
184+ ByteMessageTcpServer server = new ByteMessageTcpServer ( port , clientAmount * 3 ) ;
185+ List < ByteMessageTcpClient > clients = new List < ByteMessageTcpClient > ( ) ;
186+
187+ Stopwatch sw2 = new Stopwatch ( ) ;
188+ AutoResetEvent testCompletionEvent = new AutoResetEvent ( false ) ;
189+
190+ server . MaxIndexedMemoryPerClient = 128000 ;
191+ server . ClientSendBufsize = 128000 ;
192+ server . ClientReceiveBufsize = 128000 ;
193+ server . DropOnBackPressure = false ;
194+ server . OnBytesReceived += OnServerReceviedMessage ;
195+ server . StartServer ( ) ;
196+
197+ Task [ ] toWait = new Task [ clientAmount ] ;
198+ for ( int i = 0 ; i < clientAmount ; i ++ )
199+ {
200+ var client = new ByteMessageTcpClient ( ) ;
201+ client . BufferManager = server . BufferManager ;
202+ client . MaxIndexedMemory = server . MaxIndexedMemoryPerClient ;
203+
204+ client . DropOnCongestion = false ;
205+ client . OnBytesReceived += ( byte [ ] arg2 , int offset , int count ) => OnClientReceivedMessage ( client , arg2 , offset , count ) ;
206+
207+ toWait [ i ] = client . ConnectAsyncAwaitable ( "127.0.0.1" , port ) ;
208+ clients . Add ( client ) ;
209+ }
210+
211+ Task . WaitAll ( toWait ) ;
212+
213+ // ----------------------- Bechmark ---------------------------
214+ Console . WriteLine ( "Press any key to start" ) ;
215+ Console . Read ( ) ;
216+ sw2 . Start ( ) ;
217+
218+ // We parallely send the messages here
219+ Parallel . ForEach ( clients , client =>
220+ {
221+ for ( int i = 0 ; i < numMsg ; i ++ )
222+ {
223+ client . SendAsync ( message ) ;
224+
225+ }
226+
227+ } ) ;
228+
229+ Task . Run ( async ( ) =>
230+ {
231+ while ( ! done )
232+ {
233+ await Task . Delay ( 2000 ) ;
234+ //ShowStatus();
235+ }
236+
237+ } ) ;
238+
239+ // -------- Messages are sent by clients ------
240+
241+ Console . WriteLine ( "All messages are dispatched in :" + sw2 . ElapsedMilliseconds +
242+ "ms. Press enter to see status" ) ;
243+ Console . ReadLine ( ) ;
244+
245+ Console . WriteLine ( "Press e to Exit" ) ;
246+ while ( Console . ReadLine ( ) != "e" )
247+ {
248+ ShowStatus ( ) ;
249+ }
250+
251+ done = true ;
252+ server . ShutdownServer ( ) ;
253+ foreach ( var client1 in clients )
254+ {
255+ client1 . Disconnect ( ) ;
256+ }
257+ //----- End ---
258+ void ShowStatus ( )
259+ {
260+ Console . WriteLine ( "Press E to Exit" ) ;
261+
262+ Console . WriteLine ( "Total Messages on server: " + totMsgServer ) ;
263+ Console . WriteLine ( "Total Messages on clients: " + totMsgClient ) ;
264+
265+ lastTimeStamp = sw2 . ElapsedMilliseconds ;
266+ Console . WriteLine ( "Elapsed " + lastTimeStamp ) ;
267+
268+ var elapsedSeconds = ( double ) lastTimeStamp / 1000 ;
269+ var messagePerSecond = totMsgClient / elapsedSeconds ;
270+
271+ Console . WriteLine ( " Request-Response Per second " + ( totMsgClient / elapsedSeconds ) . ToString ( "N1" ) ) ;
272+ Console . WriteLine ( "Data transmissıon rate Inbound " + ( message . Length * messagePerSecond / 1000000 ) . ToString ( "N1" ) + " Megabytes/s" ) ;
273+ Console . WriteLine ( "Data transmissıon rate Outbound " + ( response . Length * messagePerSecond / 1000000 ) . ToString ( "N1" ) + " Megabytes/s" ) ;
153274 }
154275
276+ void OnClientReceivedMessage ( ByteMessageTcpClient client , byte [ ] arg2 , int offset , int count )
277+ {
278+ Interlocked . Increment ( ref totMsgClient ) ;
279+ client . SendAsync ( response ) ;
280+ if ( count == 502 )
281+ {
282+ lastTimeStamp = ( int ) sw2 . ElapsedMilliseconds ;
283+ Interlocked . Increment ( ref NumFinishedClients ) ;
284+
285+ if ( Volatile . Read ( ref NumFinishedClients ) == clientAmount )
286+ {
287+ Console . WriteLine ( "\n --- All Clients are finished receiving response --- \n " ) ;
288+ ShowStatus ( ) ;
289+ sw2 . Stop ( ) ;
290+ Console . WriteLine ( "\n --- All Clients are finished receiving response --- \n " ) ;
291+
292+ }
293+ }
294+
295+ }
296+
297+ void OnServerReceviedMessage ( in Guid id , byte [ ] arg2 , int offset , int count )
298+ {
299+ Interlocked . Increment ( ref totMsgServer ) ;
300+ if ( count == 502 )
301+ {
302+ server . SendBytesToClient ( id , new byte [ 502 ] ) ;
303+ return ;
304+ }
305+
306+ // response = new byte[32000];
307+ //server.SendBytesToClient(id, arg2,offset,count);
308+ server . SendBytesToClient ( id , response ) ;
309+ }
155310 }
156311
312+
157313 }
158314
159315}
0 commit comments