FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NetCoreServer/performance/ProtoClient/Program.cs at master · devdev22plus/NetCoreServer · GitHub
devdev22plus
NetCoreServer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NetCoreServer
/
performance
/
ProtoClient
/
Program.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
304 lines (247 loc) · 10.4 KB
Breadcrumbs
NetCoreServer
/
performance
/
ProtoClient
/
Program.cs
Copy path
File metadata and controls
304 lines (247 loc) · 10.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Net
.
Sockets
;
using
System
.
Threading
;
using
NetCoreServer
;
using
NDesk
.
Options
;
using
com
.
chronoxor
.
simple
;
using
com
.
chronoxor
.
simple
.
FBE
;
namespace
ProtoClient
{
class
TcpProtoClient
:
NetCoreServer
.
TcpClient
{
public
TcpProtoClient
(
string
address
,
int
port
)
:
base
(
address
,
port
)
{
}
public
delegate
void
ConnectedHandler
(
)
;
public
event
ConnectedHandler
Connected
=
(
)
=>
{
}
;
protected
override
void
OnConnected
(
)
{
Connected
?
.
Invoke
(
)
;
}
public
delegate
void
DisconnectedHandler
(
)
;
public
event
DisconnectedHandler
Disconnected
=
(
)
=>
{
}
;
protected
override
void
OnDisconnected
(
)
{
Disconnected
?
.
Invoke
(
)
;
}
public
delegate
void
ReceivedHandler
(
byte
[
]
buffer
,
long
offset
,
long
size
)
;
public
event
ReceivedHandler
Received
=
(
buffer
,
offset
,
size
)
=>
{
}
;
protected
override
void
OnReceived
(
byte
[
]
buffer
,
long
offset
,
long
size
)
{
Received
?
.
Invoke
(
buffer
,
offset
,
size
)
;
}
protected
override
void
OnError
(
SocketError
error
)
{
Console
.
WriteLine
(
$
"Client caught an error with code
{
error
}
"
)
;
Program
.
TotalErrors
++
;
}
}
class
ProtoClient
:
Client
,
ISenderListener
,
IReceiverListener
,
IDisposable
{
private
readonly
TcpProtoClient
_tcpProtoClient
;
public
Guid
Id
=>
_tcpProtoClient
.
Id
;
public
bool
IsConnected
=>
_tcpProtoClient
.
IsConnected
;
public
TcpProtoClient
TcpClient
=>
_tcpProtoClient
;
private
long
_messages
;
public
ProtoClient
(
string
address
,
int
port
,
int
messages
)
{
_messages
=
messages
;
_tcpProtoClient
=
new
TcpProtoClient
(
address
,
port
)
;
_tcpProtoClient
.
Connected
+=
OnConnected
;
_tcpProtoClient
.
Disconnected
+=
OnDisconnected
;
_tcpProtoClient
.
Received
+=
OnReceived
;
ReceivedResponse_DisconnectRequest
+=
HandleDisconnectRequest
;
ReceivedResponse_SimpleResponse
+=
HandleSimpleResponse
;
ReceivedResponse_SimpleReject
+=
HandleSimpleReject
;
ReceivedResponse_SimpleNotify
+=
HandleSimpleNotify
;
}
private
void
DisposeClient
(
)
{
_tcpProtoClient
.
Connected
-=
OnConnected
;
_tcpProtoClient
.
Connected
-=
OnDisconnected
;
_tcpProtoClient
.
Received
-=
OnReceived
;
ReceivedResponse_DisconnectRequest
-=
HandleDisconnectRequest
;
ReceivedResponse_SimpleResponse
-=
HandleSimpleResponse
;
ReceivedResponse_SimpleReject
-=
HandleSimpleReject
;
ReceivedResponse_SimpleNotify
-=
HandleSimpleNotify
;
_tcpProtoClient
.
Dispose
(
)
;
}
public
bool
ConnectAsync
(
)
{
return
_tcpProtoClient
.
ConnectAsync
(
)
;
}
public
bool
DisconnectAsync
(
)
{
return
_tcpProtoClient
.
DisconnectAsync
(
)
;
}
public
bool
ReconnectAsync
(
)
{
return
_tcpProtoClient
.
ReconnectAsync
(
)
;
}
private
void
SendMessage
(
)
{
SimpleRequest
message
=
SimpleRequest
.
Default
;
message
.
Message
=
Program
.
MessageToSend
;
Request
(
message
)
;
}
#region Connection handlers
public
delegate
void
ConnectedHandler
(
)
;
public
event
ConnectedHandler
Connected
=
(
)
=>
{
}
;
private
void
OnConnected
(
)
{
// Reset FBE protocol buffers
Reset
(
)
;
Connected
?
.
Invoke
(
)
;
for
(
long
i
=
_messages
;
i
>
0
;
i
--
)
SendMessage
(
)
;
}
public
delegate
void
DisconnectedHandler
(
)
;
public
event
DisconnectedHandler
Disconnected
=
(
)
=>
{
}
;
private
void
OnDisconnected
(
)
{
Disconnected
?
.
Invoke
(
)
;
}
public
long
OnSend
(
byte
[
]
buffer
,
long
offset
,
long
size
)
{
return
_tcpProtoClient
.
SendAsync
(
buffer
,
offset
,
size
)
?
size
:
0
;
}
public
void
OnReceived
(
byte
[
]
buffer
,
long
offset
,
long
size
)
{
Program
.
TimestampStop
=
DateTime
.
UtcNow
;
Program
.
TotalBytes
+=
size
;
Receive
(
buffer
,
offset
,
size
)
;
}
#endregion
#region Protocol handlers
private
void
HandleDisconnectRequest
(
DisconnectRequest
request
)
{
_tcpProtoClient
.
DisconnectAsync
(
)
;
}
private
void
HandleSimpleResponse
(
SimpleResponse
response
)
{
SendMessage
(
)
;
}
private
void
HandleSimpleReject
(
SimpleReject
reject
)
{
}
private
void
HandleSimpleNotify
(
SimpleNotify
notify
)
{
}
#endregion
#region IDisposable implementation
// Disposed flag.
private
bool
_disposed
;
// Implement IDisposable.
public
void
Dispose
(
)
{
Dispose
(
true
)
;
GC
.
SuppressFinalize
(
this
)
;
}
protected
virtual
void
Dispose
(
bool
disposingManagedResources
)
{
// The idea here is that Dispose(Boolean) knows whether it is
// being called to do explicit cleanup (the Boolean is true)
// versus being called due to a garbage collection (the Boolean
// is false). This distinction is useful because, when being
// disposed explicitly, the Dispose(Boolean) method can safely
// execute code using reference type fields that refer to other
// objects knowing for sure that these other objects have not been
// finalized or disposed of yet. When the Boolean is false,
// the Dispose(Boolean) method should not execute code that
// refer to reference type fields because those objects may
// have already been finalized."
if
(
!
_disposed
)
{
if
(
disposingManagedResources
)
{
// Dispose managed resources here...
DisposeClient
(
)
;
}
// Dispose unmanaged resources here...
// Set large fields to null here...
// Mark as disposed.
_disposed
=
true
;
}
}
#endregion
}
class
Program
{
public
static
string
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
=
4444
;
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
string
(
'
\0
'
,
size
)
;
// Create protocol clients
var
echoClients
=
new
List
<
ProtoClient
>
(
)
;
for
(
int
i
=
0
;
i
<
clients
;
i
++
)
{
var
client
=
new
ProtoClient
(
address
,
port
,
messages
)
;
// client.TcpClient.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
.
IsConnected
)
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"
)
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL