Ejemplo n.º 1
0
 public async Task<AsyncTaskResult> SendMessageAsync(Producer producer, EQueueMessage message, object routingKey)
 {
     try
     {
         var result = await producer.SendAsync(message, routingKey);
         if (result.SendStatus != SendStatus.Success)
         {
             return new AsyncTaskResult(AsyncTaskStatus.IOException, result.ErrorMessage);
         }
         return AsyncTaskResult.Success;
     }
     catch (Exception ex)
     {
         return new AsyncTaskResult(AsyncTaskStatus.IOException, ex.Message);
     }
 }
Ejemplo n.º 2
0
 static void SendMessage(Producer producer, IndexEntry indexEntry)
 {
     var message = "message" + Interlocked.Increment(ref messageIndex);
     producer.SendAsync(new Message("SampleTopic", Encoding.UTF8.GetBytes(message)), indexEntry.Index.ToString()).ContinueWith(sendTask =>
     {
         var finishedCount = Interlocked.Increment(ref finished);
         if (finishedCount % 1000 == 0)
         {
             _logger.InfoFormat("Sent {0} messages, time spent:{1}", finishedCount, watch.ElapsedMilliseconds);
         }
         if (indexEntry.Index < indexEntry.Total)
         {
             indexEntry.Index++;
             SendMessage(producer, indexEntry);
         }
     });
 }
Ejemplo n.º 3
0
 public async Task<AsyncTaskResult> SendMessageAsync(Producer producer, EQueueMessage message, string routingKey)
 {
     try
     {
         var result = await producer.SendAsync(message, routingKey);
         if (result.SendStatus != SendStatus.Success)
         {
             _logger.ErrorFormat("EQueue message async send failed, sendResult: {0}, routingKey: {1}", result, routingKey);
             return new AsyncTaskResult(AsyncTaskStatus.IOException, result.ErrorMessage);
         }
         _logger.InfoFormat("EQueue message async send success, sendResult: {0}, routingKey: {1}", result, routingKey);
         return AsyncTaskResult.Success;
     }
     catch (Exception ex)
     {
         _logger.Error(string.Format("EQueue message async send has exception, message: {0}, routingKey: {1}", message, routingKey), ex);
         return new AsyncTaskResult(AsyncTaskStatus.IOException, ex.Message);
     }
 }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            InitializeEQueue();

            var brokerIP = ConfigurationManager.AppSettings["BrokerIP"];
            var brokerEndPoint = string.IsNullOrEmpty(brokerIP) ? new IPEndPoint(SocketUtils.GetLocalIPV4(), 5000) : new IPEndPoint(IPAddress.Parse(brokerIP), 5000);
            var producer = new Producer("Producer1", new ProducerSetting { BrokerProducerIPEndPoint = brokerEndPoint }).Start();
            var messageSize = int.Parse(ConfigurationManager.AppSettings["MessageSize"]);
            var messageCount = int.Parse(ConfigurationManager.AppSettings["MessageCount"]);
            var message = new byte[messageSize];
            var sendCallback = new Action<Task<SendResult>>(sendTask =>
            {
                if (sendTask.Exception != null)
                {
                    _logger.ErrorFormat("Sent message failed, errorMessage: {0}", sendTask.Exception.GetBaseException().Message);
                    return;
                }
                if (sendTask.Result.SendStatus == SendStatus.Success)
                {
                    var finishedCount = Interlocked.Increment(ref finished);
                    if (finishedCount == 1)
                    {
                        watch = Stopwatch.StartNew();
                    }
                    if (finishedCount % 10000 == 0)
                    {
                        _logger.InfoFormat("Sent {0} messages, time spent:{1}", finishedCount, watch.ElapsedMilliseconds);
                    }
                }
                else
                {
                    _logger.ErrorFormat("Sent message failed, errorMessage: {0}", sendTask.Result.ErrorMessage);
                }
            });

            for (var index = 1; index <= messageCount; index++)
            {
                producer.SendAsync(new Message("SampleTopic", 100, message), Interlocked.Increment(ref messageIndex)).ContinueWith(sendCallback);
            }

            Console.ReadLine();
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            InitializeEQueue();

            var scheduleService = ObjectContainer.Resolve<IScheduleService>();
            var producer = new Producer().Start();
            var total = 1000;
            var parallelCount = 10;
            var finished = 0;
            var messageIndex = 0;
            var watch = Stopwatch.StartNew();

            var action = new Action(() =>
            {
                for (var index = 1; index <= total; index++)
                {
                    var message = "message" + Interlocked.Increment(ref messageIndex);
                    producer.SendAsync(new Message("SampleTopic", Encoding.UTF8.GetBytes(message)), index.ToString()).ContinueWith(sendTask =>
                    {
                        var finishedCount = Interlocked.Increment(ref finished);
                        if (finishedCount % 1000 == 0)
                        {
                            Console.WriteLine(string.Format("Sent {0} messages, time spent:{1}", finishedCount, watch.ElapsedMilliseconds));
                        }
                    });
                }
            });

            var actions = new List<Action>();
            for (var index = 0; index < parallelCount; index++)
            {
                actions.Add(action);
            }

            Parallel.Invoke(actions.ToArray());

            Console.ReadLine();
        }
Ejemplo n.º 6
0
        static Task SendMessageAsync(Producer producer, Message message, string routingKey)
        {
            return producer.SendAsync(message, routingKey).ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    _logger.ErrorFormat("Send message has exception, errorMessage: {0}", t.Exception.GetBaseException().Message);
                    return;
                }
                if (t.Result == null)
                {
                    _logger.Error("Send message timeout.");
                    return;
                }
                if (t.Result.SendStatus != SendStatus.Success)
                {
                    _logger.ErrorFormat("Send message failed, errorMessage: {0}", t.Result.ErrorMessage);
                    return;
                }

                _sendResultLogger.InfoFormat("Message send success, routingKey: {0}, sendResult: {1}", routingKey, t.Result);
                Interlocked.Increment(ref _sentCount);
            });
        }
Ejemplo n.º 7
0
        static void StartProducer()
        {
            var producer = new Producer("Producer1").Start();
            var total = 1000;
            var parallelCount = 10;
            var finished = 0;
            var messageIndex = 0;
            var watch = Stopwatch.StartNew();

            var action = new Action(() =>
            {
                for (var index = 1; index <= total; index++)
                {
                    var message = "message" + Interlocked.Increment(ref messageIndex);
                    producer.SendAsync(new Message("SampleTopic", Encoding.UTF8.GetBytes(message)), index).ContinueWith(sendTask =>
                    {
                        var finishedCount = Interlocked.Increment(ref finished);
                        if (finishedCount % 1000 == 0)
                        {
                            _logger.InfoFormat("Sent {0} messages, time spent:{1}", finishedCount, watch.ElapsedMilliseconds);
                        }
                    });
                }
            });

            var actions = new List<Action>();
            for (var index = 0; index < parallelCount; index++)
            {
                actions.Add(action);
            }

            Parallel.Invoke(actions.ToArray());
        }
Ejemplo n.º 8
0
        static void SendMessages(Producer producer, string mode, int messageCount, Message message)
        {
            _logger.Info("----Send message starting----");

            var sendAction = default(Action<int>);

            if (_mode == "Oneway")
            {
                sendAction = index =>
                {
                    producer.SendOneway(message, index.ToString());
                    Interlocked.Increment(ref _sentCount);
                };
            }
            else if (_mode == "Sync")
            {
                sendAction = index =>
                {
                    var result = producer.Send(message, index.ToString());
                    if (result.SendStatus != SendStatus.Success)
                    {
                        throw new Exception(result.ErrorMessage);
                    }
                    Interlocked.Increment(ref _sentCount);
                };
            }
            else if (_mode == "Async")
            {
                sendAction = index => producer.SendAsync(message, index.ToString()).ContinueWith(t =>
                {
                    if (t.Exception != null)
                    {
                        _hasError = true;
                        _logger.ErrorFormat("Send message has exception, errorMessage: {0}", t.Exception.GetBaseException().Message);
                        return;
                    }
                    if (t.Result == null)
                    {
                        _hasError = true;
                        _logger.Error("Send message timeout.");
                        return;
                    }
                    if (t.Result.SendStatus != SendStatus.Success)
                    {
                        _hasError = true;
                        _logger.ErrorFormat("Send message failed, errorMessage: {0}", t.Result.ErrorMessage);
                        return;
                    }

                    Interlocked.Increment(ref _sentCount);
                });
            }
            else if (_mode == "Callback")
            {
                producer.RegisterResponseHandler(new ResponseHandler());
                sendAction = index => producer.SendWithCallback(message, index.ToString());
            }

            Task.Factory.StartNew(() =>
            {
                for (var i = 0; i < messageCount; i++)
                {
                    try
                    {
                        sendAction(i);
                    }
                    catch (Exception ex)
                    {
                        _hasError = true;
                        _logger.ErrorFormat("Send message failed, errorMsg:{0}", ex.Message);
                    }

                    if (_hasError)
                    {
                        Thread.Sleep(3000);
                        _hasError = false;
                    }
                }
            });
        }
