Ejemplo n.º 1
0
        private static List <Task> StartClients()
        {
            Byte[] msgOut = Encoding.UTF8.GetBytes(_message);
            ArraySegment <Byte> segmentOut = new ArraySegment <Byte>(msgOut, 0, msgOut.Length);

            SendingStragegy sendingStrategy = _messageLength == _frameLength ?
                                              (SendingStragegy) new SingleFrameSendingStrategy(segmentOut) :
                                              (SendingStragegy) new MultiFrameSendingStrategy(_frameLength, segmentOut);

            Random ran = new Random(DateTime.Now.Millisecond);

            return(Enumerable.Range(0, _amount)
                   .Select(i => Task.Run(() => StartClient(ran, _cancellation.Token, sendingStrategy)))
                   .ToList());
        }
Ejemplo n.º 2
0
        private static async Task StartClient(Random ran, CancellationToken cancel, SendingStragegy strategy)
        {
            Byte[] msgIn = new Byte[4096];
            ArraySegment <Byte> segmentIn = new ArraySegment <Byte>(msgIn, 0, msgIn.Length);

            while (!cancel.IsCancellationRequested)
            {
                await Task.Delay(ran.Next(0, 450), cancel).ConfigureAwait(false);

                ClientWebSocket client = new ClientWebSocket();
                try
                {
                    await client.ConnectAsync(_host, cancel).ConfigureAwait(false);

                    Console.Write("·");
                    PerformanceCountersHelper.Connected.Increment();
                    Stopwatch stopwatch = new Stopwatch();

                    while (!cancel.IsCancellationRequested)
                    {
                        stopwatch.Start();
                        await strategy.Send(client, cancel).ConfigureAwait(false);

                        String message = null;
                        WebSocketReceiveResult result = await client.ReceiveAsync(segmentIn, cancel).ConfigureAwait(false);

                        // it could be more compact, but this way avoids creating the stream until the await returns with an actual response
                        using (var ms = new MemoryStream())
                        {
                            ms.Write(segmentIn.Array, segmentIn.Offset, result.Count);

                            while (!result.EndOfMessage)
                            {
                                result = await client.ReceiveAsync(segmentIn, cancel).ConfigureAwait(false);

                                ms.Write(segmentIn.Array, segmentIn.Offset, result.Count);
                            }

                            stopwatch.Stop();

                            var array = ms.ToArray();
                            message = Encoding.UTF8.GetString(array, 0, array.Length);
                        }

                        if (!String.IsNullOrEmpty(message) && !message.Equals(_message, StringComparison.Ordinal))
                        {
                            throw new Exception("Response is different from request, sent: " + _frameLength + ", received: " + (message == null ? 0 : message.Length).ToString());
                        }

                        PerformanceCountersHelper.EchoLatency.IncrementBy(stopwatch.ElapsedTicks);
                        PerformanceCountersHelper.EchoLatencyBase.IncrementBy(1);
                        PerformanceCountersHelper.EchoCount.Increment();

                        if (_delay != 0)
                        {
                            await Task.Delay(_delay, cancel).ConfigureAwait(false);
                        }

                        stopwatch.Reset();
                    }
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception ex)
                {
                    ShowException(ex);
                    _cancellation.Cancel();
                }
                finally
                {
                    PerformanceCountersHelper.Connected.Decrement();
                    Console.Write("~");
                    client.Dispose();
                }
            }
        }