-
Notifications
You must be signed in to change notification settings - Fork 580
/
Program.cs
178 lines (151 loc) · 6.04 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using NetCoreServer;
using NDesk.Options;
namespace SslEchoClient
{
class EchoClient : SslClient
{
public EchoClient(SslContext context, string address, int port, int messages) : base(context, address, port)
{
_messages = messages;
}
protected override void OnHandshaked()
{
for (long i = _messages; i > 0; i--)
SendMessage();
}
protected override void OnSent(long sent, long pending)
{
_sent += sent;
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
_received += size;
while (_received >= Program.MessageToSend.Length)
{
SendMessage();
_received -= Program.MessageToSend.Length;
}
Program.TimestampStop = DateTime.UtcNow;
Program.TotalBytes += size;
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Client caught an error with code {error}");
Program.TotalErrors++;
}
private void SendMessage()
{
SendAsync(Program.MessageToSend);
}
private long _sent;
private long _received;
private long _messages;
}
class Program
{
public static byte[] MessageToSend;
public static DateTime TimestampStart = DateTime.UtcNow;
public static DateTime TimestampStop = DateTime.UtcNow;
public static long TotalErrors;
public static long TotalBytes;
public static long TotalMessages;
static void Main(string[] args)
{
bool help = false;
string address = "127.0.0.1";
int port = 2222;
int clients = 100;
int messages = 1000;
int size = 32;
int seconds = 10;
var options = new OptionSet()
{
{ "h|?|help", v => help = v != null },
{ "a|address=", v => address = v },
{ "p|port=", v => port = int.Parse(v) },
{ "c|clients=", v => clients = int.Parse(v) },
{ "m|messages=", v => messages = int.Parse(v) },
{ "s|size=", v => size = int.Parse(v) },
{ "z|seconds=", v => seconds = int.Parse(v) }
};
try
{
options.Parse(args);
}
catch (OptionException e)
{
Console.Write("Command line error: ");
Console.WriteLine(e.Message);
Console.WriteLine("Try `--help' to get usage information.");
return;
}
if (help)
{
Console.WriteLine("Usage:");
options.WriteOptionDescriptions(Console.Out);
return;
}
Console.WriteLine($"Server address: {address}");
Console.WriteLine($"Server port: {port}");
Console.WriteLine($"Working clients: {clients}");
Console.WriteLine($"Working messages: {messages}");
Console.WriteLine($"Message size: {size}");
Console.WriteLine($"Seconds to benchmarking: {seconds}");
Console.WriteLine();
// Prepare a message to send
MessageToSend = new byte[size];
// Create and prepare a new SSL client context
var context = new SslContext(SslProtocols.Tls13, new X509Certificate2("client.pfx", "qwerty"), (sender, certificate, chain, sslPolicyErrors) => true);
// Create echo clients
var echoClients = new List<EchoClient>();
for (int i = 0; i < clients; i++)
{
var client = new EchoClient(context, address, port, messages);
// client.OptionNoDelay = true;
echoClients.Add(client);
}
TimestampStart = DateTime.UtcNow;
// Connect clients
Console.Write("Clients connecting...");
foreach (var client in echoClients)
client.ConnectAsync();
Console.WriteLine("Done!");
foreach (var client in echoClients)
while (!client.IsHandshaked)
Thread.Yield();
Console.WriteLine("All clients connected!");
// Wait for benchmarking
Console.Write("Benchmarking...");
Thread.Sleep(seconds * 1000);
Console.WriteLine("Done!");
// Disconnect clients
Console.Write("Clients disconnecting...");
foreach (var client in echoClients)
client.DisconnectAsync();
Console.WriteLine("Done!");
foreach (var client in echoClients)
while (client.IsConnected)
Thread.Yield();
Console.WriteLine("All clients disconnected!");
Console.WriteLine();
Console.WriteLine($"Errors: {TotalErrors}");
Console.WriteLine();
TotalMessages = TotalBytes / size;
Console.WriteLine($"Total time: {Utilities.GenerateTimePeriod((TimestampStop - TimestampStart).TotalMilliseconds)}");
Console.WriteLine($"Total data: {Utilities.GenerateDataSize(TotalBytes)}");
Console.WriteLine($"Total messages: {TotalMessages}");
Console.WriteLine($"Data throughput: {Utilities.GenerateDataSize((long)(TotalBytes / (TimestampStop - TimestampStart).TotalSeconds))}/s");
if (TotalMessages > 0)
{
Console.WriteLine($"Message latency: {Utilities.GenerateTimePeriod((TimestampStop - TimestampStart).TotalMilliseconds / TotalMessages)}");
Console.WriteLine($"Message throughput: {(long)(TotalMessages / (TimestampStop - TimestampStart).TotalSeconds)} msg/s");
}
}
}
}