Ejemplo n.º 9
0
        static void SendMessage()
        {
            var brokerIP = ConfigurationManager.AppSettings["BrokerAddress"];
            var brokerEndPoint = string.IsNullOrEmpty(brokerIP) ? new IPEndPoint(SocketUtils.GetLocalIPV4(), 5000) : new IPEndPoint(IPAddress.Parse(brokerIP), 5000);
            var messageSize = int.Parse(ConfigurationManager.AppSettings["MessageSize"]);
            var connectionCount = int.Parse(ConfigurationManager.AppSettings["ConnectionCount"]);
            var flowControlCount = int.Parse(ConfigurationManager.AppSettings["FlowControlCount"]);
            var maxMessageCount = int.Parse(ConfigurationManager.AppSettings["MaxMessageCount"]);
            var payload = new byte[messageSize];
            var message = new Message("SampleTopic", 100, payload);
            var messageIndex = 0L;
            var sendingCount = 0L;
            var finishedCount = 0L;
            var previousFinishedCount = 0L;
            var previousElapsedMilliseconds = 0L;
            var flowControlledCount = 0;
            var watch = Stopwatch.StartNew();

            for (var i = 0; i < connectionCount; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    var producer = new Producer("Producer@" + ObjectId.GenerateNewStringId(), new ProducerSetting { BrokerProducerIPEndPoint = brokerEndPoint }).Start();

                    while (true)
                    {
                        var waitingCount = sendingCount - finishedCount;
                        if (waitingCount > flowControlCount)
                        {
                            Thread.Sleep(1);
                            var current = Interlocked.Increment(ref flowControlledCount);
                            if (current % 1000 == 0)
                            {
                                _logger.InfoFormat("Start to flow control, pending messages: {0}, flow count: {1}", waitingCount, current);
                            }
                            continue;
                        }
                        producer.SendAsync(message, Interlocked.Increment(ref messageIndex), 300000).ContinueWith(sendTask =>
                        {
                            if (sendTask.Exception != null)
                            {
                                _logger.ErrorFormat("Sent message failed, errorMessage: {0}", sendTask.Exception.GetBaseException().Message);
                                return;
                            }
                            if (sendTask.Result.SendStatus == SendStatus.Success)
                            {
                                var currentFinishedCount = Interlocked.Increment(ref finishedCount);
                                if (currentFinishedCount % 10000 == 0)
                                {
                                    var currentElapsedMilliseconds = watch.ElapsedMilliseconds;
                                    _logger.InfoFormat("Sent {0} messages, time elapsed: {1}ms, current throughput: {2}, average throughput: {3}",
                                        currentFinishedCount,
                                        currentElapsedMilliseconds,
                                        (currentFinishedCount - previousFinishedCount) * 1000 / (currentElapsedMilliseconds - previousElapsedMilliseconds),
                                        currentFinishedCount * 1000 / currentElapsedMilliseconds);
                                    Interlocked.Exchange(ref previousFinishedCount, currentFinishedCount);
                                    Interlocked.Exchange(ref previousElapsedMilliseconds, currentElapsedMilliseconds);
                                }
                            }
                            else
                            {
                                _logger.ErrorFormat("Sent message failed, errorMessage: {0}", sendTask.Result.ErrorMessage);
                            }
                        });
                        var count = Interlocked.Increment(ref sendingCount);
                        if (count >= maxMessageCount)
                        {
                            break;
                        }
                    }
                });
            }
        }
Ejemplo n.º 10
0
        static void SendMessages(Producer producer, string mode, int count, int sleepMilliseconds, int batchSize, Message message)
        {
            _logger.InfoFormat("----Send message starting, producerId:{0}----", producer.Id);

            if (mode == "Oneway")
            {
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => producer.SendOneway(message, message.Key));
                    var current = Interlocked.Increment(ref _sendingCount);
                    if (current % 10000 == 0)
                    {
                        _logger.InfoFormat("Sening {0} messages, timeSpent: {1}ms, throughput: {2}/s", current, _watch.ElapsedMilliseconds, current * 1000 / _watch.ElapsedMilliseconds);
                    }
                    WaitIfNecessory(current, batchSize, sleepMilliseconds);
                }
            }
            else if (mode == "Async")
            {
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => producer.SendAsync(message, message.Key, 100000).ContinueWith(SendCallback));
                    var current = Interlocked.Increment(ref _sendingCount);
                    WaitIfNecessory(current, batchSize, sleepMilliseconds);
                }
            }
            else if (mode == "Callback")
            {
                producer.RegisterResponseHandler(new ResponseHandler());
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => producer.SendWithCallback(message, message.Key));
                    var current = Interlocked.Increment(ref _sendingCount);
                    WaitIfNecessory(current, batchSize, sleepMilliseconds);
                }
            }
        }