-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
WebSocket.cs
239 lines (209 loc) · 8.62 KB
/
WebSocket.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Sample
{
// http://localhost:8000/?task=WebSocket
class WebSocketTask : BenchTask
{
private static readonly string DefaultAzureServer = "corefx-net-http11.azurewebsites.net";
private const string EchoHandler = "WebSocket/EchoWebSocket.ashx";
private static readonly Uri EchoServer = new Uri("ws://" + DefaultAzureServer + "/" + EchoHandler);
public override string Name => "WebSocket";
public override Measurement[] Measurements => measurements;
public override bool BrowserOnly => true;
Measurement[] measurements;
public WebSocketTask()
{
measurements = new Measurement[] {
new PartialSend_1BMeasurement(),
new PartialSend_64KBMeasurement(),
new PartialSend_1MBMeasurement(),
new PartialReceive_1BMeasurement(),
new PartialReceive_10KBMeasurement(),
new PartialReceive_100KBMeasurement(),
};
}
public abstract class WebSocketMeasurement : BenchTask.Measurement
{
protected int step;
protected ClientWebSocket client;
public override string Name => GetType().Name.Replace("Measurement", "");
public override async Task BeforeBatch()
{
step = 0;
client = new ClientWebSocket();
await client.ConnectAsync(EchoServer, CancellationToken.None);
}
public override Task AfterBatch()
{
client.Abort();
client.Dispose();
return Task.CompletedTask;
}
}
public abstract class WebSocketReceiveMeasurement : WebSocketMeasurement
{
protected const int MaxLength = 130_000;
protected const int MaxMessages = 100;
public override async Task BeforeBatch()
{
await base.BeforeBatch();
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[MaxLength]);
for (int i = 0; i < MaxLength; i++)
{
buffer[i] = (byte)(i & 0xff);
}
for (int i = 0; i < MaxMessages; i++)
{
await client.SendAsync(buffer, WebSocketMessageType.Binary, true, CancellationToken.None);
}
// make sure that message arrived to receive buffer
await Task.Delay(5000);
}
}
public class PartialSend_1BMeasurement : WebSocketMeasurement
{
public override int InitialSamples => 1000;
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1]);
protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
{
return 250_000;
}
public override void RunStep()
{
buffer[0] = (byte)(step & 0xff);
client.SendAsync(buffer, WebSocketMessageType.Binary, false, CancellationToken.None);
step++;
}
}
public class PartialSend_64KBMeasurement : WebSocketMeasurement
{
const int bufferSize = 64 * 1024;
public override int InitialSamples => 1000;
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[bufferSize]);
public PartialSend_64KBMeasurement()
{
for (int i = 0; i < bufferSize; i++)
{
buffer[i] = (byte)(i & 0xff);
}
}
protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
{
return 3000;
}
public override void RunStep()
{
buffer[0] = (byte)(step & 0xff);
client.SendAsync(buffer, WebSocketMessageType.Binary, false, CancellationToken.None);
step++;
}
}
public class PartialSend_1MBMeasurement : WebSocketMeasurement
{
const int bufferSize = 1 * 1024 * 1024;
public override int InitialSamples => 10;
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[bufferSize]);
public PartialSend_1MBMeasurement()
{
for (int i = 0; i < bufferSize; i++)
{
buffer[i] = (byte)(i & 0xff);
}
}
protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
{
return 100;
}
public override void RunStep()
{
buffer[0] = (byte)(step & 0xff);
client.SendAsync(buffer, WebSocketMessageType.Binary, false, CancellationToken.None);
step++;
}
}
public class PartialReceive_1BMeasurement : WebSocketReceiveMeasurement
{
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1]);
protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
{
return MaxLength - InitialSamples - 100;
}
public override void RunStep()
{
var task = client.ReceiveAsync(buffer, CancellationToken.None);
#if DEBUG
if (!task.IsCompleted) throw new InvalidOperationException(Name + ": Expected Completed" + step);
if (task.Result.Count != 1) throw new InvalidOperationException(Name + ": Expected full buffer" + step);
if (buffer[0] != (byte)(step & 0xff)) throw new InvalidOperationException(Name + ": Expected data" + step);
#endif
step++;
}
}
public class PartialReceive_10KBMeasurement : WebSocketReceiveMeasurement
{
const int bufferSize = 10 * 1024;
public override int InitialSamples => 1;
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[bufferSize]);
protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
{
return 500;
}
public override void RunStep()
{
var task = client.ReceiveAsync(buffer, CancellationToken.None);
#if DEBUG
if (!task.IsCompleted) throw new InvalidOperationException(Name + ": Expected Completed " + step);
if (step == 0)
{
if (task.Result.Count != buffer.Count) throw new InvalidOperationException(Name + ": Expected full buffer" + step);
for (int i = 0; i < bufferSize; i++)
{
if (buffer[i] != (byte)(i & 0xff)) throw new InvalidOperationException(Name + ": Expected data at " + i + " " + step);
}
}
#endif
step++;
}
}
public class PartialReceive_100KBMeasurement : WebSocketReceiveMeasurement
{
const int bufferSize = 100_000;
public override int InitialSamples => 1;
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[bufferSize]);
protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
{
return MaxMessages - 1;
}
public override void RunStep()
{
if (step == 0)
{
// this is GC step
client.ReceiveAsync(new ArraySegment<byte>(new byte[1]), CancellationToken.None);
return;
}
var task = client.ReceiveAsync(buffer, CancellationToken.None);
#if DEBUG
if (!task.IsCompleted) throw new InvalidOperationException(Name + ": Expected Completed " + step);
if (step == 0)
{
if (task.Result.Count != buffer.Count) throw new InvalidOperationException(Name + ": Expected full buffer" + step + " " + task.Result.Count + "!");
for (int i = 0; i < bufferSize; i++)
{
if (buffer[i] != (byte)(i & 0xff)) throw new InvalidOperationException(Name + ": Expected data at " + i + " " + step);
}
}
#endif
step++;
}
}
}
}