Reputation: 13
In the following code, I send and receive 100000 messages using netmq push/pull sockets. I first tried to do a simple blocking call using ReceiveFrameString (ReceiveSimple method) on my pull socket, then I tried to use a poller to do the same (ReceiveWithPoller method).
Using the poller has a significant impact on the time it takes to send / receive the messages. I tried to figure out why by myself using dotTrace and I found that lot of time is spent waiting for Socket.Select to execute.
Can somebody confirm or explain this difference ?
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;
}
}
}
Upvotes: 1
Views: 1530
Reputation: 4842
Poller is expensive when you handle a lot of messages, but there is a very simple solution for this.
Fetch all messages in the queue when you receive the ready event, you can do so with Try* methods, in your example like so:
pullSocket.ReceiveReady += (sender, eventArgs) =>
{
string message;
while (pullSocket.TryReceiveFrameString(out message)
{
if (HandleMessage())
{
poller.Stop();
}
}
};
Upvotes: 2