Beispiel #1
0
        static void StartSendMessageTest()
        {
            var serverIP = ConfigurationManager.AppSettings["ServerAddress"];
            var serverAddress = string.IsNullOrEmpty(serverIP) ? SocketUtils.GetLocalIPV4() : IPAddress.Parse(serverIP);
            var messageSize = int.Parse(ConfigurationManager.AppSettings["MessageSize"]);

            _message = new byte[messageSize];
            _mode = ConfigurationManager.AppSettings["Mode"];
            _messageCount = int.Parse(ConfigurationManager.AppSettings["MessageCount"]);

            _client = new SocketRemotingClient(new IPEndPoint(serverAddress, 5000)).Start();

            var sendAction = default(Action);

            if (_mode == "Oneway")
            {
                sendAction = () =>
                {
                    _client.InvokeOneway(new RemotingRequest(100, _message));
                    Interlocked.Increment(ref _sentCount);
                };
            }
            else if (_mode == "Sync")
            {
                sendAction = () =>
                {
                    _client.InvokeSync(new RemotingRequest(100, _message), 5000);
                    Interlocked.Increment(ref _sentCount);
                };
            }
            else if (_mode == "Async")
            {
                sendAction = () => _client.InvokeAsync(new RemotingRequest(100, _message), 100000).ContinueWith(t =>
                {
                    if (t.Exception != null)
                    {
                        _logger.Error(t.Exception);
                        return;
                    }
                    if (t.Result.Code <= 0)
                    {
                        _logger.Error(Encoding.UTF8.GetString(t.Result.Body));
                        return;
                    }
                    Interlocked.Increment(ref _sentCount);
                });
            }
            else if (_mode == "Callback")
            {
                _client.RegisterResponseHandler(100, new ResponseHandler());
                sendAction = () => _client.InvokeWithCallback(new RemotingRequest(100, _message));
            }

            Task.Factory.StartNew(() =>
            {
                for (var i = 0; i < _messageCount; i++)
                {
                    try
                    {
                        sendAction();
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorFormat("Send remoting request failed, errorMsg:{0}", ex.Message);
                        Thread.Sleep(3000);
                    }
                }
            });
        }
Beispiel #2
0
        static void SendMessages(SocketRemotingClient client, string mode, int count, int sleepMilliseconds, int batchSize, byte[] message)
        {
            _logger.Info("----Send message test----");

            if (mode == "Oneway")
            {
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => client.InvokeOneway(new RemotingRequest(100, message)));
                    Interlocked.Increment(ref _sentCount);
                    WaitIfNecessory(batchSize, sleepMilliseconds);
                }
            }
            else if (mode == "Async")
            {
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => client.InvokeAsync(new RemotingRequest(100, message), 100000).ContinueWith(SendCallback));
                    WaitIfNecessory(batchSize, sleepMilliseconds);
                }
            }
            else if (mode == "Callback")
            {
                client.RegisterResponseHandler(100, new ResponseHandler());
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => client.InvokeWithCallback(new RemotingRequest(100, message)));
                    WaitIfNecessory(batchSize, sleepMilliseconds);
                }
            }
        }
Beispiel #3
0
        static void SendMessages(SocketRemotingClient client, string mode, int count, int sleepMilliseconds, int batchSize, byte[] message)
        {
            Console.WriteLine("----Send message test----");

            if (mode == "Oneway")
            {
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => client.InvokeOneway(new RemotingRequest(100, message)));
                    var current = Interlocked.Increment(ref _totalReceivedCount);
                    if (current % 10000 == 0)
                    {
                        Console.WriteLine("Sent {0} messages, timeSpent: {1}ms", current, _watch.ElapsedMilliseconds);
                    }
                    WaitIfNecessory(batchSize, sleepMilliseconds);
                }
            }
            else if (mode == "Async")
            {
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => client.InvokeAsync(new RemotingRequest(100, message), 100000).ContinueWith(SendCallback));
                    WaitIfNecessory(batchSize, sleepMilliseconds);
                }
            }
            else if (mode == "Callback")
            {
                client.RegisterResponseHandler(100, new ResponseHandler());
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => client.InvokeWithCallback(new RemotingRequest(100, message)));
                    WaitIfNecessory(batchSize, sleepMilliseconds);
                }
            }
        }
Beispiel #4
0
        static void StartSendMessageTest()
        {
            var serverIP = ConfigurationManager.AppSettings["ServerAddress"];
            var serverAddress = string.IsNullOrEmpty(serverIP) ? SocketUtils.GetLocalIPV4() : IPAddress.Parse(serverIP);
            var sendAction = default(Action);

            _client = new SocketRemotingClient(new IPEndPoint(serverAddress, 5000)).Start();

            if (_mode == "Oneway")
            {
                sendAction = () =>
                {
                    var request = new RemotingRequest(100, _message);
                    _client.InvokeOneway(request);
                    _performanceService.IncrementKeyCount(_performanceKey, (DateTime.Now - request.CreatedTime).TotalMilliseconds);
                };
            }
            else if (_mode == "Sync")
            {
                sendAction = () =>
                {
                    var request = new RemotingRequest(100, _message);
                    var response = _client.InvokeSync(request, 5000);
                    _performanceService.IncrementKeyCount(_performanceKey, (DateTime.Now - response.RequestTime).TotalMilliseconds);
                };
            }
            else if (_mode == "Async")
            {
                sendAction = () =>
                {
                    var request = new RemotingRequest(100, _message);
                    _client.InvokeAsync(request, 100000).ContinueWith(t =>
                    {
                        if (t.Exception != null)
                        {
                            _logger.Error(t.Exception);
                            return;
                        }
                        var response = t.Result;
                        if (response.ResponseCode <= 0)
                        {
                            _logger.Error(Encoding.UTF8.GetString(response.ResponseBody));
                            return;
                        }
                        _performanceService.IncrementKeyCount(_performanceKey, (DateTime.Now - response.RequestTime).TotalMilliseconds);
                    });
                };
            }
            else if (_mode == "Callback")
            {
                _client.RegisterResponseHandler(100, new ResponseHandler(_performanceService, _performanceKey));
                sendAction = () => _client.InvokeWithCallback(new RemotingRequest(100, _message));
            }

            Task.Factory.StartNew(() =>
            {
                for (var i = 0; i < _messageCount; i++)
                {
                    try
                    {
                        sendAction();
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorFormat("Send remotingRequest failed, errorMsg:{0}", ex.Message);
                        Thread.Sleep(3000);
                    }
                }
            });
        }