private async Task <TcpNetworkConnector> StartClient(CancellationToken cancellationToken, int port)
        {
            TcpNetworkConnector tcpNetworkConnector = new TcpNetworkConnector(MessageTypeCache, MessageSerializer, MessageProcessor, Logger, Host, port);
            await tcpNetworkConnector.ConnectAsync(cancellationToken);

            tcpNetworkConnector.Start();
            return(tcpNetworkConnector);
        }
Exemple #2
0
        public async Task RunAsync(Action <string> log)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            List <Task> tasks = new List <Task>();

            for (int j = 0; j < ClientCount; j++)
            {
                Task clientTask = Task.Run(async() =>
                {
                    IMessageProcessor messageProcessor   = new ClientMessageProcessor();
                    IMessageSerializer messageSerializer = new MessagePackMessageSerializer();
                    INetworkConnector networkConnector   = new TcpNetworkConnector(messageSerializer, messageProcessor, Host, Port);
                    await networkConnector.ConnectAsync(CancellationToken.None);
                    networkConnector.Start();

                    MessageListener <LoginResponse> loginResponseListener = new MessageListener <LoginResponse>();
                    loginResponseListener.Subscribe(messageProcessor);
                    for (int i = 0; i < MessageCount; i++)
                    {
                        LoginRequest loginRequest = new LoginRequest(Guid.NewGuid(), "Mario", "password");
                        await networkConnector.SendMessageAsync(loginRequest, CancellationToken.None);
                        LoginResponse loginResponse = await loginResponseListener.ReceiveMessageAsync();
                        log.Invoke($"Success: {loginResponse.IsSuccess}, Username: {loginResponse.UserDto.Name}, RequestId: {loginResponse.RequestId}, ResponseId: {loginResponse.Id}");
                    }
                    loginResponseListener.Unsubscribe();
                });
                tasks.Add(clientTask);
            }

            await Task.WhenAll(tasks).ContinueWith(action =>
            {
                stopwatch.Stop();
                log.Invoke($"Messages per second: {TotalMessages / stopwatch.Elapsed.TotalSeconds:N0}");
            });
        }