1+ using NetworkLibrary ;
2+ using NetworkLibrary . Components ;
3+ using NetworkLibrary . MessageProtocol ;
4+ using NetworkLibrary . MessageProtocol . Serialization ;
5+ using NetworkLibrary . Utils ;
6+ using Protobuff . P2P ;
7+ using System . Collections . Concurrent ;
8+ using System . Diagnostics ;
9+ using System . Runtime . CompilerServices ;
10+ using System . Security . Cryptography . X509Certificates ;
11+
12+ namespace RelayBenchmark
13+ {
14+ internal class Program
15+ {
16+ private static int totMsgCl ;
17+
18+ static void Main ( string [ ] args )
19+ {
20+ MiniLogger . AllLog += ( l ) => Console . WriteLine ( l ) ;
21+ RelayTest ( ) ;
22+
23+ Console . ReadLine ( ) ;
24+ }
25+ private static void SerializerTest ( )
26+ {
27+ PooledMemoryStream stream = new PooledMemoryStream ( ) ;
28+ MessageEnvelope env = new MessageEnvelope ( )
29+ {
30+ IsInternal = true ,
31+ From = Guid . NewGuid ( ) ,
32+ To = Guid . NewGuid ( ) ,
33+ Header = "rattatta" ,
34+
35+ MessageId = Guid . NewGuid ( ) ,
36+ TimeStamp = DateTime . Now ,
37+ KeyValuePairs = new Dictionary < string , string > ( ) {
38+ { "K1" , "v2" } ,
39+ { "K3" , "" } ,
40+ { "K2" , null } ,
41+ { "K4" , "%%" } ,
42+ }
43+ } ;
44+ EnvelopeSerializer . Serialize ( stream , env ) ;
45+ var result = EnvelopeSerializer . Deserialize ( stream . GetBuffer ( ) , 0 ) ;
46+ stream . Position = 0 ;
47+
48+ Stopwatch sw = new Stopwatch ( ) ;
49+ sw . Start ( ) ;
50+ for ( int i = 0 ; i < 50000000 ; i ++ )
51+ {
52+ // EnvelopeSerializer.Serialize(stream, env);
53+ var r = EnvelopeSerializer . DeserializeToRouterHeader ( stream . GetBuffer ( ) , 0 ) ;
54+ stream . Position = 0 ;
55+
56+ }
57+ sw . Stop ( ) ;
58+ Console . WriteLine ( sw . ElapsedMilliseconds ) ;
59+ }
60+ private static void RelayTest ( )
61+ {
62+ string ip = "127.0.0.1" ;
63+ MessageEnvelope testMessage = new MessageEnvelope ( )
64+ {
65+ Header = "Test" ,
66+ // Payload = new byte[32]
67+ } ;
68+
69+ var cert = new X509Certificate2 ( "client.pfx" , "greenpass" ) ;
70+ var scert = new X509Certificate2 ( "server.pfx" , "greenpass" ) ;
71+
72+ // var server = new SecureProtoRelayServer(20011, scert);
73+ var clients = new ConcurrentBag < RelayClient > ( ) ;
74+ //for (int i = 0; i < 200; i++)
75+ int numclients = 20 ;
76+ Task [ ] pending = new Task [ numclients ] ;
77+ // Parallel.For(0, numclients, (i) =>
78+ for ( int i = 0 ; i < numclients ; i ++ )
79+
80+ {
81+ var client = new RelayClient ( cert ) ;
82+ client . OnMessageReceived += ( reply ) => ClientMsgReceived ( client , reply ) ;
83+ client . OnUdpMessageReceived += ( reply ) => ClientUdpReceived ( client , reply ) ;
84+ //client.OnPeerRegistered+=(id)=> client.RequestHolePunchAsync(id, 10000, false);
85+ try
86+ {
87+ pending [ i ] = client . ConnectAsync ( ip , 20011 ) ;
88+
89+ clients . Add ( client ) ;
90+ client . StartPingService ( ) ;
91+ }
92+ catch { }
93+
94+ //Thread.Sleep(1000);
95+ }
96+ //);
97+ Task . WaitAll ( pending ) ;
98+ Thread . Sleep ( 5000 ) ;
99+ int cc = 0 ;
100+ List < Task < bool > > pndg = new List < Task < bool > > ( ) ;
101+ foreach ( var client in clients )
102+ {
103+ if ( client . sessionId == Guid . Empty )
104+ throw new Exception ( ) ;
105+ // Console.WriteLine("--- -- - | "+client.sessionId+" count: " + client.Peers.Count);
106+ foreach ( var peer in client . Peers )
107+ {
108+ if ( client . sessionId > peer . Key )
109+ {
110+ if ( peer . Key == Guid . Empty )
111+ throw new Exception ( ) ;
112+
113+ var a = client . RequestHolePunchAsync ( peer . Key , 10000 , false ) ;
114+ pndg . Add ( a ) ;
115+
116+ // Console.WriteLine(peer.Key+" cnt=> "+ ++cc);
117+ }
118+
119+ }
120+ }
121+ Task . WaitAll ( pndg . ToArray ( ) ) ;
122+ int kk = 0 ;
123+ foreach ( var item in pndg )
124+ {
125+ kk ++ ;
126+
127+ if ( item . Result == false )
128+ {
129+ Console . WriteLine ( " +++++++++-------***************---------------- Fucked" ) ;
130+ }
131+ else
132+ {
133+ Console . WriteLine ( "All good" + kk ) ;
134+ }
135+ }
136+
137+ Task . Run ( async ( ) =>
138+ {
139+ while ( true )
140+ {
141+ await Task . Delay ( 3000 ) ;
142+ Console . WriteLine ( totMsgCl ) ;
143+ }
144+
145+ } ) ;
146+ Thread . Sleep ( 5000 ) ;
147+ Parallel . ForEach ( clients , ( client ) =>
148+ {
149+ for ( int i = 0 ; i < 10 ; i ++ )
150+ {
151+ //return;
152+ foreach ( var peer in client . Peers . Keys )
153+ {
154+ //await client.SendRequestAndWaitResponse(peer, testMessage,1000);
155+ //client.SendAsyncMessage(peer, testMessage);
156+
157+ // client.SendUdpMesssage(peer, testMessage);
158+ }
159+ }
160+
161+ } ) ;
162+
163+
164+
165+ void ClientMsgReceived ( RelayClient client , MessageEnvelope reply )
166+ {
167+ //Interlocked.Increment(ref totMsgCl);
168+ client . SendAsyncMessage ( reply . From , reply ) ;
169+
170+ }
171+
172+
173+ void ClientUdpReceived ( RelayClient client , MessageEnvelope reply )
174+ {
175+ Interlocked . Increment ( ref totMsgCl ) ;
176+ client . SendUdpMesssage ( reply . From , reply ) ;
177+
178+ }
179+ }
180+
181+ }
182+ }
0 commit comments