public async Task Run() { // NOTE: if the service is so busy that it cannot respond to a PING within the KeepAliveInterval interval the websocket connection will be closed // To run extreme tests it is best to set the KeepAliveInterval to TimeSpan.Zero to disable ping pong WebSocketClientOptions options = new WebSocketClientOptions() { NoDelay = true, KeepAliveInterval = TimeSpan.FromSeconds(2), SecWebSocketProtocol = "chatV2, chatV1" }; using (_webSocket = await _clientFactory.ConnectAsync(_uri, options)) { var source = new CancellationTokenSource(); _token = source.Token; Task recTask = Task.Run(ReceiveLoop); stopwatch = Stopwatch.StartNew(); for (int i = 0; i < Constants.NoOfIterations; i++) { var buffer = myTestBytes.Array.Clone() as byte[]; await _webSocket.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Binary, true, source.Token); } recTask.Wait(); /* Random rand = new Random(_seed); * _expectedValues = new byte[50][]; * for (int i = 0; i < _expectedValues.Length; i++) * { * int numBytes = rand.Next(_minNumBytesPerMessage, _maxNumBytesPerMessage); * byte[] bytes = new byte[numBytes]; * rand.NextBytes(bytes); * _expectedValues[i] = bytes; * } * * Task recTask = Task.Run(ReceiveLoop); * byte[] sendBuffer = new byte[_maxNumBytesPerMessage]; * for (int i = 0; i < _numItems; i++) * { * int index = i % _expectedValues.Length; * byte[] bytes = _expectedValues[index]; * Buffer.BlockCopy(bytes, 0, sendBuffer, 0, bytes.Length); * ArraySegment<byte> buffer = new ArraySegment<byte>(sendBuffer, 0, bytes.Length); * await _webSocket.SendAsync(myPersonBytesMap[_maxNumBytesPerMessage], WebSocketMessageType.Binary, true, source.Token); * } * * await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, source.Token); * recTask.Wait();*/ } }
public async Task Run() { WebSocketClientOptions options = new WebSocketClientOptions() { NoDelay = true, KeepAliveInterval = TimeSpan.Zero, SecWebSocketProtocol = "chatV2, chatV1" }; using (_webSocket = await _clientFactory.ConnectAsync(_uri, options)) { var source = new CancellationTokenSource(); _token = source.Token; Random rand = new Random(_seed); _expectedValues = new byte[50][]; for (int i = 0; i < _expectedValues.Length; i++) { int numBytes = rand.Next(_minNumBytesPerMessage, _maxNumBytesPerMessage); byte[] bytes = new byte[numBytes]; rand.NextBytes(bytes); _expectedValues[i] = bytes; } Task recTask = Task.Run(ReceiveLoop); byte[] sendBuffer = new byte[_maxNumBytesPerMessage]; for (int i = 0; i < _numItems; i++) { int index = i % _expectedValues.Length; byte[] bytes = _expectedValues[index]; Buffer.BlockCopy(bytes, 0, sendBuffer, 0, bytes.Length); ArraySegment <byte> buffer = new ArraySegment <byte>(sendBuffer, 0, bytes.Length); await _webSocket.SendAsync(buffer, WebSocketMessageType.Binary, true, source.Token); } await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, source.Token); recTask.Wait(); } }