【问题标题】:Performance impact of using netmq poller使用 netmq poller 的性能影响
【发布时间】:2016-05-05 22:07:11
【问题描述】:

在以下代码中,我使用 netmq 推/拉套接字发送和接收 100000 条消息。我首先尝试在拉式套接字上使用 ReceiveFrameString(ReceiveSimple 方法)进行简单的阻塞调用,然后尝试使用轮询器来执行相同的操作(ReceiveWithPoller 方法)。

使用轮询器对发送/接收消息所需的时间有重大影响。我试图弄清楚为什么我自己使用 dotTrace,我发现等待 Socket.Select 执行花费了很多时间。

有人可以确认或解释这种差异吗?

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetMQ;
using NetMQ.Sockets;

namespace PushPull
{
    class Program
    {
        static int messageReceived = 0;

        const int sampleCount = 100 * 1000;

        static void Main(string[] args)
        {
            // Create psu socket
            PushSocket pushSocket = new PushSocket();
            pushSocket.Bind("tcp://localhost:5555");

            // Create pull socket
            PullSocket pullSocket = new PullSocket();
            pullSocket.Connect("tcp://localhost:5555");

            Console.WriteLine("Ready...press any key to start");
            Console.ReadKey();
            Console.WriteLine();

            // Start sending
            Task.Run(() =>
            {
                for (int i = 0; i < sampleCount; i++)
                {
                    pushSocket.SendFrame(Encoding.UTF8.GetBytes("ping"));
                }
            });

            Stopwatch sw = Stopwatch.StartNew();

            //ReceiveSimple(pullSocket);

            ReceiveWithPoller(pullSocket);

            // Display result.
            sw.Stop();
            Console.WriteLine();
            Console.WriteLine("{0} message exchanged in {1} msecs", sampleCount, sw.Elapsed.TotalMilliseconds);
        }

        private static void ReceiveSimple(PullSocket pullSocket)
        {
            messageReceived = 0;
            do
            {
                pullSocket.ReceiveFrameString();
            } while (!HandleMessage());
        }

        private static void ReceiveWithPoller(PullSocket pullSocket)
        {
            NetMQPoller poller = new NetMQPoller();
            poller.Add(pullSocket);
            pullSocket.ReceiveReady += (sender, eventArgs) =>
            {
                if (HandleMessage())
                {
                    poller.Stop();
                }
            };
            poller.Run();
        }

        private static bool HandleMessage()
        {
            messageReceived++;

            if (messageReceived % 10000 == 0)
            {
                Console.WriteLine("10k");
            }

            return messageReceived == sampleCount;
        }
    }
}

【问题讨论】:

  • 顺便说一句,您的示例中有一个错误,在 ReceiveReady 事件处理程序中,您永远不会收到来自套接字的消息。

标签: netmq


【解决方案1】:

当您处理大量消息时,轮询器很昂贵,但有一个非常简单的解决方案。

当您收到 ready 事件时获取队列中的所有消息,您可以使用 Try* 方法执行此操作,在您的示例中如下所示:

 pullSocket.ReceiveReady += (sender, eventArgs) =>
 {
    string message;
    while (pullSocket.TryReceiveFrameString(out message) 
    {
        if (HandleMessage())
        {
            poller.Stop();
        }
    }
 };

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2010-09-30
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
  • 2012-12-10
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多