Skip to content

Commit 8deedf7

Browse files
committed
Dirty Protobuf P2P
1 parent ec82ec1 commit 8deedf7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+5016
-351
lines changed

Benchmarks/SslBenchmark/Program.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using NetworkLibrary.TCP.SSL.ByteMessage;
1414
using System.Security.Cryptography.X509Certificates;
1515
using System.Net.Security;
16+
using NetworkLibrary.TCP;
1617

1718
namespace SslBenchmark
1819
{
@@ -27,15 +28,14 @@ static void Main(string[] args)
2728
private static void Bench()
2829
{
2930
MiniLogger.AllLog += (log) => Console.WriteLine(log);
30-
3131
int NumFinishedClients = 0;
3232
int totMsgClient = 0;
3333
int totMsgServer = 0;
3434
int lastTimeStamp = 1;
3535
int clientAmount = 100;
36-
const int numMsg = 10000;
37-
var message = new byte[3200];
38-
var response = new byte[3200];
36+
const int numMsg = 1000;
37+
var message = new byte[32];
38+
var response = new byte[32];
3939

4040
var scert = new X509Certificate2("server.pfx", "greenpass");
4141
var ccert = new X509Certificate2("client.pfx", "greenpass");
@@ -52,8 +52,9 @@ private static void Bench()
5252
server.DropOnBackPressure = false;
5353
server.OnBytesReceived += OnServerReceviedMessage;
5454
server.RemoteCertificateValidationCallback += ValidateCertAsServer;
55+
Console.Read();
5556
server.StartServer();
56-
57+
Console.Read();
5758
Task[] toWait = new Task[clientAmount];
5859
for (int i = 0; i < clientAmount; i++)
5960
{
@@ -83,8 +84,9 @@ bool ValidateCertAsServer(object sender, X509Certificate? certificate, X509Chain
8384
{
8485
for (int i = 0; i < numMsg; i++)
8586
{
86-
//message = new byte[32000];
87+
//message = new byte[32];
8788
client.SendAsync(message);
89+
//client.Disconnect();
8890

8991
}
9092

@@ -127,12 +129,12 @@ void ShowStatus()
127129
void OnClientReceivedMessage(SslByteMessageClient client, byte[] arg2, int offset, int count)
128130
{
129131
Interlocked.Increment(ref totMsgClient);
130-
132+
client.SendAsync(message);
131133
if (count == 502)
132134
{
133135
lastTimeStamp = (int)sw2.ElapsedMilliseconds;
134136
Interlocked.Increment(ref NumFinishedClients);
135-
137+
return;
136138
if(Volatile.Read(ref NumFinishedClients) == clientAmount)
137139
{
138140
Console.WriteLine("--- All Clients are finished receiving response --- \n");
@@ -144,12 +146,12 @@ void OnClientReceivedMessage(SslByteMessageClient client, byte[] arg2, int offse
144146
}
145147
}
146148

147-
void OnServerReceviedMessage(Guid id, byte[] arg2, int offset, int count)
149+
void OnServerReceviedMessage(in Guid id, byte[] arg2, int offset, int count)
148150
{
149151
Interlocked.Increment(ref totMsgServer);
150152
if (count == 502)
151153
{
152-
server.SendBytesToClient(id, new byte[502]);
154+
server.SendBytesToClient(in id, new byte[502]);
153155
return;
154156
}
155157
// response = new byte[32000];

Benchmarks/SslBenchmark2/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,16 @@ void OnClientReceivedMessage(CustomSslClient client, byte[] arg2, int offset, in
129129
}
130130
}
131131

132-
void OnServerReceviedMessage(Guid id, byte[] arg2, int offset, int count)
132+
void OnServerReceviedMessage(in Guid id, byte[] arg2, int offset, int count)
133133
{
134134
Interlocked.Increment(ref totMsgServer);
135135
if (count == 502)
136136
{
137-
server.SendBytesToClient(id, new byte[502]);
137+
server.SendBytesToClient(in id, new byte[502]);
138138
return;
139139
}
140140

141-
server.SendBytesToClient(id, response);
141+
server.SendBytesToClient(in id, response);
142142
}
143143

144144
}

Benchmarks/TcpBenchmark/Program.cs

Lines changed: 164 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NetworkLibrary.TCP.ByteMessage;
1+
using NetworkLibrary.TCP;
2+
using NetworkLibrary.TCP.ByteMessage;
23
using NetworkLibrary.Utils;
34
using System;
45
using System.Collections.Concurrent;
@@ -11,23 +12,24 @@
1112
using System.Threading;
1213
using System.Threading.Tasks;
1314

14-
1515
namespace 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("\nPress 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

Comments
 (0)